Compose and Kubernetes YAML
Hour overview
Real applications have several services. In this hour you run the two-service project in podman-app/compose.yaml with podman compose, then turn a pod into a Kubernetes manifest, replay it with kube play, and tear it down with kube down. You finish with a decision table and a migration checklist, so you know which format to use for which job.
Time allocation. Compose basics 10, multi-service project 12, generate Kubernetes YAML 10, play and tear down 12, choosing the format 8, migration checklist 8.
Objectives. By the end of the hour you can:
- check which Compose provider Podman uses, and run a project with
podman compose - call one service from another by its service name
- explain what
compose downkeeps and whatdown -vremoves - generate a pod's Kubernetes manifest, read its main fields and replay it with
kube play - remove what
kube playcreated withkube down - choose between Compose,
kube playand Quadlet for a given job
Prerequisite. A Compose provider. Run podman compose version. If it reports no provider, install the docker-compose v2 binary or podman-compose, then run it again.
Modules
podman compose
Compose describes several services in one YAML file. Podman does not implement the whole Compose specification itself. podman compose passes the file to an external provider, which can be docker-compose or podman-compose. Check which one runs:
cd podman-app
podman compose version
podman compose config
Expected: version prints the provider's version. config prints the project as Compose resolves it, with defaults filled in. Read it once. It shows the image, the ports, the environment and the volume for web, and the service and volume for probe.
Build and start the project in the background:
podman compose up -d --build
podman compose ps
Expected: ps lists web and probe with status running. web shows the port mapping from 127.0.0.1:8080 to 8000.
Check the service:
curl -s http://127.0.0.1:8080/health
curl -s http://127.0.0.1:8080/
Expected: {"status": "ok"}, then {"message": "hello from compose", "visits": 1}. The greeting comes from the environment section of compose.yaml, which overrides the image default.
podman-app. Containers and volumes are prefixed with it, so two projects in two folders do not collide.Common errors.
no such commandor no provider: install a provider, as described in the prerequisite.port is already allocated: another container or process uses 8080. Change the host port incompose.yaml, then runupagain.
Production note. Keep compose.yaml in version control with the code. Pin images by digest in the file before you use it outside a laptop.
A multi-service project
In the project, probe is a second container on the same Compose network. It calls web by the service name, which is the same DNS rule you saw in hour 5:
podman compose exec probe wget -qO- http://web:8000/health
podman volume ls
Expected: the wget prints {"status": "ok"}. volume ls lists the project's named volume, appdata, with the project prefix in its full name.
Check that the data survives a restart of the project:
curl -s http://127.0.0.1:8080/
podman compose down
podman compose up -d
curl -s http://127.0.0.1:8080/
Expected: the first curl prints visits: 2, because the earlier call in m1 counted one. After down and up, the next curl prints visits: 3. The volume survived.
What down keeps. By default down removes the containers and the network, and keeps named volumes. podman compose down -v also removes the volumes, so the count starts again. Check your provider's documentation for the exact behaviour in your version before you rely on it.
down -v deletes the data in named volumes. Run it only on a disposable project, and never on a project whose volumes hold data you have not backed up.Logs and exec. Read the logs of one service, and run a command in a running one:
podman compose logs web
podman compose exec web sh -c 'cat /data/visits.txt'
Expected: logs shows the access lines from web. exec prints the counter, the same number the last curl returned.
Common errors.
wget: bad address 'web:8000'fromprobe: the service is not running. Checkpodman compose ps.- The count restarts from 1: the volume was removed with
down -v, or the project name changed, which creates a new volume.
Production note. Use Compose for a single host, development and testing. Add health checks and depends_on ordering only with a clear purpose, because depends_on waits for start-up, not for readiness.
Generate Kubernetes YAML
Podman can write a pod as a Kubernetes manifest. That is useful when you want to run the same service on a cluster later, or to replay it with kube play on a host. Build a pod first, then generate its manifest:
podman pod create --name kubedemo -p 127.0.0.1:8015:8000
podman run -d --pod kubedemo --name kubeapp -e GREETING="from kube" localhost/podman-app:dev
podman kube generate kubedemo > kubedemo.yaml
grep -n "kind:" kubedemo.yaml
Expected: kubedemo.yaml is created. grep shows kind: Pod, and the container's image is localhost/podman-app:dev.
Read the main fields. A generated pod manifest has the same shape as any Kubernetes pod:
apiVersion: v1
kind: Pod
metadata:
name: kubedemo
spec:
containers:
- name: kubeapp
image: localhost/podman-app:dev
env: ...
ports: ...
Check the environment variable, which should carry GREETING with the value you set:
grep -n "GREETING" kubedemo.yaml
Expected: one line with the variable name and its value.
Common errors.
kube generateprints nothing: the pod name is wrong. Checkpodman pod ps.- The manifest uses a local image name that a cluster cannot pull: push the image to a registry and change the
imagefield before you use the manifest on a cluster.
Production note. Generate the manifest from a known-good pod, review it, and version it. Do not let a manifest drift from the running system without a review.
Play and tear down Kubernetes YAML
kube play creates the pods, containers and volumes that a manifest describes, and starts them. kube down removes what a manifest created. Start from a clean state, then replay the manifest:
podman pod rm -f kubedemo
podman kube play kubedemo.yaml
podman pod ps
podman ps --pod
Expected: pod ps lists kubedemo, created from the manifest. ps --pod shows the containers in it.
Call the service. The manifest keeps the published port, so the health route answers on the same address:
curl -s http://127.0.0.1:8015/health
Expected: {"status": "ok"}. If the connection is refused, the manifest did not keep the host port. Read it with podman pod inspect kubedemo to see the published ports, and fix the manifest.
Remove what the manifest created:
podman kube down kubedemo.yaml
podman pod ps
Expected: kube down stops and removes the pod, and pod ps no longer lists it.
Replace in one step. To recreate the pod from the manifest, use --replace. It removes an earlier run of the same manifest, then plays it again:
podman kube play --replace kubedemo.yaml
podman pod ps
podman kube down kubedemo.yaml
kube play supports a subset of the Kubernetes API. A manifest that uses a field Podman does not support fails or is only partly applied. Read the output of kube play, and check the result with podman pod ps and podman ps --pod.Common errors.
kube playfails on a field: the field is not supported by Podman. Remove it, or use the field's Podman equivalent, and record the change.- The pod is up but the port is missing: check
podman pod inspectand fix the manifest's port section.
Production note. Keep the manifest, the image digest and the environment file in one repository. Replay in a test environment before you use the same manifest anywhere else.
Choose Compose, kube play or Quadlet
The three tools solve different jobs. Choose by where the service runs and who maintains it:
| Need | Use | Why |
|---|---|---|
| A developer runs several services on a laptop | podman compose | short, readable, and the networks and volumes are set up for you |
| A test host should resemble a Kubernetes pod | podman kube play | the same manifest later goes to a cluster |
| A Linux server runs one service that must start at boot | Quadlet (hour 7) | systemd starts, restarts and logs it |
| A cluster runs the workload | Kubernetes, with the manifest from kube generate as the starting point | the cluster provides scheduling, scaling and service discovery |
Decide on three questions: does the service need to survive a reboot, does it run on more than one host, and does it need a cluster's scheduling? If the answer to all three is no, Compose is usually enough.
Production note. Write the choice into the runbook, with the reason. The next engineer then knows which file to change, and which tool to use to change it.
Migration checklist
When you move a Compose project to a manifest or to Quadlet, the parts that change are the ones that depend on the host. Check each item:
| Item | In compose.yaml | In a Kubernetes manifest | Check |
|---|---|---|---|
| Image | image or build | image with a registry name | pushed to a registry and pinned by digest |
| Configuration | environment | env, or a ConfigMap | no secrets in plain values |
| Secrets | secrets or env_file | Secret objects | values not written in the file |
| Ports | ports | ports in the pod spec | published on the intended address only |
| Storage | volumes | a volume or claim | data class and backup owner recorded |
| Health | healthcheck | liveness or readiness probe | checks a real route |
| Stop time | stop_grace_period | terminationGracePeriodSeconds | enough time to drain requests |
| Privilege | cap_drop, read_only | securityContext | no privileged mode |
Compare the sample's files against this list. The sample has an image built from Containerfile, one published port on 127.0.0.1, and a named volume. It does not have a health check or a pinned image yet. Write down the change each missing item needs, and make it before you move the service.
Production note. Migrate one service at a time, keep the old definition until the new one passes its health check, and record the rollback step.
Cheat sheet
| Task | Command |
|---|---|
| Check the Compose provider | podman compose version |
| Show the resolved project | podman compose config |
| Build and start in the background | podman compose up -d --build |
| List the project's containers | podman compose ps |
| Logs of one service | podman compose logs web |
| Run a command in a service | podman compose exec web sh |
| Stop and remove, keep volumes | podman compose down |
| Stop and remove, delete volumes | podman compose down -v |
| Generate a pod manifest | podman kube generate POD > file.yaml |
| Play a manifest | podman kube play file.yaml |
| Replay, replacing an earlier run | podman kube play --replace file.yaml |
| Remove what a manifest created | podman kube down file.yaml |
| Show a pod's published ports | podman pod inspect POD |
Capstone lab: the same service under Compose and kube play
Goal. Run the same pod under two tools, check both, and tear both down.
cd podman-app
podman compose up -d --build
curl -s http://127.0.0.1:8080/health
podman compose down
podman pod create --name capstone -p 127.0.0.1:8016:8000
podman run -d --pod capstone --name capstone-app -e GREETING="from kube" localhost/podman-app:dev
podman kube generate capstone > capstone.yaml
podman pod rm -f capstone
podman kube play capstone.yaml
curl -s http://127.0.0.1:8016/health
podman kube down capstone.yaml
Expected. Both curl calls print {"status": "ok"}. compose down and kube down each remove what their tool created.
Verify. podman pod ps and podman compose ps list nothing after the teardown.
Clean up. podman volume rm podman-app_appdata removes the Compose volume if you no longer need its data. The name depends on the project prefix, so check podman volume ls first. Remove capstone.yaml.