How to Create and Manage Pod with multiple containers using Podman

How to Create and Manage Pod with multiple containers using Podman

podman_multiple_container_pod

Here in this article we will see how we Create and Manage Pods with multiple containers. We will create a single pod and launch a set of containers within the pod.

Test Environment

Fedora 32
Podman

What is Podman Pod

Podman Pod is a concept based Kubernetes Pod. A Pod is basic unit that can be deployed in Kubernetes. Its a group of containers that are launched together provide application services which are tightly coupled or inter dependent. A Pod can contain a single container or multiple containers related to an application to launch them as different services of an application.

Benefits of Pod

  • They are grouped together to share the same resources and namespace
  • Used to deploy a set of services as a single application
  • Easy to manage an entire application as single entity
  • Specific services of the application can be exposed

These containers with a pod can now be managed as a single entity. We can start or stop the containers within a single pod and check the statistics of the pod with multiple containers as a single entity. We will also generate pod definition in yaml format which can further later be used in Kubernetes Orchestration Platform to launch as an application.

If you are interested in watching the video. Here is the YouTube video for the below mentioned step by step procedure.

Procedure

Step1: Create a Pod

Here in this step we will create an empty pod named ‘busypod’. This pod will be used in next step launch containers from within it.

[admin@fed32 ~]$ podman pod create --name busypod

Step2: Launch Containers within a Pod

Once the empty pod is created in Step1 we will use that to launch multiple busybox containers within it as shown below. For each container that you run within the pod you will be provided with a bash terminal for that container which you can exit to stop that container.

[admin@fed32 ~]$ podman run -it --pod busypod --name busy11 busybox
[admin@fed32 ~]$ podman run -it --pod busypod --name busy12 busybox
[admin@fed32 ~]$ podman run -it --pod busypod --name busy13 busybox

Step3: Start the Pod to launch all the containers within it

Now, we have a pod with multiple containers within it in stopped state. We can start all the containers within a pod using the below command.

[admin@fed32 ~]$ podman pod start busypod

Step4: Get the statistics of the Pod containers

We can verify the CPU, Memory and IOPS utilization for the pod with the stats commands as shown below.

[admin@fed32 ~]$ podman pod stats busypod

POD           CID           NAME                CPU %  MEM USAGE/ LIMIT   MEM %  NET IO   BLOCK IO  PIDS
c558fdab7202  f892485b3b82  busy11              2.64%  1.053MB / 2.047GB  0.05%  -- / --  -- / --   1
c558fdab7202  66165196f025  busy12              2.92%  1.061MB / 2.047GB  0.05%  -- / --  -- / --   1
c558fdab7202  9ce5fd89107a  busy13              2.50%  1.049MB / 2.047GB  0.05%  -- / --  -- / --   1
c558fdab7202  c6704d206539  c558fdab7202-infra  2.84%  761.9kB / 2.047GB  0.04%  -- / --  -- / --   1

Step5: Stop the Pod to shutdown all the containers within it

Once, we have verified the statistics of the pod we can stop the pod to which will stop all the containers within as shown below.

[admin@fed32 ~]$ podman pod stop busypod

Step6: Genereate Kubernetes Pod yaml defninition

As a last step, we can generate pod yaml definition as shown below which can be used in Kubernetes orchestration engines to launch pod in that environment. This will be really useful if you want to migrate the pods that you created in your local system to a orchestration engine like kubernetes for rollout.

[admin@fed32 ~]$ podman generate kube busypod
# Generation of Kubernetes YAML is still under development!
#
# Save the output of this file and use kubectl create -f to import
# it into Kubernetes.
#
# Created with podman-2.2.0
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2020-12-24T00:41:43Z"
  labels:
    app: busypod
  name: busypod
spec:
  containers:
  - command:
    - sh
    env:
    - name: PATH
      value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    - name: TERM
      value: xterm
    - name: container
      value: podman
    - name: HOSTNAME
      value: busypod
    image: docker.io/library/busybox:latest
    name: busy12
    resources: {}
    securityContext:
      allowPrivilegeEscalation: true
      capabilities:
        drop:
        - CAP_MKNOD
        - CAP_NET_RAW
        - CAP_AUDIT_WRITE
      privileged: false
      readOnlyRootFilesystem: false
      seLinuxOptions: {}
    stdin: true
    tty: true
    workingDir: /
  - command:
    - sh
    env:
    - name: PATH
      value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    - name: TERM
      value: xterm
    - name: container
      value: podman
    - name: HOSTNAME
      value: busypod
    image: docker.io/library/busybox:latest
    name: busy13
    resources: {}
    securityContext:
      allowPrivilegeEscalation: true
      capabilities:
        drop:
        - CAP_MKNOD
        - CAP_NET_RAW
        - CAP_AUDIT_WRITE
      privileged: false
      readOnlyRootFilesystem: false
      seLinuxOptions: {}
    stdin: true
    tty: true
    workingDir: /
  - command:
    - sh
    env:
    - name: PATH
      value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    - name: TERM
      value: xterm
    - name: container
      value: podman
    - name: HOSTNAME
      value: busypod
    image: docker.io/library/busybox:latest
    name: busy11
    resources: {}
    securityContext:
      allowPrivilegeEscalation: true
      capabilities:
        drop:
        - CAP_MKNOD
        - CAP_NET_RAW
        - CAP_AUDIT_WRITE
      privileged: false
      readOnlyRootFilesystem: false
      seLinuxOptions: {}
    stdin: true
    tty: true
    workingDir: /
  restartPolicy: Never
status: {}
---
metadata:
  creationTimestamp: null
spec: {}
status:
  loadBalancer: {}

Hope you enjoyed reading this article. Thank you..