TExcelTAD
Home › Kubernetes › Kubernetes Intro

Kubernetes Intro

Kubernetes (often shortened to K8s: a "K", eight letters, an "s") is an open-source system that runs containers across a group of machines and keeps them running. You tell it what you want, for example "three copies of my web server, version 1.4, reachable on port 80", and Kubernetes works out how: which machine each copy goes on, what to do when a machine dies, how traffic finds the copies, and how to replace version 1.4 with 1.5 without downtime.

Kubernetes grew out of Google's internal cluster managers (Borg and Omega) and was released in 2014. Today it is maintained by the Cloud Native Computing Foundation (CNCF) and is the standard way to run containers in production on every major cloud.

Why not just run containers with Docker?

On one laptop, docker run is all you need. In production you quickly run into questions that a single container engine doesn't answer:

Problem What Kubernetes gives you
A container crashes at 3 a.m. It is restarted automatically (self-healing)
A whole server dies Its workloads are rescheduled onto healthy servers
Traffic doubles Add replicas with one command, or let an autoscaler do it
Copies come and go, IPs change A stable Service name and IP load-balances across them
Releasing a new version Rolling updates with automatic rollback
Config and passwords ConfigMaps and Secrets, injected at start-up
Many teams on one cluster Namespaces, quotas and access control (RBAC)

The core idea: desired state

The most important idea in this whole course is the reconcile loop. You never tell Kubernetes "start a container now". Instead you store a desired state (a YAML document) in the cluster. Small programs called controllers constantly compare the desired state with the actual state and take one step to close the gap, forever.

Here is that idea as a tiny Python model. The "cluster" wants 3 web replicas; one crashes, and the loop notices and repairs it:

python · try it yourself
desired = {"web": 3}
actual = {"web": 1}

def reconcile(desired, actual):
    for app, want in desired.items():
        have = actual.get(app, 0)
        if have < want:
            print(f"{app}: have {have}, want {want} -> start {want - have}")
            actual[app] = want
        elif have > want:
            print(f"{app}: have {have}, want {want} -> stop {have - want}")
            actual[app] = want
        else:
            print(f"{app}: ok ({have}/{want})")

reconcile(desired, actual)   # first pass: create the missing copies
actual["web"] -= 1           # a container crashes
reconcile(desired, actual)   # the loop repairs it
reconcile(desired, actual)   # nothing to do
web: have 1, want 3 -> start 2
web: have 2, want 3 -> start 1
web: ok (3/3)

Real controllers work exactly like this, only they read state from the Kubernetes API and talk to real machines.

What you write: manifests

You describe desired state in manifests, usually YAML files. This one asks for three copies of an nginx web server:

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: nginx
          image: nginx:1.27
          ports:
            - containerPort: 80

You send it to the cluster with the command-line tool kubectl:

bash
kubectl apply -f web.yaml
kubectl get pods

Sample output:

text
deployment.apps/web created
NAME                   READY   STATUS    RESTARTS   AGE
web-7c5ddbdf54-4xk2p   1/1     Running   0          12s
web-7c5ddbdf54-9qmzt   1/1     Running   0          12s
web-7c5ddbdf54-tl8wd   1/1     Running   0          12s

A manifest is plain data, so programs can read it too. Throughout this course we will use Python and the yaml module to inspect manifests and model what Kubernetes does with them:

python · try it yourself
import yaml

manifest = """
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 3
  template:
    spec:
      containers:
        - name: nginx
          image: nginx:1.27
"""
obj = yaml.safe_load(manifest)
print("kind:", obj["kind"])
print("name:", obj["metadata"]["name"])
print("replicas:", obj["spec"]["replicas"])
for c in obj["spec"]["template"]["spec"]["containers"]:
    print("container:", c["name"], "->", c["image"])
kind: Deployment
name: web
replicas: 3
container: nginx -> nginx:1.27

How this course works

Kubernetes itself can't run inside a browser tab, so the lessons use two kinds of blocks:

  • Real material in plain code blocks: YAML manifests, kubectl/helm commands and their sample output. Copy them into a real cluster (the Local Clusters lesson shows how to get one for free on your laptop).
  • Runnable Python models marked with ▶ Run. Each models one decision the cluster makes (which node a Pod lands on, which Pods a Service picks, how many replicas an autoscaler wants) so you can experiment and see the rules in action.

Note: Sample output from kubectl includes generated names (web-7c5ddbdf54-4xk2p) and ages that will differ on your cluster. The Python outputs are exact.

Test yourself

What does the "8" in K8s stand for?

  1. Version 8
  2. The eight letters between K and s
  3. Eight core components
  4. Port 8080

How do you normally ask Kubernetes to run three copies of an app?

  1. Run docker run three times on each node
  2. Store a desired state (e.g. replicas: 3) and let controllers reconcile it
  3. SSH into a node and start the processes
  4. Edit etcd directly

A container in a Deployment crashes. What happens?

  1. Nothing until an admin intervenes
  2. The whole Deployment is deleted
  3. A controller notices fewer replicas than desired and replaces it
  4. The cluster shuts down