Part 4 · Operations and troubleshooting · Lesson 03 · 45 minutes
Run several containers as one service
- Run two containers in one pod that share a network and a volume
- Describe the same service in a Kubernetes-style YAML file and start it with podman kube play
- Say in plain words what orchestration, a service mesh and bootstrapping are, and what you cannot test on one machine
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.
Scenario
A small web service has two parts: a web server and a helper that writes the page it serves. Your lead wants them to start, stop and move together, and wants the setup written in a file that goes into version control, not in a list of commands from someone's history. You build the service with commands first, then write it as a YAML file and start it from that file.
Recall
Your container wrote a file, you removed the container, and the file is gone. Where should the file have been written?
In a volume or a bind mount (lesson 2). The writable layer of a container is deleted with it.
Why does a rootless container publish only ports from 1024 up?
Ports below 1024 are privileged. Only root may bind them, and a rootless container runs as your own user.
The idea
A pod is a group of containers that run together on one machine and share things. In podman, the pod holds a small infra container (named <id>-infra) that owns the network namespace. All containers of the pod join it. So they share one IP address, and each reaches the others on localhost. Ports are published on the pod, not on a single container. The containers can also share a volume.
- You → Infra container: port 8090 is published on the pod
- Infra container → krlab-web: the shared network reaches web
- krlab-writer → krlab-pod-data: writes index.html
- krlab-web → krlab-pod-data: reads it, read-only
A file in the Kubernetes format describes the same pod. Kubernetes is the best-known system that runs containers on many machines, and podman reads the part of its format that describes one pod. The fields that you need:
| Field | Meaning |
|---|---|
kind: Pod | The type of object that the file describes |
metadata.name | The name of the pod |
spec.containers | A list: each item has name, image and its options |
ports with hostPort | The port that is published on the host |
volumeMounts | Where a volume appears inside one container |
spec.volumes | The volumes of the pod; emptyDir is a folder that lives as long as the pod |
Three words that you meet in job advertisements, and what to know about them:
- Orchestration is a system that runs many containers on many machines from files that say what you want (three copies of the web server, this image, this port). It starts the missing ones, restarts the failed ones and rolls out a new image step by step. Kubernetes is the common example.
- A service mesh is a layer of small proxy programs, one next to each service. A helper container that runs next to the main one, like
writerhere, is called a sidecar. The proxies encrypt the traffic between services, retry failed calls and log them, so the services do not need code for it. - Bootstrapping is starting a machine or a cluster from nothing: a first-boot tool such as
cloud-initreads a configuration and installs the first software and keys.
You cannot test these three on one machine with this course. They need several machines or a cloud. What you test here is the single-pod format, which they all build on.
Worked example
The output comes from the machine where this lesson was tested; ids and times differ on your machine.
1. Build the pod with commands
podman pod create --name NAME -p 8090:80 makes the pod and publishes its port 80 on the host port 8090. --pod NAME puts a container into it. The writer starts first and writes a page into the volume; the web server mounts the same volume read-only.
cd ~/krlab/linux-operations
podman volume create krlab-pod-data
podman pod create --name krlab-pod -p 8090:80
podman run -d --pod krlab-pod --name krlab-writer -v krlab-pod-data:/out docker.io/library/alpine:3.20 sh -c 'echo "<h1>shared volume</h1>" > /out/index.html; sleep 3600'
podman run -d --pod krlab-pod --name krlab-web -v krlab-pod-data:/usr/share/nginx/html:ro docker.io/library/nginx:alpine
sleep 2
podman ps --pod --format "{{.PodName}} {{.Names}}"krlab-pod ee403f473175-infra
krlab-pod krlab-writer
krlab-pod krlab-webThe pod has three containers: the infra container and your two.
2. Prove the shared network and the volume
The page comes from the web server on the host port. The writer container asks localhost, and it reaches the web server, because both share one network namespace. wget -qO- prints a web page to the screen.
curl -s http://127.0.0.1:8090/
podman exec krlab-writer wget -qO- http://localhost/<h1>shared volume</h1>
<h1>shared volume</h1>3. Let podman write the YAML
podman kube generate NAME -f FILE describes a running pod as a Kubernetes file. The file is long, so list only the lines that name the parts.
podman kube generate krlab-pod -f krlab-pod.yaml
grep -E '^kind|image:|hostPort|mountPath|claimName' krlab-pod.yamlkind: Pod
image: docker.io/library/alpine:3.20
hostPort: 8090
- mountPath: /out
image: docker.io/library/nginx:alpine
- mountPath: /usr/share/nginx/html
claimName: krlab-pod-dataEvery part of the commands is in the file. Remove the pod, with -f to force it and -t 0 to skip the ten-second wait for the sleeping writer, and remove the volume.
podman pod rm -f -t 0 krlab-pod
podman volume rm krlab-pod-data4. Write the file by hand
A generated file has a time stamp and names that you did not choose. A file that you keep in Git should be short. Save this one as krlab-site.yaml. It uses emptyDir, so no volume has to exist before the pod. The command line is a list of words: the program, its option, and the text for sh -c.
krlab-site.yaml
apiVersion: v1
kind: Pod
metadata:
name: krlab-site
spec:
containers:
- name: web
image: docker.io/library/nginx:alpine
ports:
- containerPort: 80
hostPort: 8091
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
readOnly: true
- name: writer
image: docker.io/library/alpine:3.20
command: ["sh", "-c", "echo '<h1>same service, from YAML</h1>' > /out/index.html; sleep 3600"]
volumeMounts:
- name: html
mountPath: /out
volumes:
- name: html
emptyDir: {}5. Start the service from the file
podman kube play FILE creates the pod and its containers from a file. podman kube down FILE stops and removes what the file made.
- You → Podman: kube play krlab-site.yaml
- Podman → Pod: create krlab-site with its infra container
- Podman → Pod: start the containers web and writer
- Pod → You: port 8091 answers with the page
- You → Podman: kube down krlab-site.yaml
- Podman → Pod: stop and remove everything
podman kube play krlab-site.yaml > /dev/null
sleep 3
podman ps --format "{{.Names}}"
curl -s http://127.0.0.1:8091/
podman kube down krlab-site.yaml > /dev/null
podman ps -a --format "{{.Names}}"c7ff7d8b3476-infra
krlab-site-web
krlab-site-writer
<h1>same service, from YAML</h1>The last command prints nothing: the pod is gone. The file is the whole description, so a colleague gets the same service with one command. Pod names are prefixed to the container names, so web became krlab-site-web.
Your turn
A colleague wrote krlab-pair.yaml. The page should say pair ok, but the web server shows its own welcome page. Save the file, start the pod from it, find the mistake with the tools of this lesson and lesson 2, repair the file and start the pod again. Stop the pod when you have finished.
krlab-pair.yaml
apiVersion: v1
kind: Pod
metadata:
name: krlab-pair
spec:
containers:
- name: web
image: docker.io/library/nginx:alpine
ports:
- containerPort: 80
hostPort: 8092
volumeMounts:
- name: pages
mountPath: /usr/share/nginx/htm
readOnly: true
- name: writer
image: docker.io/library/alpine:3.20
command: ["sh", "-c", "echo 'pair ok' > /out/index.html; sleep 3600"]
volumeMounts:
- name: pages
mountPath: /out
volumes:
- name: pages
emptyDir: {}Check yourself
Two containers of one pod both want to listen on port 80. What happens?
The second one fails. They share one network namespace, so they share the port numbers too. Give each a different port, or put them in different pods.
Why is a YAML file in Git better than a list of podman commands in a wiki?
The file is one exact description, and Git records who changed what and when. A reader can start the same service with one command, compare two versions with git diff and go back. A list of commands drifts from what really runs.
What does a service mesh add, and what does it cost?
It gives encrypted traffic between services, retries and logs without changes in the code. It costs a proxy for every service, more memory and CPU, and one more system to learn, operate and update.
You change the YAML file while the pod runs. Does the pod change?
No. A running pod does not read the file again. Run podman kube down and podman kube play again, or use podman kube play --replace. An orchestrator does this comparison for you.
Field notes
podman kube playsupports only part of the Kubernetes format. A file that works here can need changes on a real cluster, and the other way round. Test on the target.- Pin the images in a file that you keep: a version tag, or a digest (lesson 2). A bare tag such as
alpinecan change under you. - A service that must come back after a reboot needs a unit.
podman generate systemdis deprecated; currentpodmanruns a pod from a Quadlet file that systemd reads. A file~/.config/containers/systemd/krlab-site.kubewith the section[Kube]and the lineYaml=and the full path ofkrlab-site.yamlbecomes the unitkrlab-site.serviceaftersystemctl --user daemon-reload;systemctl --user start krlab-sitethen serves the page on port 8091. An[Install]section withWantedBy=default.targetandsudo loginctl enable-linger $USERstart it at boot. This path was not run for the course. - A pod is one failure unit. If the web server and the helper must scale on their own, they belong in separate pods.
- Do not put passwords in the YAML. Kubernetes has a
Secretobject, andpodmanreads one as well. A YAML file in Git lives for ever (lesson 4).