Inside a Pod

Shared network, shared files, and restarts.

Beginner20 min · lesson 4 of 21
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (5 files, 1 KB): pods.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: 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: 3700c145dab65a5d3c77cf3a39c831a160f1fcea43f9f64770ac673e2ec19531

The 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:

shared.yaml
apiVersion: v1
kind: Pod
metadata:
name: shared
spec:
volumes:
- name: html
emptyDir: {}
containers:
- name: web
image: nginx:1.30.6-alpine
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
- name: writer
image: busybox:1.38.0
command: ["sh", "-c", "while true; do date > /out/index.html; sleep 5; done"]
volumeMounts:
- name: html
mountPath: /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):

ubuntu@secopslog-k8s:~/lab/pods · Kubernetes 1.37
$ kubectl apply -f shared.yaml && kubectl wait --for=condition=Ready pod/shared --timeout=120s && kubectl get pod shared
pod/shared created pod/shared condition met NAME READY STATUS RESTARTS AGE shared 2/2 Running 0 2s
$ kubectl exec shared -c writer -- wget -qO- http://localhost
Wed Nov 11 09:43:27 UTC 2026
$ kubectl exec shared -c web -- cat /usr/share/nginx/html/index.html
Wed Nov 11 09:43:27 UTC 2026
$ kubectl get pod shared -o jsonpath='{.status.podIP}{"\n"}' && kubectl exec shared -c web -- hostname -i && kubectl exec shared -c writer -- hostname -i
10.244.2.2 10.244.2.2 10.244.2.2

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:

init.yaml
apiVersion: v1
kind: Pod
metadata:
name: init-demo
spec:
volumes:
- name: work
emptyDir: {}
initContainers:
- name: setup
image: busybox:1.38.0
command: ["sh", "-c", "echo preparing; sleep 15; echo ready > /work/flag; echo done"]
volumeMounts:
- name: work
mountPath: /work
containers:
- name: app
image: busybox:1.38.0
command: ["sh", "-c", "cat /work/flag; sleep 3600"]
volumeMounts:
- name: work
mountPath: /work
ubuntu@secopslog-k8s:~/lab/pods · Kubernetes 1.37
$ kubectl apply -f init.yaml
pod/init-demo created
$ kubectl get pod init-demo
NAME READY STATUS RESTARTS AGE init-demo 0/1 Init:0/1 0 1s
$ kubectl wait --for=condition=Ready pod/init-demo --timeout=120s && kubectl get pod init-demo
pod/init-demo condition met NAME READY STATUS RESTARTS AGE init-demo 1/1 Running 0 21s
$ kubectl logs init-demo -c setup && kubectl logs init-demo -c app
preparing done ready

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:

crash.yaml
apiVersion: v1
kind: Pod
metadata:
name: crashy
spec:
containers:
- name: app
image: busybox:1.38.0
command: ["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).

ubuntu@secopslog-k8s:~/lab/pods · Kubernetes 1.37
$ kubectl apply -f crash.yaml
pod/crashy created

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:

ubuntu@secopslog-k8s:~/lab/pods · Kubernetes 1.37
$ kubectl get pod crashy
NAME READY STATUS RESTARTS AGE crashy 0/1 Error 4 (54s ago) 114s
$ kubectl describe pod crashy | sed -n '/^ State:/,/^ Restart Count:/p'
State: Terminated Reason: Error Exit Code: 1 Started: Wed, 11 Nov 2026 09:45:41 +0000 Finished: Wed, 11 Nov 2026 09:45:47 +0000 Last State: Terminated Reason: Error Exit Code: 1 Started: Wed, 11 Nov 2026 09:44:49 +0000 Finished: Wed, 11 Nov 2026 09:44:54 +0000 Ready: False Restart Count: 4
$ kubectl logs crashy
starting at 09:45:41 config file /etc/app.conf not found

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:

ubuntu@secopslog-k8s:~/lab/pods · Kubernetes 1.37
$ kubectl get pod crashy
NAME READY STATUS RESTARTS AGE crashy 1/1 Running 5 (82s ago) 3m15s
$ kubectl logs crashy --previous
starting at 09:45:41 config file /etc/app.conf not found
$ kubectl events --for pod/crashy | grep -E 'LAST SEEN|BackOff|Started'
LAST SEEN TYPE REASON OBJECT MESSAGE 81s (x4 over 3m1s) Warning BackOff Pod/crashy Back-off restarting failed container app in pod crashy_default(d00677fe-11a3-44ba-aeed-cffe92719690) 0s (x6 over 3m15s) Normal Started Pod/crashy Container started

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:

once.yaml
apiVersion: v1
kind: Pod
metadata:
name: once
spec:
restartPolicy: Never
containers:
- name: app
image: busybox:1.38.0
command: ["sh", "-c", "echo starting; exit 1"]
ubuntu@secopslog-k8s:~/lab/pods · Kubernetes 1.37
$ kubectl apply -f once.yaml
pod/once created
$ kubectl get pod once
NAME READY STATUS RESTARTS AGE once 0/1 Error 0 3s
$ kubectl get pods -o custom-columns=POD:.metadata.name,POLICY:.spec.restartPolicy,RESTARTS:.status.containerStatuses[0].restartCount
POD POLICY RESTARTS crashy Always 5 init-demo Always 0 once Never 0 shared Always 0
$ kubectl delete pod shared init-demo crashy once && kubectl get pods
pod "shared" deleted from default namespace pod "init-demo" deleted from default namespace pod "crashy" deleted from default namespace pod "once" deleted from default namespace No resources found in default namespace.

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.

Quick check
01Two containers in one Pod both try to listen on port 8080. What happens?
Correct — Containers in a Pod share an IP address and ports, as hostname -i showed for both containers.
Incorrect — Both containers in the lab reported the same IP address.
Incorrect — Kubernetes never changes the ports your containers listen on.
Incorrect — All containers of a Pod always run together on one node.
02A Pod shows Init:0/1 for several minutes. What does that tell you?
Incorrect — The main container has not even started. It waits for every init container.
Incorrect — Possible, but a failing pull would show Init:ImagePullBackOff or Init:ErrImagePull.
Correct — app started only after setup had exited successfully.
Incorrect — The 0/1 counts init containers that finished, not running containers.
03A container crashes, and the kubelet has already started it again. Which command shows the output of the run that crashed?
Incorrect — Once the next attempt runs, plain logs shows that new run.
Correct — --previous reads the container run before the current one, here the run that crashed.
Incorrect — exec reaches the new run only, and container output is not kept in files.
Incorrect — describe shows the exit code and events, not the container's output.

Related