Sub topic 1
What containers in a Pod share
A Pod is the smallest unit Kubernetes schedules. It can hold several containers, and those containers share some resources and keep others separate. Init containers and sidecars both depend on this split, so this chapter sets out which is which.
Prerequisites
This chapter assumes you can:
- Read and write YAML manifests.
- Use
kubectlto apply resources, inspect them, and view logs. - Explain what a container image is and what a Deployment and a Service do.
The model: shared by the Pod, private to the container
Kubernetes starts each container in its own isolated environment. It then places all containers of a Pod in a few shared environments. The Pod's containers always run on the same node.
| Resource | Shared between containers? | Notes |
|---|---|---|
| Network namespace | Yes | One IP address, one set of ports, `localhost` works |
| Volumes | Only if mounted in both | You declare them in `spec.volumes` |
| Container filesystem | No | Each image has its own root filesystem |
| Process IDs | No, by default | Opt in with `shareProcessNamespace: true` |
| CPU and memory limits | No | Set per container |
| Image and environment variables | No | Set per container |
Shared network
All containers in a Pod share one network namespace. The Pod gets a single IP address, and every container uses it. Two consequences follow:
- Containers reach each other on
localhost. No Service is needed. - Containers share one port space. Two containers in the same Pod cannot both listen on port 80.
The next Pod runs nginx and a second container that calls it over localhost.
apiVersion: v1
kind: Pod
metadata:
name: shared-network
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80
- name: client
image: busybox:1.36
command: ['sh', '-c', 'while true; do wget -qO- http://localhost:80 | head -n 4; sleep 10; done']Apply it and read the client's output:
kubectl apply -f shared-network.yaml
kubectl logs shared-network -c clientYou see nginx's welcome page. The client container found web without any DNS name or Service.
Clients outside the Pod see one address. They cannot tell which container answered, so you expose a port by listing it on the container that listens on it.
Shared volumes
Containers do not share files by default. A file written in one container's filesystem is invisible to the others. To share files, declare a volume at the Pod level and mount it into each container that needs it.
apiVersion: v1
kind: Pod
metadata:
name: shared-volume
spec:
containers:
- name: writer
image: busybox:1.36
command: ['sh', '-c', 'while true; do date > /data/now.txt; sleep 5; done']
volumeMounts:
- name: scratch
mountPath: /data
- name: reader
image: busybox:1.36
command: ['sh', '-c', 'while true; do cat /data/now.txt; sleep 5; done']
volumeMounts:
- name: scratch
mountPath: /data
volumes:
- name: scratch
emptyDir: {}An emptyDir volume is a directory that Kubernetes creates empty when the Pod starts on a node. It lives as long as the Pod does. Container restarts keep its contents, but deleting the Pod removes them. The next chapter covers emptyDir in more detail.
Each container chooses its own mountPath. The writer and reader could mount the same volume at different paths and still see the same files.
Other shared and optional settings
- IPC. Containers in a Pod can use shared memory and System V IPC, because they share an IPC namespace.
- Process namespace. Setting
spec.shareProcessNamespace: truelets containers see each other's processes and send signals. It is off by default. - Pod-level fields.
restartPolicy,serviceAccountName,nodeSelector, andvolumesapply to the whole Pod. Image, command, probes, and resource limits belong to each container.
Common errors
- Port conflict. Two containers listen on the same port. One fails to bind and often enters
CrashLoopBackOff. Check its logs for "address already in use". - Missing mount. The volume is declared but mounted in only one container. The other container sees an empty or absent path.
- Wrong assumption about
localhost.localhostreaches containers in the same Pod only. A container in another Pod needs that Pod's IP or a Service name. - Shared limits assumed. Each container needs its own memory limit. One container exceeding its limit is killed on its own, and the others keep running.
Exercise
A Pod has containers api and metrics. api writes /tmp/stats.json in its own filesystem. metrics tries to read /tmp/stats.json and fails. Name two changes that fix this, and say why both containers can still use port 9090 only if one of them is moved.
Show answer
Declare an emptyDir volume in spec.volumes and mount it in both containers, then have api write to the mounted path (or point both at the same mountPath). Without a volume, each container has a private filesystem. Port 9090 cannot be used by both containers because they share one network namespace and one port space, so one of them must listen on a different port.