Sub topic 6
Describing containers in a Pod spec
This chapter assumes you can read basic YAML, know the four top-level parts of a manifest (apiVersion, kind, metadata, spec), and have a cluster with kubectl configured. It covers the part of a manifest that describes what actually runs: the Pod spec.
What a Pod spec is
A Pod is the smallest unit Kubernetes schedules. It holds one or more containers that share a network address and can share storage. You rarely create a bare Pod. Instead, a Deployment embeds a Pod description under spec.template, and that description has its own metadata and spec. The Pod spec is everything under template.spec.
The container list
The key field is containers, a list. Each entry needs at least a name and an image. Here is the Pod spec from the Deployment used in this tutorial:
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80The fields work like this:
nameidentifies the container inside the Pod. It must be unique within that Pod.imageis the container image to run. Pin a specific tag such asnginx:1.27. Thelatesttag can change under you, so the same manifest may run different code on different days.portslists the ports the container listens on.containerPortis mostly documentation. The container accepts traffic on a port whether or not you list it. Services and probes can refer to the port by name if you add anamefield.
Commands, arguments, and environment
An image already defines a default command. Override it only when you need to. In a manifest, command replaces the image's entrypoint and args replaces its default arguments.
containers:
- name: tool
image: busybox:1.36
command: ["sh", "-c"]
args: ["echo $GREETING && sleep 3600"]
env:
- name: GREETING
value: "hello"env sets environment variables. A later chapter shows how to fill them from a ConfigMap or Secret instead of hard-coding values.
Several containers in one Pod
A Pod can list more than one container. All of them start together, share localhost, and are scheduled onto the same node. Use this for tightly coupled helpers, such as a log shipper next to an application. Do not use it to put unrelated services together, because they would scale and restart as one unit.
Common errors
| Symptom | Likely cause |
|---|---|
| `ImagePullBackOff` | Wrong image name or tag, or a private registry without credentials |
| `CrashLoopBackOff` | The container starts and exits repeatedly. Check `kubectl logs` |
| `unknown field` on apply | A field is indented at the wrong level, for example `image` outside the list item |
| Command ignored | `command` and `args` were confused. `command` replaces the entrypoint, not just the arguments |
A frequent indentation mistake is putting containers at the same level as template instead of under template.spec. Use kubectl explain to confirm where a field belongs:
kubectl explain pod.spec.containers
kubectl explain pod.spec.containers.imageExercise
In a Deployment, a teammate wrote containers: directly under spec.template rather than under spec.template.spec. What happens, and how can you find the correct location?
Show answer
The API server rejects the manifest with an unknown field or validation error, because template only accepts metadata and spec. Move containers under template.spec. You can confirm the structure with kubectl explain deployment.spec.template.