DevOps
Kubernetes
Kubernetes is a container orchestration system that deploys, scales and heals applications across a cluster of servers.
What it is
Kubernetes was created at Google from experience with its internal system Borg, open sourced in 2014 and handed to the CNCF. It became the standard for container orchestration and the first CNCF project to reach graduated status (2018).
You describe the desired state in YAML (a Deployment: 3 replicas of this image), and controllers keep reconciling reality to it: restart crashed pods, spread them across nodes, run rolling updates. Services and Ingress handle networking, ConfigMaps and Secrets handle configuration.
It is powerful but complex. Many teams choose managed Kubernetes (EKS, GKE, AKS) instead of running their own cluster.
Use it when
- You have many services that must scale and update without downtime.
- You need self-healing, rolling updates and declarative configuration.
- The team can invest in a platform, or you have a managed cluster.
- You need portability across clouds and own servers.
Avoid it when
- You have one or two apps: Docker Compose, a PaaS or serverless is simpler and cheaper.
- A small team without time to operate a cluster.
- You need to ship an MVP quickly.
Key concepts
- Pod
- The smallest deployable unit: one or more containers sharing network and storage.
- Deployment
- Describes the desired number of pod replicas and the update strategy.
- Service / Ingress
- A Service gives a stable address to a set of pods, Ingress routes external HTTP traffic.
- Control loop
- A controller compares desired and actual state and fixes the difference.
- Namespace
- A logical partition of a cluster to isolate teams or environments.
Example
Cheat sheet
| Command or code | What it does |
|---|---|
kubectl get pods -A | Pods in all namespaces |
kubectl apply -f deployment.yaml | Apply a manifest |
kubectl describe pod my-pod | Pod details and events |
kubectl logs -f my-pod | Follow pod logs |
kubectl exec -it my-pod -- sh | Shell inside a pod |
kubectl port-forward svc/web 8080:80 | Forward a service port to local |
kubectl scale deployment web --replicas=3 | Change the replica count |
kubectl rollout status deployment/web | Rollout status |
kubectl rollout undo deployment/web | Roll back to the previous version |
kubectl get events --sort-by=.lastTimestamp | Latest cluster events |
kubectl delete -f deployment.yaml | Delete resources from a manifest |
How to learn it
- Master Docker first: Kubernetes runs containers, so images and Dockerfiles must already be familiar.
- Take the Kubernetes basics course on ShortLesson and start a local cluster with kind, minikube or Docker Desktop.
- Learn the objects: Pod, Deployment, Service, and deploy your app from a YAML manifest with
kubectl apply. - Add a ConfigMap and a Secret, environment variables and readiness and liveness probes.
- Set up an Ingress for external access and resource
requestsandlimitsfor containers. - Learn to debug:
kubectl describe,logs, events, and work through CrashLoopBackOff and ImagePullBackOff. - Try Helm for templating and a rolling update with a rollback (
rollout undo). - Read about StatefulSet, PersistentVolume, RBAC and GitOps (Argo CD) once you reach a production cluster.
Interview questions
1. What is a pod and why, if we have containers?
A pod is a group of containers that always run together on one node and share network and volumes. That allows a main container with a sidecar, for example for logs or a proxy.
2. How does a Deployment differ from a StatefulSet?
A Deployment is for stateless apps where pods are interchangeable. A StatefulSet gives pods stable names and their own volumes, so it fits databases and brokers.
3. How do liveness and readiness probes work?
Liveness checks whether a container is alive and restarts it on failure. Readiness checks whether it can accept traffic: until ready, the Service sends it no requests.
4. How does Kubernetes do a rolling update?
It creates pods of the new version, waits for their readiness and gradually removes old ones within maxSurge and maxUnavailable. If something goes wrong, you can roll back.
Interview questions from the bank
- Describe graceful shutdown of a Node service in Kubernetes that serves HTTP and processes jobs from a queue. What is the order of actions after SIGTERM, and what happens if something hangs?SeniorNode.js
- A Node service in Kubernetes sometimes ends as
OOMKilledwith no JS error and sometimes printsJavaScript heap out of memory, and p99 jumps every few minutes. How does Node pick the heap size in a container, and what would you set?SeniorNode.js - A single Node service handles 500 rps on one pod. The business wants 5000. What in the code could stop you from simply adding pods?SeniorNode.js
- A Node api service calls recommendations over gRPC through a regular Kubernetes Service. Recommendations was scaled from 2 to 8 pods, but the load stayed on the two old ones, and the new ones are almost idle. Why? How do you fix it?MiddleSystem design
- You deployed a new version and the pod is in
CrashLoopBackOff. What do you check first, and in what order?JuniorDevOps - The new version's pods are
RunningandReady, but requests through the Service getconnection refusedand the Ingress returns 503. What do you check?MiddleDevOps - Every rolling deploy causes 30 seconds of 5xx: some requests fail with 502 and
connection refused. Probes are configured and the image is fine. What is happening, and how do you get rid of the errors?MiddleDevOps - A Node service in Kubernetes restarts every few hours with
OOMKilled, exit code 137, although the logs show no errors and the heap graph shows only 300 MB. The memory limit is 512Mi. Why, and what do you change?MiddleDevOps
Comparisons with this technology
Related technologies
- DockerDocker packages an application with its dependencies into a container that runs the same on a laptop, a server and the cloud.
- HelmHelm is a package manager for Kubernetes: an application is packaged as a chart with templates and configured through values.
- Argo CDArgo CD is a GitOps tool for Kubernetes: it watches a Git repository and syncs the cluster to the state described there.
- TerraformTerraform is an infrastructure as code tool: you describe cloud resources in configuration and it creates and changes them.
- PrometheusA monitoring system with a time series database: it scrapes metrics in a pull model and offers the PromQL query language.
Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).