Inside a Pod
Shared network, shared files, and restarts.
cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/k8s-fund/pods.tar.gz && tar -xzf pods.tar.gz, which creates ~/lab/pods/. SHA-256: 3700c145dab65a5d3c77cf3a39c831a160f1fcea43f9f64770ac673e2ec19531The Pod in lesson 3 held a single container, and most Pods do. Sometimes, though, a web server needs a helper that keeps its files fresh, or an app must wait for a setup step before it starts. With plain Docker you would wire up a shared network and a volume between separate containers yourself. A Pod can hold several containers that already share both.
A Pod also keeps count of every time its containers stop and start again, which is how you spot an app that keeps crashing.
Two containers, one Pod
This Pod runs nginx and a small busybox container. They share an emptyDir volume: an empty folder created with the Pod and deleted with it. The writer container puts the current time into index.html every five seconds, and nginx serves that folder:
apiVersion: v1kind: Podmetadata:name: sharedspec:volumes:- name: htmlemptyDir: {}containers:- name: webimage: nginx:1.30.6-alpinevolumeMounts:- name: htmlmountPath: /usr/share/nginx/html- name: writerimage: busybox:1.38.0command: ["sh", "-c", "while true; do date > /out/index.html; sleep 5; done"]volumeMounts:- name: htmlmountPath: /out
With more than one container in a Pod, -c picks which one exec runs in. wget -qO- fetches a page and prints it, and hostname -i prints the container's IP address. -o jsonpath=... makes kubectl get print a single field, here the Pod's IP (lesson 5, kubectl basics, covers it):
READY 2/2 counts containers: both run. From the writer container, http://localhost reached nginx in the other container, because the containers of a Pod share one network. nginx served the date the writer wrote, because both mount the same volume. And both containers report the Pod's IP address.
Sharing a network also means two containers in one Pod cannot both listen on the same port. They are always scheduled together on one node and scaled together. Put containers in one Pod only when they really belong together, like this writer and its web server. A web app and its database belong in separate Pods.
Init containers
An init container runs to completion before the normal containers start. Use one for a setup step such as waiting for a database or fetching a config file. Here setup takes 15 seconds and writes a file that app reads:
apiVersion: v1kind: Podmetadata:name: init-demospec:volumes:- name: workemptyDir: {}initContainers:- name: setupimage: busybox:1.38.0command: ["sh", "-c", "echo preparing; sleep 15; echo ready > /work/flag; echo done"]volumeMounts:- name: workmountPath: /workcontainers:- name: appimage: busybox:1.38.0command: ["sh", "-c", "cat /work/flag; sleep 3600"]volumeMounts:- name: workmountPath: /work
While setup runs, the status is Init:0/1: zero of one init containers have finished, and app has not started. Once setup exited successfully, app started and the Pod became Running. The logs show setup printed preparing and done, and app read the ready that setup left in the shared volume. With several init containers, they run one at a time, in order.
A container that keeps failing
This container prints a line, waits five seconds, complains about a missing config file and exits with status 1, the way an app does when it cannot start:
apiVersion: v1kind: Podmetadata:name: crashyspec:containers:- name: appimage: busybox:1.38.0command: ["sh", "-c", "echo starting at $(date +%T); sleep 5; echo 'config file /etc/app.conf not found' >&2; exit 1"]
A Pod's restartPolicy defaults to Always, so the kubelet starts the container again each time it exits. Apply the file, then run kubectl get pod crashy every few seconds for two minutes. kubectl get pod crashy --watch prints each change as it happens instead (Ctrl+C stops it).
The first restart comes at once. After that the kubelet waits longer each time before starting the container again: about 10 seconds before the second restart, about 25 before the third and about 45 before the fourth. The documented delays are 10, 20 and 40 seconds, doubling up to a cap of five minutes, and the real waits run a little longer. Between attempts the STATUS column mostly says Error, the result of the last run. CrashLoopBackOff appears only briefly, just before the next attempt. Both mean the same thing: the container keeps exiting and the kubelet is holding back. After the fourth restart:
RESTARTS counts every attempt and says how long ago the last one was. describe shows the current state and the last state, both Terminated with reason Error and Exit Code: 1. The exit code is your first clue. 1 usually means the app gave up on its own, while 137 means it was killed, often for using too much memory. While the container is stopped, kubectl logs shows that stopped run, and the real reason is in it: the config file is missing.
Once the kubelet starts the next attempt, plain logs shows the new run instead. To read the run that crashed, add --previous:
The Pod is Running again with 5 restarts, and --previous prints the crashed run, with the same start time that plain logs showed while the container was stopped. The events show six starts, with back-offs in between.
restartPolicy
Always suits servers that should never stop. OnFailure restarts only after a non-zero exit, which suits batch work. Never leaves a stopped container stopped:
apiVersion: v1kind: Podmetadata:name: oncespec:restartPolicy: Nevercontainers:- name: appimage: busybox:1.38.0command: ["sh", "-c", "echo starting; exit 1"]
With Never, the same failure ends as status Error with 0 restarts, and the Pod is finished for good. -o custom-columns builds a table from the fields you name, here each Pod's policy and restart count: only crashy restarted. You will rarely set the policy on a bare Pod. Deployments require Always, and Jobs, which run a task to completion, use OnFailure or Never.
Hands-on
Break the init container and see what happens to the app container.
1. Make a broken copy of init.yaml, named init-broken and with sleep 15 replaced by exit 1: sed -e 's/init-demo/init-broken/' -e 's/sleep 15/exit 1/' init.yaml > init-broken.yaml
2. Apply it: kubectl apply -f init-broken.yaml
3. Run kubectl get pod init-broken every few seconds until RESTARTS shows 2
4. Look at the app container's state: kubectl get pod init-broken -o jsonpath='{.status.containerStatuses[0].state}'
5. Read the init container's output: kubectl logs init-broken -c setup
6. Clean up: kubectl delete pod init-broken
What you should see: in step 3 the status is Init:Error (or Init:CrashLoopBackOff just before a retry), and the restart count is the init container's. Step 4 prints {"waiting":{"reason":"PodInitializing"}}: the app container never starts while its init container fails. Step 5 prints only preparing, because setup now exits right after that line.
Remember
Containers in one Pod share an IP address, localhost and any volumes they both mount, and init containers must finish before the others start. A rising restart count means the container itself keeps exiting: its logs, and the exit code in describe, tell you why.
hostname -i showed for both containers.Init:0/1 for several minutes. What does that tell you?Init:ImagePullBackOff or Init:ErrImagePull.app started only after setup had exited successfully.0/1 counts init containers that finished, not running containers.logs shows that new run.--previous reads the container run before the current one, here the run that crashed.