Sub topic 1
Pods: the Thing Being Replicated
Prerequisites
This chapter assumes you know what a cluster and a node are, can read basic YAML, and can run kubectl get, describe and apply. You should also know what a container image and a tag are.
Pods: the thing being replicated
A Pod is the smallest unit Kubernetes schedules. It wraps one or more containers that share a network identity (one IP address and one port space) and can share volumes. Most Pods hold a single application container.
Pods are not meant to last. If a Pod's node fails, or someone deletes the Pod, Kubernetes does not bring that same Pod back. A bare Pod you create by hand has no one watching over it. A ReplicaSet or Deployment replaces lost Pods by creating new ones from a template. The new Pods have different names and different IP addresses.
For this reason, treat Pods as interchangeable. Do not depend on one Pod's name or IP. Clients should reach a group of Pods through a stable abstraction, such as a Service.
Here is a bare Pod:
apiVersion: v1
kind: Pod
metadata:
name: web-pod
labels:
app: web
tier: frontend
spec:
containers:
- name: web
image: nginx:1.25
ports:
- containerPort: 80Create it and look at it:
kubectl apply -f pod.yaml
kubectl get pod web-pod -o wide
kubectl delete pod web-pod
kubectl get pod web-podAfter the delete, the Pod is gone for good. Nothing recreates it. That gap is what ReplicaSets fill.
Labels and selectors: how objects find their Pods
A label is a key/value pair attached to an object's metadata. Labels carry no meaning for Kubernetes on their own. You choose them, and other objects use them to find things.
A selector is a query over labels. The most common form is matchLabels, which requires every listed pair to be present. A ReplicaSet, Deployment or Service holds a selector and treats every Pod whose labels match as its own, or as its target.
In the Pod above, app: web and tier: frontend are labels. A selector with app: web matches it. A selector with app: web and tier: backend does not, because every pair must match.
You can try selectors from the command line:
kubectl get pods -l app=web
kubectl get pods -l 'app=web,tier=frontend'
kubectl get pods -l 'tier in (frontend,backend)'
kubectl get pods --show-labelsThe same idea appears inside manifests:
selector:
matchLabels:
app: webThe label link is how a controller knows which Pods to count. It does not track Pods by name. It lists Pods that match its selector and compares that count with the number you asked for. The next chapters build on this.
Exercise
You create a Pod with labels app: web and tier: frontend. Which selectors match it: (a) app=web, (b) app=web,tier=backend, (c) tier in (frontend,backend)?
Show answer
(a) and (c) match. (b) fails because every pair in a selector must match, and the Pod's tier is frontend, not backend.
Exercise
You delete a bare Pod that no controller owns. What happens, and why?
Show answer
The Pod stays deleted. Nothing is watching it, so nothing creates a replacement. A ReplicaSet would notice the missing Pod through its selector and create a new one.