Container Orchestration In Podman Pods With Kubernetes Compatibility

Containerization has revolutionized the way we deploy and manage applications, offering lightweight and scalable solutions for a variety of workloads. The Podman, a container management tool, provides a flexible and user-friendly alternative to traditional container orchestration systems.

If you have any questions or comments, don’t forget to join my Discord: https://discord.gg/YbSYGsQYES

Why Podman?

Podman is a container management tool that stands out for its simplicity, security, and compatibility with Kubernetes. Unlike traditional container runtimes, Podman doesn’t require a daemon to run, making it a more lightweight and user-friendly option. It also provides a seamless transition to Kubernetes, allowing users to manage containers using Kubernetes commands without the need for a dedicated Kubernetes cluster.

What Is A Podman Pod?

A Podman Pod is a group of containers that share resources and are managed by the podman command-line tool . The podman pod command is used to manage pods, and it has various subcommands to clone, create, inspect, kill, log, pause, prune, start, stop, restart, remove, and unpause pods.

Podman pods are similar to Kubernetes definitions, and every Podman pod includes an “infra” container that holds the namespaces associated with the pod and allows Podman to connect other containers to the pod.

What Is Podman Kube?

Have you ever thought about testing Kubernetes pods locally before importing them in the K8s infrastructure? Do you need multiple logical groups of containers? Here is a solution with Podman Kube and Podman Pod!

podman-kube is a command-line tool that allows you to manage containers, pods, and volumes based on a structured input file, such as YAML. The podman-kube command has several subcommands, including play, generate, apply, and down, which allow you to create, modify, and delete containers, pods, and volumes.

For example, you can use the podman-kube play command to recreate containers, pods, or volumes based on the input from a structured file input. Conversely, the podman-kube generate command exports your existing containers into Kubernetes Pod YAML for import into an OpenShift or Kubernetes cluster.

Creating A New Pod In Podman

To create a pod in Podman with volumes and published ports, you can use the podman pod create command. This command creates an empty pod and prepares it to have containers added to it. You can then use podman create --pod <pod_id|pod_name> ... to add containers to the pod, and podman pod start <pod_id|pod_name> to start the pod.

To create a pod with volumes, you can use the podman volume create command. This command creates a new volume that can be used by containers in the pod. You can then use podman create --pod <pod_id|pod_name> --volume <volume_name>:<container_path> ... to add containers to the pod with the specified volume.

To publish ports, you can use the --publish or -p flag with the podman pod create command. By default, Podman publishes TCP ports. To publish a UDP port instead, give udp as protocol. To publish both TCP and UDP ports, set --publish twice, with tcp, and udp as protocols respectively. Rootful containers can also publish ports using the sctp protocol. The host port does not have to be specified (e.g. podman run -p 127.0.0.1::80).

Here is an example command to create a pod with a volume and published ports:

podman pod create --name itcafe \
  --publish 8080:80 --publish 4430:443

This command creates a pod named itcafe. The ports must be published during the pod creation. It is not possible to modify the published ports after creating a pod as per my current information.

Creating Podman Containers In Pods

To add a container to a pod in Podman, you can use the podman create command with the --pod option. Here is an example command to add two containers to a pod named itcafe:

podman create --pod itcafe --tty \
  --interactive --network n_itcafe \
  --volume v_itcafe:/var/log \
  --name dbserver \
  docker.io/library/debian:12

podman create --pod itcafe --tty \
  --interactive --network n_itcafe \
  --volume v_itcafe:/var/log \
  --name webserver \
  docker.io/library/debian:12

This command creates two containers (dbserver, webserver) with the image docker.io/library/debian:12 and adds them to the pod itcafe.

podman ps -a
CONTAINER ID  IMAGE                           COMMAND     CREATED         STATUS      PORTS                                        NAMES
c5cf869b8f7d  localhost/podman-pause:4.3.1-0              8 minutes ago   Created     0.0.0.0:4430->443/tcp, 0.0.0.0:8080->80/tcp  3af088e5c550-infra
363e62d629f6  docker.io/library/debian:12     bash        52 seconds ago  Created                                                  dbserver
8a8b207f872d  docker.io/library/debian:12     bash        22 seconds ago  Created                                                  webserver

Now you can start our pod with the containers in it:

podman pod start itcafe 
3af088e5c5503350fee1d23e4ee6f3a814a9cdc61050ff5fd69c83df7c08ea16

podman ps -a
CONTAINER ID  IMAGE                           COMMAND     CREATED             STATUS            PORTS                                        NAMES
c5cf869b8f7d  localhost/podman-pause:4.3.1-0              9 minutes ago       Up 2 seconds ago  0.0.0.0:4430->443/tcp, 0.0.0.0:8080->80/tcp  3af088e5c550-infra
363e62d629f6  docker.io/library/debian:12     bash        About a minute ago  Up 2 seconds ago                                               dbserver
8a8b207f872d  docker.io/library/debian:12     bash        About a minute ago  Up 2 seconds ago                                               webserver

Alternatively you can add containers to a running pod with the podman run command:

podman run --pod itcafe --rm \
  --name agent1 --init \
  docker.io/jenkins/inbound-agent \
  -url http://localhost:8080 \
  -secret <secret> -name <agent_name>

Checking The Information Of A Pod

The inspect option can show the information about a pod.

podman pod inspect itcafe
{
     "Id": "2789cbf85519851631b960265915eddbaed3b06f3e46f57521856f06784ad27e",
     "Name": "itcafe",
     "Created": "2023-11-29T11:41:53.838971679+01:00",

(...)

          {
               "Id": "8c8833d32adada33a279353e53c1e688f88e0c9bd9a8120fc44323355e725e45",
               "Name": "sqlserver",
               "State": "running"
          }
     ]
}

Stopping A Pod

We can stop a pod using the stop option of the podman pod command.

podman pod stop itcafe

Generating A Kubernetes Pod Configuration File From a Podman Pod

podman kube generate is a command-line tool that generates Kubernetes YAML files from existing Podman containers, pods, or volumes . The generated YAML files are based on the Kubernetes v1 specification . Regardless of whether the input is for containers or pods, Podman generates the specification as a Pod by default . The input may be in the form of one or more containers, pods, or volumes names or IDs . Bind-mounted volumes become hostPath volume types, while named volumes become persistentVolumeClaim volume types . The generated YAML files can be used to fire up new pods or re-run the deployment via podman-play-kube .

podman kube generate itcafe --filename itcafe.yml

The config file will be written to the itcafe.yml file. Using this configuration file you can reproduce the pod or import it to a Kubernetes cluster.

If you like this article, don’t forget to join my Discord and engage in conversation with tech professionals: https://discord.gg/YbSYGsQYES

Leave a comment