Compose and Kubernetes YAML

Multi-service projects with podman compose, generating and replaying Kubernetes YAML with kube generate, kube play and kube down, choosing between Compose, kube play and Quadlet, and a migration checklist.
PodmanComposeKubernetes YAMLPodsCore hour

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 down keeps and what down -v removes
  • generate a pod's Kubernetes manifest, read its main fields and replay it with kube play
  • remove what kube play created with kube down
  • choose between Compose, kube play and 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

Hour 6 · 00–10 · 10 min

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.

The project name defaults to the folder name, podman-app. Containers and volumes are prefixed with it, so two projects in two folders do not collide.

Common errors.

  • no such command or no provider: install a provider, as described in the prerequisite.
  • port is already allocated: another container or process uses 8080. Change the host port in compose.yaml, then run up again.

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

Hour 6 · 10–22 · 12 min

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' from probe: the service is not running. Check podman 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

Hour 6 · 22–32 · 10 min

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.

A generated manifest reflects the running pod at the moment you generate it. It is a starting point. Review it, remove fields that come from the run (such as generated names or host-specific settings), and keep it in version control.

Common errors.

  • kube generate prints nothing: the pod name is wrong. Check podman pod ps.
  • The manifest uses a local image name that a cluster cannot pull: push the image to a registry and change the image field 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

Hour 6 · 32–44 · 12 min

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 play fails 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 inspect and 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

Hour 6 · 44–52 · 8 min

The three tools solve different jobs. Choose by where the service runs and who maintains it:

NeedUseWhy
A developer runs several services on a laptoppodman composeshort, readable, and the networks and volumes are set up for you
A test host should resemble a Kubernetes podpodman kube playthe same manifest later goes to a cluster
A Linux server runs one service that must start at bootQuadlet (hour 7)systemd starts, restarts and logs it
A cluster runs the workloadKubernetes, with the manifest from kube generate as the starting pointthe 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.

Do not run the same service under two tools at once. Two managers that each start a container with the same name create confusion during an incident.

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

Hour 6 · 52–60 · 8 min

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:

ItemIn compose.yamlIn a Kubernetes manifestCheck
Imageimage or buildimage with a registry namepushed to a registry and pinned by digest
Configurationenvironmentenv, or a ConfigMapno secrets in plain values
Secretssecrets or env_fileSecret objectsvalues not written in the file
Portsportsports in the pod specpublished on the intended address only
Storagevolumesa volume or claimdata class and backup owner recorded
Healthhealthcheckliveness or readiness probechecks a real route
Stop timestop_grace_periodterminationGracePeriodSecondsenough time to drain requests
Privilegecap_drop, read_onlysecurityContextno 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.

A checklist is most useful when it is in the pull request template, so every migration is measured against the same list.

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

Hour 6 ·
TaskCommand
Check the Compose providerpodman compose version
Show the resolved projectpodman compose config
Build and start in the backgroundpodman compose up -d --build
List the project's containerspodman compose ps
Logs of one servicepodman compose logs web
Run a command in a servicepodman compose exec web sh
Stop and remove, keep volumespodman compose down
Stop and remove, delete volumespodman compose down -v
Generate a pod manifestpodman kube generate POD > file.yaml
Play a manifestpodman kube play file.yaml
Replay, replacing an earlier runpodman kube play --replace file.yaml
Remove what a manifest createdpodman kube down file.yaml
Show a pod's published portspodman pod inspect POD

Capstone lab: the same service under Compose and kube play

Hour 6 ·

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.

Final quiz

Hour 6

Sources and version notes