Working with pods with podman generate and podman play

Podman pods are a way to manage group of application containers together as one pod. It is similar in that way to Kubernetes pods. While you may add many containers as you need with a pod, it would be easier if you can export and import pod manifests entirely. This would allow you to easily create pod with requisite containers rather than running a bunch of commands. You can also use generated manifest to create kubernetes pods. podman generate is a way to generate pod definition manifest yaml format. Similarly, podman play is to import pod definition and spin up a pod for you.

Create Pod for the demo

If you want to know more about pods and how to work with them, refer to our previous blog post. Lets create a new pod wpapp_pod containing wordpress and mariadb inside it:

# create pod and run mariadb container inside it
[cloud_user@5ecaa360f91c ~]$ sudo podman run \
> -d --restart=always --pod new:wpapp_pod \
> -e MYSQL_ROOT_PASSWORD="myrootpass" \
> -e MYSQL_DATABASE="wp-db" \
> -e MYSQL_USER="wp-user" \
> -e MYSQL_PASSWORD="w0rdpr3ss" \
> -p 8080:80 \
> --name=wptest-db mariadb
870e4dd0c22b515d1fe72559bea93c506903f5936bf2fc87fbd9a691a3e96b0d


# add wordpress container to previously created pod
[cloud_user@5ecaa360f91c ~]$ 
[cloud_user@5ecaa360f91c ~]$ sudo podman run \
> -d --restart=always --pod=wpapp_pod \
> -e WORDPRESS_DB_NAME="wp-db" \
> -e WORDPRESS_DB_USER="wp-user" \
> -e WORDPRESS_DB_PASSWORD="w0rdpr3ss" \
> -e WORDPRESS_DB_HOST="127.0.0.1" \
> --name wptest-web wordpress
8f399390babd4f761b271a8c921991a5302cf15b0e558a553b0ebcf9fee188bf


# verify pod is up and running 
[cloud_user@5ecaa360f91c ~]$ sudo podman pod list
POD ID        NAME       STATUS   CREATED        INFRA ID      # OF CONTAINERS
4ae6b452365b  wpapp_pod  Running  2 minutes ago  916ffdfda18b  3


# verify containers running in the pod
[cloud_user@5ecaa360f91c ~]$ sudo podman ps -a --pod
CONTAINER ID  IMAGE                               COMMAND               CREATED        STATUS            PORTS                 NAMES               POD ID        PODNAME
8f399390babd  docker.io/library/wordpress:latest  apache2-foregroun...  2 minutes ago  Up 2 minutes ago  0.0.0.0:8080->80/tcp  wptest-web          4ae6b452365b  wpapp_pod
870e4dd0c22b  docker.io/library/mariadb:latest    mysqld                2 minutes ago  Up 2 minutes ago  0.0.0.0:8080->80/tcp  wptest-db           4ae6b452365b  wpapp_pod
916ffdfda18b  k8s.gcr.io/pause:3.2                                      2 minutes ago  Up 2 minutes ago  0.0.0.0:8080->80/tcp  4ae6b452365b-infra  4ae6b452365b  wpapp_pod

Export Pod definition with podman generate

We can output our pod as a YAML definition by using podman generate kube command:

[cloud_user@5ecaa360f91c ~]$ sudo podman generate kube wpapp_pod >> wpapp_pod.yaml
[sudo] password for cloud_user: 
[cloud_user@5ecaa360f91c ~]$ cat wpapp_pod.yaml 
# 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.1
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2021-03-25T10:38:07Z"
  labels:
    app: wpapppod
  name: wpapp_pod
spec:
  containers:
  - command:
    - mysqld
    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: GOSU_VERSION
      value: "1.12"
    - name: GPG_KEYS
      value: 177F4010FE56CA3336300305F1656F24C74CD1D8
    - name: MARIADB_MAJOR
      value: "10.5"
    - name: MYSQL_ROOT_PASSWORD
      value: myrootpass
    - name: MYSQL_DATABASE
      value: wp-db
    - name: MARIADB_VERSION
      value: 1:10.5.9+maria~focal
    - name: MYSQL_USER
      value: wp-user
    - name: MYSQL_PASSWORD
      value: w0rdpr3ss
    - name: HOSTNAME
      value: wpapp_pod
    image: docker.io/library/mariadb:latest
    name: wptest-db
    ports:
    - containerPort: 80
      hostPort: 8080
      protocol: TCP
    resources: {}
    securityContext:
      allowPrivilegeEscalation: true
      capabilities: {}
      privileged: false
      readOnlyRootFilesystem: false
      seLinuxOptions: {}
    workingDir: /
  - command:
    - apache2-foreground
    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: PHP_SHA256
      value: 1c16cefaf88ded4c92eed6a8a41eb682bb2ef42429deb55f1c4ba159053fb98b
    - name: APACHE_CONFDIR
      value: /etc/apache2
    - name: PHP_CPPFLAGS
      value: -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
    - name: PHP_ASC_URL
      value: https://www.php.net/distributions/php-7.4.16.tar.xz.asc
    - name: WORDPRESS_DB_NAME
      value: wp-db
    - name: WORDPRESS_DB_PASSWORD
      value: w0rdpr3ss
    - name: PHP_LDFLAGS
      value: -Wl,-O1 -pie
    - name: GPG_KEYS
      value: 42670A7FE4D0441C8E4632349E4FDC074A4EF02D 5A52880781F755608BF815FC910DEB46F53EA312
    - name: PHPIZE_DEPS
      value: "autoconf \t\tdpkg-dev \t\tfile \t\tg++ \t\tgcc \t\tlibc-dev \t\tmake \t\tpkg-config \t\tre2c"
    - name: PHP_EXTRA_CONFIGURE_ARGS
      value: --with-apxs2 --disable-cgi
    - name: PHP_EXTRA_BUILD_DEPS
      value: apache2-dev
    - name: PHP_VERSION
      value: 7.4.16
    - name: WORDPRESS_DB_USER
      value: wp-user
    - name: WORDPRESS_DB_HOST
      value: 127.0.0.1
    - name: APACHE_ENVVARS
      value: /etc/apache2/envvars
    - name: PHP_INI_DIR
      value: /usr/local/etc/php
    - name: PHP_CFLAGS
      value: -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
    - name: PHP_URL
      value: https://www.php.net/distributions/php-7.4.16.tar.xz
    - name: HOSTNAME
      value: wpapp_pod
    image: docker.io/library/wordpress:latest
    name: wptest-web
    resources: {}
    securityContext:
      allowPrivilegeEscalation: true
      capabilities: {}
      privileged: false
      readOnlyRootFilesystem: false
      seLinuxOptions: {}
    workingDir: /var/www/html
  restartPolicy: Always
status: {}
---
metadata:
  creationTimestamp: null
spec: {}
status:
  loadBalancer: {}

This definition is similar to Kubernetes resource definition for pod.

Import Pod definition with podman play to create Pod

We can use podman play kube to start the pod from the defined YAML:

# verify that existing pod is already running
[cloud_user@5ecaa360f91c ~]$ sudo podman pod list
POD ID        NAME       STATUS   CREATED         INFRA ID      # OF CONTAINERS
4ae6b452365b  wpapp_pod  Running  11 minutes ago  916ffdfda18b  3


# stop pod wpapp_pod
[cloud_user@5ecaa360f91c ~]$ sudo podman pod stop wpapp_pod
4ae6b452365bf6875d0f286617ebd1c57ec60829f64fbaf970b74e85f8c8b927


# remove pod wpapp_pod
[cloud_user@5ecaa360f91c ~]$ sudo podman pod rm wpapp_pod
4ae6b452365bf6875d0f286617ebd1c57ec60829f64fbaf970b74e85f8c8b927


# verify pod is removed
[cloud_user@5ecaa360f91c ~]$ sudo podman pod list
POD ID  NAME    STATUS  CREATED  INFRA ID  # OF CONTAINERS


# import pod definition and create new pod
[cloud_user@5ecaa360f91c ~]$ sudo podman play kube ./wpapp_pod.yaml 
Trying to pull docker.io/library/mariadb:latest...
Getting image source signatures
Copying blob 2022ea4dab77 skipped: already exists  
Copying blob 3fc2062ea667 skipped: already exists  
Copying blob 5d3b2c2d21bb skipped: already exists  
Copying blob 62aa2722e098 skipped: already exists  
Copying blob 75adf526d75b skipped: already exists  
Copying blob 756d25563a9f skipped: already exists  
Copying blob 0ab4098b0f7c skipped: already exists  
Copying blob d03413915fdf skipped: already exists  
Copying blob fb8e671b1408 skipped: already exists  
Copying blob 5e2452f3fb5c skipped: already exists  
Copying blob c2da3a6fe532 skipped: already exists  
Copying blob 153b7df268e7 [--------------------------------------] 0.0b / 0.0b
Copying config e27cf5bc24 done  
Writing manifest to image destination
Storing signatures
Trying to pull docker.io/library/wordpress:latest...
Getting image source signatures
Copying blob 3bb74037bf77 skipped: already exists  
Copying blob ffae70ea03a9 skipped: already exists  
Copying blob 1e8027612378 skipped: already exists  
Copying blob 3ec32e53dce5 skipped: already exists  
Copying blob feda0fbd85b1 skipped: already exists  
Copying blob b2244185b327 skipped: already exists  
Copying blob f262da4e7afa skipped: already exists  
Copying blob 985e21deb66e skipped: already exists  
Copying blob 157f3d683e13 skipped: already exists  
Copying blob 990684a56233 skipped: already exists  
Copying blob 6f28985ad184 skipped: already exists  
Copying blob db883aae18bc skipped: already exists  
Copying blob 8852ae668073 skipped: already exists  
Copying blob c80999e49328 skipped: already exists  
Copying blob 02585da80b89 skipped: already exists  
Copying blob d68ab7635a0a skipped: already exists  
Copying blob 5a577fb48682 skipped: already exists  
Copying blob d27e8a2c96b8 skipped: already exists  
Copying blob f94fa08d2764 [--------------------------------------] 0.0b / 0.0b
Copying blob 70085d81b07c [--------------------------------------] 0.0b / 0.0b
Copying blob b75ce9c16911 [--------------------------------------] 0.0b / 0.0b
Copying config 6f6ff186d9 done  
Writing manifest to image destination
Storing signatures
Pod:
8ae52547df5616ead3cb9732b84b3e141e85ff822500f1bb9c68fcae80f20a86
Containers:
599cf32ce684eb8ac59813625bec250ea416c0faa653df1b0b457cfb97934b23
8587152860ef1a8d39c655f564d9d8bd8465d416dfad63bbc47989e4d3c14d8c

# verify pod is created and running now
[cloud_user@5ecaa360f91c ~]$ sudo podman pod list
POD ID        NAME       STATUS   CREATED         INFRA ID      # OF CONTAINERS
8ae52547df56  wpapp_pod  Running  20 seconds ago  72a404021f72  3


# verify app containers are available and running 
[cloud_user@5ecaa360f91c ~]$ sudo podman ps -a --pod
CONTAINER ID  IMAGE                               COMMAND               CREATED         STATUS             PORTS                 NAMES                 POD ID        PODNAME
8587152860ef  docker.io/library/wordpress:latest  apache2-foregroun...  18 seconds ago  Up 16 seconds ago  0.0.0.0:8080->80/tcp  wpapp_pod-wptest-web  8ae52547df56  wpapp_pod
599cf32ce684  docker.io/library/mariadb:latest    mysqld                24 seconds ago  Up 17 seconds ago  0.0.0.0:8080->80/tcp  wpapp_pod-wptest-db   8ae52547df56  wpapp_pod
72a404021f72  k8s.gcr.io/pause:3.2                                      29 seconds ago  Up 17 seconds ago  0.0.0.0:8080->80/tcp  8ae52547df56-infra    8ae52547df56  wpapp_pod
[cloud_user@5ecaa360f91c ~]$ 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s