Kubernetes
Container orchestration with Kubernetes — cluster architecture (control plane, kubelet), the reconciliation loop, Pods, Deployments, Services, Ingress, ConfigMaps and Secrets, probes, resource requests and limits, autoscaling, StatefulSets, and rolling updates.
Running one container is easy. Running hundreds across dozens of machines — restarting crashed ones, spreading load, rolling out new versions without downtime, scaling with traffic — needs an orchestrator. Kubernetes (K8s) is the industry standard, offered by every cloud (EKS, GKE, AKS).
The core idea: desired state and reconciliation
You declare the desired state in YAML ("3 replicas of image api:1.4.2, each with 500m CPU"). Controllers continuously compare actual state with desired state and act to close the gap:
loop forever:
desired = read spec from API server
actual = observe cluster
if actual != desired:
take action (create pod, kill pod, update endpoint…)A node dies? Its pods disappear from "actual"; the controller creates replacements elsewhere. This reconciliation loop is what makes Kubernetes self-healing — and the pattern behind every operator.
Architecture
| Component | Role |
|---|---|
| API server | The only component that talks to etcd; everything (kubectl, controllers, kubelets) goes through its REST API |
| etcd | Consistent key–value store holding all cluster state (Raft consensus — Consensus) |
| Scheduler | Assigns new pods to nodes based on resources, affinity, taints and tolerations |
| Controller manager | Runs controllers: Deployment, ReplicaSet, Node, Job… |
| kubelet (each node) | Ensures the pods assigned to its node are running and healthy |
| kube-proxy / CNI | Implements Service networking; the CNI plugin gives every pod an IP |
| Container runtime | containerd or CRI-O actually runs containers |
Core objects
Pod
The smallest deployable unit: one or more containers sharing a network namespace (same IP, talk via localhost) and volumes. Usually one main container, sometimes plus sidecars (log shipper, service mesh proxy). Pods are ephemeral — they get replaced, not repaired, and get new IPs.
Deployment
Manages a ReplicaSet that keeps N identical pods running, and performs rolling updates:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
spec:
replicas: 3
selector:
matchLabels: { app: api }
strategy:
rollingUpdate: { maxSurge: 1, maxUnavailable: 0 } # zero-downtime rollout
template:
metadata:
labels: { app: api }
spec:
containers:
- name: api
image: registry.example.com/api:1.4.2
ports: [{ containerPort: 3000 }]
envFrom:
- configMapRef: { name: api-config }
- secretRef: { name: api-secrets }
resources:
requests: { cpu: 250m, memory: 256Mi }
limits: { memory: 512Mi }
readinessProbe:
httpGet: { path: /readyz, port: 3000 }
periodSeconds: 5
livenessProbe:
httpGet: { path: /healthz, port: 3000 }
initialDelaySeconds: 10Service
Pods come and go with changing IPs. A Service gives a stable virtual IP and DNS name (api.default.svc.cluster.local) that load-balances across all ready pods matching a label selector:
apiVersion: v1
kind: Service
metadata: { name: api }
spec:
selector: { app: api }
ports: [{ port: 80, targetPort: 3000 }]
type: ClusterIP # internal only (default)| Service type | Reachable from |
|---|---|
ClusterIP | Inside the cluster |
NodePort | Each node's IP on a high port |
LoadBalancer | A cloud load balancer with a public IP |
ExternalName | DNS alias to an outside service |
Ingress and Gateway API
HTTP(S) routing from outside into Services — by host and path, with TLS termination — via an ingress controller (NGINX, Traefik, cloud LBs). The newer Gateway API is its more expressive successor.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata: { name: web }
spec:
tls: [{ hosts: [api.example.com], secretName: api-tls }]
rules:
- host: api.example.com
http:
paths:
- path: /
pathType: Prefix
backend: { service: { name: api, port: { number: 80 } } }Configuration
- ConfigMap: non-secret config, injected as env vars or files.
- Secret: sensitive values — only base64-encoded by default, so enable encryption at rest, restrict access with RBAC, or use an external secrets manager (Vault, cloud secret stores via External Secrets Operator).
- Namespaces partition a cluster for teams or environments, with their own quotas and RBAC.
Other workloads
| Object | For |
|---|---|
| StatefulSet | Stateful apps (databases, Kafka): stable names (db-0, db-1), stable per-pod storage, ordered rollout |
| DaemonSet | One pod per node: log collectors, monitoring agents, CNI |
| Job / CronJob | Run-to-completion tasks / scheduled tasks |
| PersistentVolumeClaim | Request durable storage, provisioned by a StorageClass (EBS, PD) |
Health probes
| Probe | Question | On failure |
|---|---|---|
| Readiness | Can this pod serve traffic right now? | Removed from Service endpoints (not restarted) |
| Liveness | Is this pod stuck beyond recovery? | Container restarted |
| Startup | Has a slow-starting app finished booting? | Delays the other probes |
Common mistake: a liveness probe that checks the database — when the DB blips, every pod restarts at once, making the outage worse. Liveness should check only the process itself.
Resources and scheduling
- Requests: what the scheduler reserves; a pod is placed only on a node with enough unreserved capacity.
- Limits: hard caps. Exceeding the memory limit gets the container OOMKilled; exceeding a CPU limit causes throttling (many teams set CPU requests but no CPU limit).
- QoS classes: Guaranteed (requests = limits), Burstable, BestEffort — determine eviction order under pressure.
- Placement: node selectors, affinity/anti-affinity (spread replicas across zones), taints and tolerations (dedicated GPU nodes), topology spread constraints.
Scaling
| Autoscaler | Scales |
|---|---|
| Horizontal Pod Autoscaler (HPA) | Number of pods, by CPU, memory or custom metrics (requests per second, queue length — KEDA for event-driven) |
| Vertical Pod Autoscaler (VPA) | Pods' requests/limits |
| Cluster Autoscaler / Karpenter | Number of nodes, when pods can't be scheduled or nodes are idle |
kubectl autoscale deployment api --cpu-percent=70 --min=3 --max=30Deploying and day-2 operations
kubectl apply -f k8s/ # declarative: create or update
kubectl get pods -o wide # list pods and their nodes
kubectl describe pod api-7d9f… # events: scheduling, pulls, probe failures
kubectl logs -f deploy/api # logs
kubectl exec -it api-7d9f… -- sh # shell into a container
kubectl rollout status deploy/api
kubectl rollout undo deploy/api # roll back to the previous ReplicaSet
kubectl port-forward svc/api 8080:80 # local access for debuggingCommon failure states: CrashLoopBackOff (the app keeps exiting — check logs), ImagePullBackOff (wrong image name or registry auth), Pending (no node has enough resources), OOMKilled (memory limit too low or a leak).
Teams usually manage manifests with Helm charts or Kustomize overlays and deploy via GitOps (Argo CD, Flux): the cluster continuously syncs to manifests in a Git repository (CI/CD).
Is Kubernetes worth it?
Kubernetes is powerful but complex: networking, storage, security (RBAC, network policies, pod security), upgrades and observability are real operational work. For small teams, managed platforms — Cloud Run, ECS/Fargate, App Runner, Fly.io, or serverless (Serverless and edge) — often deliver most of the value with far less effort.
- Kubernetes reconciles actual state toward declared desired state — the basis of self-healing.
- Control plane: API server, etcd, scheduler, controllers; nodes run kubelet, kube-proxy and a container runtime.
- Pods are ephemeral; Deployments manage replicas and rolling updates; Services give stable discovery and load balancing; Ingress/Gateway route HTTP.
- ConfigMaps and Secrets inject config; StatefulSets, DaemonSets and Jobs cover other workloads.
- Readiness gates traffic, liveness restarts stuck containers; requests drive scheduling, memory limits cause OOMKills.
- HPA scales pods, Cluster Autoscaler/Karpenter scales nodes; GitOps with Helm/Kustomize manages deployments.
QWhat's the difference between readiness and liveness probes?medium
A readiness probe tells Kubernetes whether a pod should receive traffic; if it fails, the pod is removed from the Service's endpoints but keeps running, which is right for warm-up or temporary dependency issues. A liveness probe tells Kubernetes whether the container is irrecoverably stuck; if it fails, the container is restarted. Liveness checks should not depend on external services, or a dependency outage will restart every pod.
QWhat happens when you run kubectl apply on a Deployment with a new image?hard
kubectl sends the updated spec to the API server, which validates it and stores it in etcd. The Deployment controller notices the template changed and creates a new ReplicaSet, scaling it up and the old one down according to maxSurge and maxUnavailable. The scheduler assigns the new pods to nodes; each node's kubelet pulls the image and starts containers via the runtime. Once a new pod passes its readiness probe, it's added to the Service's endpoints and an old pod is terminated (SIGTERM, grace period, then SIGKILL). The rollout can be paused or undone back to the previous ReplicaSet.