Sub topic 1
What a Pod is and why it exists
Prerequisites
This chapter assumes you can:
- Run a container image with a tool such as Docker and explain what an image is.
- Use a terminal and read basic YAML.
- Describe a Kubernetes cluster as a set of nodes (machines) that run your workloads.
The problem Pods solve
Kubernetes does not schedule individual containers. It schedules Pods. A Pod is the smallest deployable unit in Kubernetes. It wraps one or more containers that always run together on the same node.
Some applications need a helper process running right next to the main one. Examples are a log shipper or a proxy. These processes must share a network address and some files, and they must start and stop together. A Pod gives them that shared environment as a single unit.
Most Pods hold one container. Think of the Pod as a thin wrapper around that container. You add a second container only when the two are tightly coupled.
What containers in a Pod share
Containers in the same Pod share:
- One IP address. Containers reach each other on
localhost. Two containers in one Pod cannot listen on the same port. - Volumes. A volume declared on the Pod can be mounted into any of its containers.
- A node. All containers in a Pod are placed on the same node. A Pod is never split across nodes.
- A lifecycle. The Pod is created, scheduled, and deleted as a whole.
Each container still has its own image, filesystem, and process. Containers do not share a filesystem unless you mount a shared volume.
A first Pod
This manifest defines a Pod with one nginx container.
apiVersion: v1
kind: Pod
metadata:
name: web
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.27
ports:
- containerPort: 80Create it and check that it runs:
kubectl apply -f pod.yaml
kubectl get pod web -o wideThe -o wide output shows the Pod's IP address and the node it landed on.
Pods are disposable
A Pod is not a long-lived server that you repair. If a Pod is deleted or its node fails, Kubernetes does not move that Pod elsewhere. The replacement is a new Pod with a new name and a new IP address. For this reason, you should not depend on a Pod's IP or on data stored only inside its containers. Later chapters cover Services, volumes, and controllers, which handle these concerns.
Exercise
Two containers in the same Pod both need to listen on port 8080. Will this work? Explain why.
Show answer
No. Containers in a Pod share one network namespace and one IP address, so they share the same port space. The second container fails to bind port 8080. Change one container to use a different port, or place the containers in separate Pods.