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:
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 doweb: 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:
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: 80You send it to the cluster with the command-line tool kubectl:
kubectl apply -f web.yaml
kubectl get podsSample output:
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 12sA 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:
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/helmcommands 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
kubectlincludes 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?
- Version 8
- The eight letters between K and s
- Eight core components
- Port 8080
How do you normally ask Kubernetes to run three copies of an app?
- Run
docker runthree times on each node - Store a desired state (e.g.
replicas: 3) and let controllers reconcile it - SSH into a node and start the processes
- Edit etcd directly
A container in a Deployment crashes. What happens?
- Nothing until an admin intervenes
- The whole Deployment is deleted
- A controller notices fewer replicas than desired and replaces it
- The cluster shuts down