Podman — Extending Services Out to Systemd and Kubernetes

Jason Bell
3 min readApr 18, 2024

Podman, the container management tool that emerged as a daemonless alternative to Docker, has gained traction for its simplicity and integration capabilities with various system components, including Systemd and Kubernetes.

During 2023/2024 it became my default container platform of choice, easier to handle than Docker in my opinion. While Podman, like Docker, has a graphical UI interface, I never use it. I’m strictly on the command line.

This article explores how Podman can generate Systemd unit files and Kubernetes (K8s) deployment templates, which can be instrumental in managing container lifecycles and ensuring containers are properly handled at system startup or during a node reboot.

Generating Systemd Unit Files with Podman

Why Use Systemd with Podman?

Systemd is the de facto initialisation and service management scheme for various Linux distributions. Integrating Podman with Systemd allows users to manage container lifecycles using standard systemctl commands, aligning container management with other native system services. This is particularly useful for ensuring that containers start in the correct order and restart automatically upon system reboot or failure.

Step-by-Step Generation of Systemd Templates

1. Creating a Pod with Podman

Before generating a Systemd unit file, you need an existing Podman pod. Here’s how to create one:

$ podman pod create — name mypod -p 8080:80

2. Adding Containers to the Pod

Add one or more containers to the pod:


$ podman run — pod mypod -d nginx
$ podman run — pod mypod -d redis

3. Generating the Systemd Unit File

To generate a Systemd unit file for the pod:

podman generate systemd — files — name mypod > /etc/systemd/system/mypod.service

This command creates a `.service` file in the `/etc/systemd/system` directory, which can be controlled using systemctl.

4. Enabling and Starting the Service

Enable the service to start on boot and start it immediately (usually as the root user):

$ systemctl enable mypod.service
$ systemctl start mypod.service

Managing Service on Reboot

To ensure the service restarts automatically after a reboot, simply ensure the service is enabled. Systemd handles the rest, attempting to restart the service according to the configuration specified in the unit file.

Generating Kubernetes Templates with Podman

Why Kubernetes?

Kubernetes is a powerful orchestration tool for managing containerized applications across a cluster. Generating Kubernetes YAML templates from existing Podman containers can simplify the process of deploying these containers on a Kubernetes cluster.

Generating Kubernetes Deployment Templates

1. Exporting Podman Pods to Kubernetes

Assuming you have a Podman pod running, you can export it to a Kubernetes YAML file:


podman generate kube mypod > mypod.yml

This command generates a Kubernetes deployment file based on your existing Podman pod configuration.

2. Deploying to Kubernetes

To deploy your pod to a Kubernetes cluster, use the following command:


kubectl apply -f mypod.yml

This will create the necessary deployment and service resources in your Kubernetes cluster.

Managing Deployments on Node Reboot

Kubernetes inherently manages pods’ lifecycles and ensures that pods are scheduled for restart after a node reboot. Ensure your Kubernetes cluster is correctly configured to manage pod lifecycle, including setting the appropriate restart policies in your deployment templates.

Conclusion

Integrating Podman with Systemd and Kubernetes bridges the gap between container management and native system service management, providing a robust method for handling container lifecycles across different environments.

Whether through Systemd unit files or Kubernetes deployment templates, Podman simplifies the process of container orchestration, making it an indispensable tool for modern DevOps practices.

By following the steps outlined above, developers and system administrators can ensure that their services are durable and resilient, automatically recovering from reboots or failures without manual intervention.

Just don’t forget to add monitoring so you can keep an eye on things, you should be alerted if a reboot happens.

--

--

Jason Bell

A polymath of ML/AI, expert in container deployments and engineering. Author of two machine learning books for Wiley Inc.