SteelReed

Search

Search posts, questions…

6. DevOps & Operations

Kubernetes for backend developers: architecture, Pods, Services, ConfigMaps, autoscaling and debugging failing pods

How Kubernetes runs on desired state and reconcile loops, what the control plane and nodes contain, how Pods, Deployments, StatefulSets and Jobs differ, ClusterIP/NodePort/LoadBalancer Services, Ingress and the Gateway API, ConfigMaps and Secrets, requests/limits and QoS, how the HPA computes replica counts, and a checklist for Pending, ImagePullBackOff, CrashLoopBackOff and OOMKilled.

19 Nov 20268 min readMid

–

1. The problem

Spring Boot takes 40 seconds to start. The liveness probe starts checking at second 10 and restarts after 3 failures. The pod sits in CrashLoopBackOff forever, and kubectl get pods only says "restarting".

2. Key idea

Docker runs containers on one machine. Kubernetes runs them across many machines like a thermostat: you set "3 pods" and controllers measure and correct until there are 3. If a node dies at midnight, its pods are recreated elsewhere. Everything goes through the API server and is stored in etcd; the scheduler picks a node, the kubelet on that node runs the containers.

2.1 Which workload?

A Pod is one or a few containers sharing an IP. A dead pod is replaced by a new pod with a new IP, so never store pod IPs.

Kind Use when Don't use when
Deployment APIs, stateless workers you need fixed names and disks
StatefulSet Kafka, databases: kafka-0, one disk per pod stateless services
DaemonSet log and monitoring agents: one pod per node business apps
Job / CronJob migrations, batches that finish long-running services

2.2 How does a request reach a pod?

A Service gives a stable DNS name and virtual IP to pods matching its selector and ready. ClusterIP (the default) is reachable only inside the cluster; LoadBalancer asks the cloud for a load balancer. Ingress or the Gateway API routes HTTP by host and path to many services through one entry point. "Service has no endpoints" almost always means the selector doesn't match the pod labels, or every pod fails readiness.

2.3 requests, limits and autoscaling

  • requests: what the node reserves; the scheduler uses it, the HPA uses it as the denominator.
  • limits: hard caps. Exceed memory and you're killed (OOMKilled, exit 137); exceed CPU and you're throttled, p99 climbs.
  • HPA: desired = ceil(current × metric / target). CPU is measured as % of requests; no requests means CPU-based HPA doesn't work.
  • A Secret is just base64. Real protection is RBAC, etcd encryption, an external secret store.

3. A sample manifest

YAML
apiVersion: apps/v1
kind: Deployment
metadata: { name: order-service }
spec:
  replicas: 3
  selector: { matchLabels: { app: order-service } }   # must match the labels below
  template:
    metadata: { labels: { app: order-service } }
    spec:
      containers:
        - name: app
          image: registry.local/order-service:1.4.2     # pinned tag, not :latest
          ports: [{ containerPort: 8080 }]
          env: [{ name: JAVA_TOOL_OPTIONS, value: "-XX:MaxRAMPercentage=75" }]
          resources:
            requests: { cpu: 500m, memory: 768Mi }
            limits: { memory: 768Mi }                   # exceed it and you're OOMKilled
          startupProbe:   { httpGet: { path: /actuator/health/liveness, port: 8080 }, failureThreshold: 30, periodSeconds: 2 }
          livenessProbe:  { httpGet: { path: /actuator/health/liveness, port: 8080 } }
          readinessProbe: { httpGet: { path: /actuator/health/readiness, port: 8080 } }

startupProbe allows 60 seconds to start; liveness only runs after startup succeeds. Changing a ConfigMap doesn't restart pods; Helm and Kustomize put its hash in an annotation so each change becomes a rollout.

Debugging failing pods: symptoms and commands
Symptom Common cause Where to look
Pending no node has enough requests left Events FailedScheduling
ImagePullBackOff wrong tag, missing imagePullSecrets Events
CrashLoopBackOff config error, liveness killing it logs --previous, exit code
OOMKilled (137) heap + off-heap > limit describe → Last State
0/1 READY readiness wrong path/port Events
BASH
kubectl describe pod <pod> -n shop        # Events at the bottom: the real reason
kubectl logs <pod> -n shop --previous     # logs from the run that just died
kubectl get endpointslices -l kubernetes.io/service-name=order-service
kubectl port-forward svc/order-service 8080:80 -n shop
kubectl rollout undo deploy/order-service -n shop

Exit codes: 1 app error, 137 = SIGKILL (OOM or liveness), 143 = SIGTERM (normal stop).

4. Working with AI

Delegate to AI: writing manifests and Helm charts, reading long describe output.

Check yourself:

  • The Service selector matches the pod template labels.
  • CPU and memory requests are set; the memory limit covers heap + off-heap.
  • Slow-starting apps have a startupProbe; liveness doesn't check the DB.
  • Images use pinned tags; Secrets aren't committed to Git.

5. Exercises

Exercise 1: How many pods will the HPA run?

AI configured: 4 pods, CPU request 500m, HPA target 70%, minReplicas: 2, maxReplicas: 20, noting "enough for any traffic spike". Compute the pod count when average CPU per pod is 450m, 370m, 2000m. Is "enough for any spike" true?

Solution to Exercise 1
  • 450m = 90% → ceil(4 × 90/70) = 6.
  • 370m = 74% → within the default 10% tolerance, stays at 4.
  • 2000m = 400% → 23, capped at 20. 400% shows requests are set far below real load.
  • Not true: the HPA reacts tens of seconds after metrics, and a JVM pod needs tens more seconds to be ready. Keep minReplicas enough for base load and scale ahead of peaks. Queue workers scale better on lag (KEDA) than on CPU.

Exercise 2: The Service doesn't answer

The payment-service pod is Running 1/1, but calling http://payment-service/actuator/health from another pod gives Connection refused. What do you check, in order?

Solution to Exercise 2
  1. kubectl get endpointslices -l kubernetes.io/service-name=payment-service: empty → selector doesn't match labels, or the pod isn't ready.
  2. Endpoints exist but still refused → targetPort differs from the app's port, or the app listens only on 127.0.0.1.
  3. kubectl port-forward pod/<pod> 8080:8080: works means the Service is wrong, fails means the app is.
  4. Another namespace needs payment-service.<namespace>. A blocking NetworkPolicy usually gives a timeout, not refused.

Summary

  • Declare desired state; controllers fix reality to match.
  • Pods are temporary; a Service points at ready pods through its selector.
  • requests feed the scheduler and HPA; exceeding the memory limit is OOMKilled 137.
  • Slow-starting apps need a startupProbe.
  • Debug with Events and logs --previous.

Further reading: Deployment strategies.


Self-compiled notes from official documentation and hands-on practice.

Practice interview questions on this topic

All DevOps & Cloud questions →

Loading comments…
Next on the roadmapPerformance testing: load, stress, spike, soak and coordinated omission