All Posts

How Kubernetes Works: The Container Orchestrator

Docker runs containers. Kubernetes manages them. We explain Pods, Nodes, Deployments, and Services to demystify the world's most popular orchestrator.

Abstract AlgorithmsAbstract Algorithms
ยทยท5 min read
Share
Share on X / Twitter
Share on LinkedIn
Copy link

TLDR: Kubernetes (K8s) is an operating system for the cloud. It manages clusters of computers (Nodes) and schedules applications (Pods) onto them. It handles self-healing (restarting crashed apps), scaling (adding more copies), and networking via a continuous declarative control loop.


๐Ÿ“– The Shipping Port Manager Analogy

Before Kubernetes, deploying an app meant SSH-ing into servers and running commands manually. If a server died, so did your app.

Kubernetes introduces a Shipping Port Manager model:

  • Container (Docker image): A shipping container โ€” standardized, portable.
  • Pod: A crane holding one or more containers together on the same network.
  • Node: A cargo ship (server) that carries Pods.
  • Control Plane: The manager in the tower. She says "put 3 cranes on Ship A." If Ship A sinks, she moves them to Ship B.

You never talk to ships directly. You talk to the manager.


๐Ÿ”ข Nodes, Pods, and Deployments: The Object Hierarchy

Pod (The Atom)

The smallest schedulable unit. Usually runs one container (your app). Sometimes includes sidecar containers โ€” helpers that share the same IP and disk (e.g., log forwarders, service mesh proxies).

Node (The Worker Machine)

A physical or virtual machine running the Pods. Each Node runs:

  • kubelet โ€” the agent that talks to the Control Plane.
  • kube-proxy โ€” handles network rules.
  • Container runtime (Docker, containerd).

Deployment (The Blueprint)

You don't create Pods directly. You write a Deployment:

apiVersion: apps/v1
kind: Deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    spec:
      containers:
        - name: nginx
          image: nginx:1.25

K8s creates 3 Pods. If one dies, K8s creates a replacement automatically.

Service (The Stable Phone Number)

Pods die and change IPs. A Service gives a stable virtual IP to a set of Pods.

flowchart TD
    User --> Service["Service\n(ClusterIP: 10.0.0.5)"]
    Service --> Pod1["Pod 1"]
    Service --> Pod2["Pod 2"]
    Service --> Pod3["Pod 3"]

The Service load-balances incoming traffic across all healthy Pods โ€” even as Pods restart and get new IPs.


โš™๏ธ The Control Loop: Desired State โ†’ Current State โ†’ Reconcile

This is the core mechanism behind everything Kubernetes does.

flowchart LR
    YAML["Desired State\n(replicas: 3)"] --> CP["Control Plane\n(kube-controller-manager)"]
    CP --> Check["Observe Current State\n(replicas: 2 โ€” one crashed)"]
    Check --> Act["Reconcile:\nStart 1 new Pod"]
    Act --> CP

The loop never stops. Every few seconds:

  1. Read the desired state from etcd (the cluster database).
  2. Observe the current state (running Pods, health checks).
  3. If they differ โ†’ act (start, stop, or reschedule Pods).

This is why Kubernetes is called declarative: you describe what you want, not how to do it. K8s figures out the "how."


๐Ÿง  Services, ConfigMaps, and the Network Model

ConfigMaps and Secrets

Decouple configuration from container images:

ResourcePurposeExample
ConfigMapNon-sensitive configDATABASE_URL=postgres://...
SecretSensitive credentialsAPI_KEY=abc123 (base64-encoded)
PersistentVolumeDurable disk storageDatabase data directories

Networking

Every Pod gets a unique cluster-internal IP. Pods talk to each other via Service DNS names:

http://payment-service.default.svc.cluster.local:8080

No hardcoded IPs. The DNS name resolves to the Service's virtual IP, which load-balances to healthy Pods.


โš–๏ธ When Kubernetes Is Overkill

ContextUse KubernetesUse Something Simpler
10+ microservices needing auto-scalingโœ…โ€”
Self-healing across node failures requiredโœ…โ€”
Single-service app with steady trafficโ€”Docker Compose or a single VM
Small team, no K8s expertiseโ€”PaaS (Heroku, Railway, Fly.io)
Serverless workloadsโ€”Lambda / Cloud Run

The real cost of Kubernetes is operational complexity: certificates, RBAC, networking policies, pod disruption budgets, and cluster upgrades. Size your investment to your scale.


๐Ÿ“Œ Summary

  • Pods wrap containers and are the atomic unit of scheduling.
  • Deployments declare desired replica count. K8s maintains it continuously.
  • Services give stable IPs to ephemeral Pods and load-balance traffic.
  • The Control Loop: desired state โ†’ observe current state โ†’ reconcile. This never stops.
  • etcd is the cluster's source of truth โ€” all desired state is stored there.

๐Ÿ“ Practice Quiz

  1. You set replicas: 3 in a Deployment and one Pod crashes. What does Kubernetes do?

    • A) Alerts an on-call engineer.
    • B) Starts a new Pod automatically to reach the desired count of 3.
    • C) Waits for you to restart it manually.
      Answer: B
  2. Why don't you connect to Pods directly by IP?

    • A) Pod IPs are external-only.
    • B) Pods are ephemeral and change IPs when restarted; a Service provides a stable endpoint.
    • C) K8s encrypts Pod IPs.
      Answer: B
  3. What is etcd in a Kubernetes cluster?

    • A) A container runtime.
    • B) A distributed key-value store that holds the desired state of the cluster.
    • C) A load balancer for Services.
      Answer: B

Abstract Algorithms

Written by

Abstract Algorithms

@abstractalgorithms