Learn Labs
Foundations

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         0

The 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).

Labels and selectors

Objects find each other through labels, not names:

Deploymentselector: { matchLabels: { app: web } }"my pods are the ones labeled app=web"template:metadata: { labels: { app: web } }...and every pod it creates carries that labelServiceselector: { app: web }send traffic to ready pods labeled app=webPodDisruptionBudget, NetworkPolicy, HPA (via the Deployment), topology spreadselector / podSelector: { matchLabels: { app: web } }

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.41

Covered 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

manifests/├── kustomization.yamlnamespace: shop; resources: [...]├── namespace.yamlwith Pod Security labels├── redis.yamlService + StatefulSet├── web.yamlConfigMap, Deployment, Service, PDB, HPA├── policies.yamlNetworkPolicies└── gateway.yamlGateway + HTTPRoutekubectl apply -k manifests/apply all, with the namespace set on eachkubectl apply -k manifests/ --dry-run=servervalidate against the live API firstkubectl diff -k manifests/what would change

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.

Lab

  1. Change the Service's selector to app: wbe and apply. What does curl localhost:8180/api/whoami return? What do the endpoints show?
  2. Delete a ReplicaSet the Deployment owns. What happens?
  3. Run kubectl diff -k manifests/ after changing a resource limit.

On this page