ENع
←Kubernetes
Lesson 09 · Containers and Kubernetes

Kubernetes basics

14 min10 steps5 questionsIntermediate
About this lesson

Kubernetes runs containers across a cluster of machines and keeps them running. You write down the state you want in a manifest, such as three copies of a web server, and the control plane works to make it true. Follow one Deployment from a YAML file to running Pods, see a Service spread traffic over them, scale it with a one-character edit, and watch the cluster heal itself when a node dies.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 9 · Interactive
01 / 10

A cluster of machines

Kubernetes runs containers across a cluster: a group of machines called nodes. A control plane manages them. You normally do not say "start this here"; you say what you want, and Kubernetes keeps the real state matching it.

cluster = control plane + worker nodes

control plane
  API server   the front door
  etcd         the store of state
  scheduler    picks a node per Pod
  controllers  close the gap

nodes: Node 1, Node 2, Node 3
Loading figure…
or ← →
Read the lesson step by step
  1. A cluster of machines

    Kubernetes runs containers across a cluster: a group of machines called nodes. A control plane manages them. You normally do not say "start this here"; you say what you want, and Kubernetes keeps the real state matching it.

    cluster = control plane + worker nodes
    
    control plane
      API server   the front door
      etcd         the store of state
      scheduler    picks a node per Pod
      controllers  close the gap
    
    nodes: Node 1, Node 2, Node 3
  2. Write the desired state

    A manifest is a YAML file (a plain-text settings format) describing an object. This Deployment asks for three replicas (copies) of a Pod running the container image nginx:1.27. It states the goal, not the steps to reach it: that is declarative.

    web.yaml
    
    kind: Deployment
      replicas: 3            ← how many
      image: nginx:1.27       ← what to run
    
    declarative: say WHAT, not HOW
  3. Apply it

    kubectl, the Kubernetes command-line tool, runs apply -f web.yaml to send the manifest to the API server, the front door of the control plane. It checks the object and saves it in etcd, the cluster's database. Desired state is now 3 Pods; running is 0.

    $ kubectl apply -f web.yaml
    deployment.apps/web created
    
    kubectl → API server → etcd
    
    desired 3 · running 0
  4. Create and place Pods

    A controller sees 3 wanted, 0 existing, and creates 3 Pods. A Pod is the smallest unit Kubernetes runs: one or more containers sharing a network address. The scheduler then assigns each new Pod to a node.

    controllers: want 3, have 0 → create 3 Pods
      (Deployment → ReplicaSet → Pods)
    
    scheduler: Pod with no node? pick one
      web-7d4b9c8f5-x2k9p → Node 1
      web-7d4b9c8f5-m4tqz → Node 2
      web-7d4b9c8f5-b8wns → Node 3
    
    status: Pending
  5. The kubelet starts them

    Each node runs an agent called the kubelet. It sees a Pod assigned to its node, pulls the image nginx:1.27 and asks the container runtime (the program that runs containers) to start it. The Pods turn Running: 3 of 3.

    kubelet on each node:
      a Pod for me? pull the image
      nginx:1.27
      start the container
    
    $ kubectl get deployment web
    NAME   READY   UP-TO-DATE   AVAILABLE   AGE
    web    3/3     3            3           10s
  6. One address for all

    Pods come and go, and each has its own address. A Service gives them one stable name and IP. It finds the Pods by their label app: web, and each request goes to one of them.

    Service "web" → selector app: web
      stable cluster IP + DNS name
    
    request 1 → a web Pod
    request 2 → a web Pod
    request 3 → a web Pod
    (kube-proxy picks one, at random by default)
  7. Scale by editing

    More visitors? Change replicas: 3 to replicas: 4 and apply again. Nothing else changes. The controller sees 4 wanted, 3 existing, creates one more Pod, and the scheduler places it.

    -  replicas: 3
    +  replicas: 4
    $ kubectl apply -f web.yaml
    deployment.apps/web configured
    
    want 4, have 3 → create 1
    desired 4 · running 4
  8. A node fails

    Node 3 loses power. Its kubelet stops reporting, the node is marked not ready, and after a timeout its Pod is evicted. Desired is still 4, but only 3 are running. Nobody has to notice.

    Node 3: NotReady (no heartbeats)
      after 5 min (default) its Pods are evicted
    
    desired 4 · running 3
    gap = 1
  9. It heals itself

    The controller sees 4 wanted, 3 running, and creates a new Pod. The scheduler skips the dead node and places it on Node 2. Running is 4 again.

    controllers: want 4, have 3 → create 1
    scheduler: Node 3 not ready → Node 2
    
    desired 4 · running 4
    no one was paged
  10. The control loop

    This is the whole idea. You declare the desired state in a file. Controllers loop forever: watch the actual state, compare, act to close the gap. A thermostat works the same way.

    loop forever:
      observe   what is running?
      compare   desired vs actual
      act       create or delete Pods
    
    thermostat: set 21 °C, heater on/off

Quiz Unlocks after the last step.