Podman Hello-World container to Kubernetes Hello-World Pod
In this blog, we will perform the following activities:
- Create a HelloWorld Containerfile
- Build an image from the Containerfile using Podman (helloworldimage)
- Now create a container using Podman and the image you built. (hellocontainer)
- Now use the ‘podman kube generate’ command to create a yaml file (hellopod.yaml) from hellocontainer.
- Create a pod using kubectl and hellopod.yaml file.
Let's first create a Dockerfile or Containerfile that pulls an OracleLinux 8 image and then echos “Hello_World”
Step 1:
Create a Containerfile:
vi Containerfile
FROM os/oraclelinux:8
MAINTAINER Puneeth
CMD [“echo”, “Hello World”]
OR
FROM alpine
MAINTAINER Puneeth
CMD [“echo”, “Hello World”]
Step 2:
Build an image:
If your Dockerfile/Containerfile is in the current working directory:
podman build -t helloworldimage:v1 .
If you have created a Containerfile/Dockerfile with a different name say, HelloWorldfile, and if it is on a different location, then use the following command:
podman build -t helloworldimage:v1 -f /home/opc/lab/Hello-World/HelloWorldfile
Step 3:
Check if the image is built successfully:
podman images
Step 4:
Let's start a container using this image:
podman run -d --name hellocontainer localhost/helloworldimage:v1
Step 5:
Use Podman command to generate a kubernetes yaml file that can be used to create a pod:
podman generate kube <container-id> -f=hellopod.yaml
Step 6:
Let’s create a pod using kubectl and the yaml file generated by podman:
kubectl create -f hellopod.yaml
Known Issue 1:
If you are using an older version of podman, say:
[opc@control Hello-World]$ podman version
Client: Podman Engine
Version: 4.0.2
API Version: 4.0.2
Go Version: go1.17.7
Then the yaml file generated will have an “_” for Pod name, which is not allowed according to RFC 1123.
The Pod “hellocontainer_pod” is invalid: metadata.name: Invalid value: “hellocontainer_pod”: a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, ‘-’ or ‘.’, and must start and end with an alphanumeric character (e.g. ‘example.com’, regex used for validation is ‘[a-z0–9]([-a-z0–9]*[a-z0–9])?(\.[a-z0–9]([-a-z0–9]*[a-z0–9])?)*’)
You can manually edit the yaml file generated and change the pod name from say “hellocontainer_pod” to “hellocontainer-pod” and then create the pod using kubectl command.
OR
- Upgrade podman to the latest version:
[opc@control Hello-World]$ sudo yum update podman
[opc@control Hello-World]$ podman version
Client: Podman Engine
Version: 4.6.1
API Version: 4.6.1
Go Version: go1.20.10
Built: Wed Dec 13 02:31:03 2023
OS/Arch: linux/amd64
Now generate the yaml file and you should have a valid pod name in the yaml file.
Known Issue 2:
You may see an issue while trying to create a pod using the image you build locally using podman:
[opc@control Hello-World]$ kubectl get po
NAME READY STATUS RESTARTS AGE
[opc@control Hello-World]$
[opc@control Hello-World]$ kubectl create -f hellopod.yaml
pod/hellocontainer-pod created
[opc@control Hello-World]$
[opc@control Hello-World]$ kubectl get po
NAME READY STATUS RESTARTS AGE
hellocontainer-pod 0/1 ErrImagePull 0 118s
[opc@control Hello-World]$ kubectl describe po hellocontainer-pod
...
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m9s default-scheduler Successfully assigned default/hellocontainer-pod to worker1.us.oracle.com
Normal Pulling 35s (x4 over 2m8s) kubelet Pulling image "localhost/helloworldimage:v1"
Warning Failed 35s (x4 over 2m8s) kubelet Failed to pull image "localhost/helloworldimage:v1": rpc error: code = Unknown desc = pinging container registry localhost: Get "http://localhost/v2/": dial tcp [::1]:80: connect: connection refused
Warning Failed 35s (x4 over 2m8s) kubelet Error: ErrImagePull
Warning Failed 24s (x6 over 2m8s) kubelet Error: ImagePullBackOff
Normal BackOff 11s (x7 over 2m8s) kubelet Back-off pulling image "localhost/helloworldimage:v1"
podman is rootless and is not tied to a registry.
K8s cluster cannot talk to local machine. To workaround this issue you can tag and push the image to a registry where k8s cluster can pull from.
podman tag 00512f95bb0f ghcr.io/thelearnloop/exploringkubernetes/helloworldimage
podman push ghcr.io/thelearnloop/exploringkubernetes/helloworldimage
Now edit the hellopod.yaml file to use this new image name :
Then create the pod using kubectl command.