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.
On this page
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
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 |
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 shopExit 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
selectormatches the pod templatelabels. - CPU and memory
requestsare 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
minReplicasenough 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
kubectl get endpointslices -l kubernetes.io/service-name=payment-service: empty →selectordoesn't matchlabels, or the pod isn't ready.- Endpoints exist but still refused →
targetPortdiffers from the app's port, or the app listens only on127.0.0.1. kubectl port-forward pod/<pod> 8080:8080: works means the Service is wrong, fails means the app is.- 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. requestsfeed 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
- What are a Pod, a Deployment and a Service in Kubernetes? How do you expose a service outside the cluster?
- How does service discovery work in Kubernetes, from the DNS name to the pod?
- How do Kubernetes requests and limits differ? How should you set them for the JVM and the HPA?
- A Spring Boot pod is in CrashLoopBackOff or restarts with exit code 137. How do you investigate?
- How do readiness, liveness and startup probes differ? How do you configure them for Spring Boot?
- What is the Twelve-Factor App? How do config and stateless processes apply to Spring Boot?
Comments
Community rules