Objects & Manifests
Pods, Deployments, ReplicaSets, Services, labels, namespaces, and kustomize
Pods
A pod is one or more containers that share a network namespace (one IP,
localhost between them) and volumes, scheduled together onto one node.
It's the unit Kubernetes runs. Pods are disposable: a replaced pod gets
a new name and a new IP. Nothing should depend on a particular pod.
Deployments and ReplicaSets
You almost never create pods directly. A Deployment describes a pod template and a replica count; it manages ReplicaSets, one per version of the template; each ReplicaSet keeps its number of pods running.
kubectl -n shop get deploy,rs
NAME READY UP-TO-DATE AVAILABLE
deployment.apps/web 3/3 3 3
NAME DESIRED CURRENT READY
replicaset.apps/web-7cb7f4d4cd 3 3 3 # the current template
replicaset.apps/web-65bf54497c 0 0 0 # older templates, kept for rollback
replicaset.apps/web-c66d7f869 0 0 0The hash in each name (7cb7f4d4cd) is a hash of the pod template. Change
anything in the template and you get a new ReplicaSet, which is what a
rollout is (see Rolling Updates).
A bare pod (no controller) is never recreated, and kubectl drain
refuses to remove one without --force, because it would be gone for
good. The labs' first toolbox was a bare pod; it made a node drain fail
in scheduling.py. It's a Deployment now.
Labels and selectors
Objects find each other through labels, not names:
A typo in one label quietly breaks the link: a Service selecting app: wbe
has zero endpoints and every request fails, but every object is "valid."
kubectl get endpointslices -n shop is the first check when traffic goes
nowhere.
Services
A Service gives a stable name and virtual IP to a changing set of pods:
kubectl -n shop get svc,endpointslices
service/web ClusterIP 10.96.95.206 8000/TCP
endpointslice.discovery.k8s.io/web-c2lh9 8000 10.244.3.40,10.244.1.31,10.244.3.41Covered in depth in Services & the Gateway API.
Namespaces
Namespaces group objects for access control, quotas, policies and naming.
This module uses shop for the app, nginx-gateway for the Gateway
controller, kube-system for cluster components, and labs for the lab
toolbox. Namespaces are not a network boundary by themselves: without
NetworkPolicies, any pod can reach any other.
Manifests and kustomize
kubectl apply is declarative: it sends the desired state, and
Kubernetes works out the changes. Keeping manifests in git and applying them
from CI (or a GitOps controller such as Argo CD or Flux) makes git the
source of truth, and every change reviewable.
Avoid changing live objects by hand (kubectl edit, scale, patch)
except in an emergency, and then copy the change back into git. The next
apply from git silently reverts anything that exists only in the cluster.
Lab
- Change the Service's selector to
app: wbeand apply. What doescurl localhost:8180/api/whoamireturn? What do the endpoints show? - Delete a ReplicaSet the Deployment owns. What happens?
- Run
kubectl diff -k manifests/after changing a resource limit.