Chapter 13. Porting containers to OpenShift using Podman
You can generate portable descriptions of containers and pods by using the YAML ("YAML Ain’t Markup Language") format. The YAML is a text format used to describe the configuration data.
The YAML files are:
- Readable.
- Easy to generate.
- Portable between environments (for example between RHEL and OpenShift).
- Portable between programming languages.
- Convenient to use (no need to add all the parameters to the command line).
Reasons to use YAML files:
- You can re-run a local orchestrated set of containers and pods with minimal input required which can be useful for iterative development.
-
You can run the same containers and pods on another machine. For example, to run an application in an OpenShift environment and to ensure that the application is working correctly. You can use
podman generate kubecommand to generate a Kubernetes YAML file. Then, you can usepodman playcommand to test the creation of pods and containers on your local system before you transfer the generated YAML files to the Kubernetes or OpenShift environment. Using thepodman playcommand, you can also recreate pods and containers originally created in OpenShift or Kubernetes environments.
The podman kube play command supports a subset of Kubernetes YAML capabilities. For more information, see the support matrix of supported YAML fields.
13.1. Generating a Kubernetes YAML file using Podman Copy linkLink copied to clipboard!
You can create a pod with one container and generate the Kubernetes YAML file using the podman generate kube command.
Prerequisites
-
The
container-toolsmodule is installed. - The pod has been created. For details, see section Creating pods.
Procedure
List all pods and containers associated with them:
podman ps -a --pod
$ podman ps -a --pod CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES POD 5df5c48fea87 registry.access.redhat.com/ubi8/ubi:latest /bin/bash Less than a second ago Up Less than a second ago myubi 223df6b390b4 3afdcd93de3e k8s.gcr.io/pause:3.1 Less than a second ago Up Less than a second ago 223df6b390b4-infra 223df6b390b4Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the pod name or ID to generate the Kubernetes YAML file:
podman generate kube mypod > mypod.yaml
$ podman generate kube mypod > mypod.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Note that the
podman generatecommand does not reflect any Logical Volume Manager (LVM) logical volumes or physical volumes that might be attached to the container.Display the
mypod.yamlfile:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
13.2. Generating a Kubernetes YAML file in OpenShift environment Copy linkLink copied to clipboard!
In the OpenShift environment, use the oc create command to generate the YAML files describing your application.
Procedure
Generate the YAML file for your
myappapplication:oc create myapp --image=me/myapp:v1 -o yaml --dry-run > myapp.yaml
$ oc create myapp --image=me/myapp:v1 -o yaml --dry-run > myapp.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow The
oc createcommand creates and run themyappimage. The object is printed using the--dry-runoption and redirected into themyapp.yamloutput file.
In the Kubernetes environment, you can use the kubectl create command with the same flags.
13.3. Starting containers and pods with Podman Copy linkLink copied to clipboard!
With the generated YAML files, you can automatically start containers and pods in any environment. The YAML files can be generated using tools other than Podman, such as Kubernetes or Openshift. The podman play kube command allows you to recreate pods and containers based on the YAML input file.
Prerequisites
-
The
container-toolsmodule is installed.
Procedure
Create the pod and the container from the
mypod.yamlfile:podman play kube mypod.yaml
$ podman play kube mypod.yaml Pod: b8c5b99ba846ccff76c3ef257e5761c2d8a5ca4d7ffa3880531aec79c0dacb22 Container: 848179395ebd33dd91d14ffbde7ae273158d9695a081468f487af4e356888eceCopy to Clipboard Copied! Toggle word wrap Toggle overflow List all pods:
podman pod ps
$ podman pod ps POD ID NAME STATUS CREATED # OF CONTAINERS INFRA ID b8c5b99ba846 mypod Running 19 seconds ago 2 aa4220eaf4bbCopy to Clipboard Copied! Toggle word wrap Toggle overflow List all pods and containers associated with them:
podman ps -a --pod
$ podman ps -a --pod CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES POD 848179395ebd registry.access.redhat.com/ubi8/ubi:latest /bin/bash About a minute ago Up About a minute ago myubi b8c5b99ba846 aa4220eaf4bb k8s.gcr.io/pause:3.1 About a minute ago Up About a minute ago b8c5b99ba846-infra b8c5b99ba846Copy to Clipboard Copied! Toggle word wrap Toggle overflow The pod IDs from
podman pscommand matches the pod ID from thepodman pod pscommand.
13.4. Starting containers and pods in OpenShift environment Copy linkLink copied to clipboard!
You can use the oc create command to create pods and containers in the OpenShift environment.
Procedure
Create a pod from the YAML file in the OpenShift environment:
oc create -f mypod.yaml
$ oc create -f mypod.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
In the Kubernetes environment, you can use the kubectl create command with the same flags.
13.5. Manually running containers and pods using Podman Copy linkLink copied to clipboard!
The following procedure shows how to manually create a WordPress content management system paired with a MariaDB database using Podman.
Suppose the following directory layout:
├── mariadb-conf │ ├── Containerfile │ ├── my.cnf
├── mariadb-conf
│ ├── Containerfile
│ ├── my.cnf
Prerequisites
-
The
container-toolsmodule is installed.
Procedure
Display the
mariadb-conf/Containerfilefile:cat mariadb-conf/Containerfile FROM docker.io/library/mariadb COPY my.cnf /etc/mysql/my.cnf
$ cat mariadb-conf/Containerfile FROM docker.io/library/mariadb COPY my.cnf /etc/mysql/my.cnfCopy to Clipboard Copied! Toggle word wrap Toggle overflow Display the
mariadb-conf/my.cnffile:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Build the
docker.io/library/mariadbimage usingmariadb-conf/Containerfile:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: List all images:
podman images
$ podman images LIST IMAGES REPOSITORY TAG IMAGE ID CREATED SIZE localhost/mariadb-conf latest b66fa0fa0ef2 57 seconds ago 416 MBCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create the pod named
wordpresspodand configure port mappings between the container and the host system:podman pod create --name wordpresspod -p 8080:80
$ podman pod create --name wordpresspod -p 8080:80Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the
mydbcontainer inside thewordpresspodpod:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the
mywebcontainer inside thewordpresspodpod:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: List all pods and containers associated with them:
podman ps --pod -a
$ podman ps --pod -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES POD ID PODNAME 9ea56f771915 k8s.gcr.io/pause:3.5 Less than a second ago Up Less than a second ago 0.0.0.0:8080->80/tcp 4b7f054a6f01-infra 4b7f054a6f01 wordpresspod 60e8dbbabac5 localhost/mariadb-conf:latest mariadbd Less than a second ago Up Less than a second ago 0.0.0.0:8080->80/tcp mydb 4b7f054a6f01 wordpresspod 045d3d506e50 docker.io/library/wordpress:latest apache2-foregroun... Less than a second ago Up Less than a second ago 0.0.0.0:8080->80/tcp myweb 4b7f054a6f01 wordpresspodCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Verify that the pod is running: Visit the http://localhost:8080/wp-admin/install.php page or use the
curlcommand:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
13.6. Generating a YAML file using Podman Copy linkLink copied to clipboard!
You can generate a Kubernetes YAML file using the podman generate kube command.
Prerequisites
-
The
container-toolsmodule is installed. -
The pod named
wordpresspodhas been created. For details, see section Creating pods.
Procedure
List all pods and containers associated with them:
podman ps --pod -a
$ podman ps --pod -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES POD ID PODNAME 9ea56f771915 k8s.gcr.io/pause:3.5 Less than a second ago Up Less than a second ago 0.0.0.0:8080->80/tcp 4b7f054a6f01-infra 4b7f054a6f01 wordpresspod 60e8dbbabac5 localhost/mariadb-conf:latest mariadbd Less than a second ago Up Less than a second ago 0.0.0.0:8080->80/tcp mydb 4b7f054a6f01 wordpresspod 045d3d506e50 docker.io/library/wordpress:latest apache2-foregroun... Less than a second ago Up Less than a second ago 0.0.0.0:8080->80/tcp myweb 4b7f054a6f01 wordpresspodCopy to Clipboard Copied! Toggle word wrap Toggle overflow Use the pod name or ID to generate the Kubernetes YAML file:
podman generate kube wordpresspod >> wordpresspod.yaml
$ podman generate kube wordpresspod >> wordpresspod.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Display the
wordpresspod.yamlfile:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
13.7. Automatically running containers and pods using Podman Copy linkLink copied to clipboard!
You can use the podman play kube command to test the creation of pods and containers on your local system before you transfer the generated YAML files to the Kubernetes or OpenShift environment.
The podman play kube command can also automatically build and run multiple pods with multiple containers in the pod using the YAML file similarly to the docker compose command. The images are automatically built if the following conditions are met:
- a directory with the same name as the image used in YAML file exists
- that directory contains a Containerfile
Prerequisites
-
The
container-toolsmodule is installed. -
The pod named
wordpresspodhas been created. For details, see section Manually running containers and pods using Podman. - The YAML file has been generated. For details, see section Generating a YAML file using Podman.
To repeat the whole scenario from the beginning, delete locally stored images:
podman rmi localhost/mariadb-conf podman rmi docker.io/library/wordpress podman rmi docker.io/library/mysql
$ podman rmi localhost/mariadb-conf $ podman rmi docker.io/library/wordpress $ podman rmi docker.io/library/mysqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Procedure
Create the wordpress pod using the
wordpress.yamlfile:Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
podman play kubecommand:-
Automatically build the
localhost/mariadb-conf:latestimage based ondocker.io/library/mariadbimage. -
Pull the
docker.io/library/wordpress:latestimage. -
Create a pod named
wordpresspodwith two containers namedwordpresspod-mydbandwordpresspod-myweb.
-
Automatically build the
List all containers and pods:
podman ps --pod -a
$ podman ps --pod -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES POD ID PODNAME a1dbf7b5606c k8s.gcr.io/pause:3.5 3 minutes ago Up 2 minutes ago 0.0.0.0:8080->80/tcp 3e391d091d19-infra 3e391d091d19 wordpresspod 6c59ebe96846 localhost/mariadb-conf:latest mariadbd 2 minutes ago Exited (1) 2 minutes ago 0.0.0.0:8080->80/tcp wordpresspod-mydb 3e391d091d19 wordpresspod 29717878452f docker.io/library/wordpress:latest apache2-foregroun... 2 minutes ago Up 2 minutes ago 0.0.0.0:8080->80/tcp wordpresspod-myweb 3e391d091d19 wordpresspodCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Verify that the pod is running: Visit the http://localhost:8080/wp-admin/install.php page or use the
curlcommand:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
13.8. Automatically stopping and removing pods using Podman Copy linkLink copied to clipboard!
The podman play kube --down command stops and removes all pods and their containers.
If a volume is in use, it is not removed.
Prerequisites
-
The
container-toolsmodule is installed. -
The pod named
wordpresspodhas been created. For details, see section Manually running containers and pods using Podman. - The YAML file has been generated. For details, see section Generating a YAML file using Podman.
- The pod is running. For details, see section Automatically running containers and pods using Podman.
Procedure
Remove all pods and containers created by the
wordpresspod.yamlfile:podman play kube --down wordpresspod.yaml
$ podman play kube --down wordpresspod.yaml Pods stopped: 3e391d091d190756e655219a34de55583eed3ef59470aadd214c1fc48cae92ac Pods removed: 3e391d091d190756e655219a34de55583eed3ef59470aadd214c1fc48cae92acCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Verify that all pods and containers created by the
wordpresspod.yamlfile were removed:podman ps --pod -a
$ podman ps --pod -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES POD ID PODNAMECopy to Clipboard Copied! Toggle word wrap Toggle overflow