Lesson 01 beginner

Introduction to Kubernetes

What Kubernetes is, the problems it solves that Docker alone does not, the five objects you meet on day one and how to decide if you need it at all.

Kubernetes is a system that runs containers across a group of machines and keeps them running. You do not tell it how to start things. You write down what you want (“three copies of this image, reachable on port 80”) and Kubernetes works continuously to make the cluster match that description. That idea, called desired state, is the key to everything else in this course.

The problem Kubernetes solves

Starting one container is easy: docker run and you are done. The hard part starts the moment that container matters:

  • The process crashes at 3 a.m. Who restarts it?
  • The server it runs on dies. Where does the app go now?
  • Traffic doubles. How do you add copies, and how do clients find them?
  • You ship version 2. How do you replace version 1 without a minute of errors?
  • Ten services need to call each other. How do they find each other when IP addresses keep changing?

Teams used to answer these questions with scripts, cron jobs and runbooks. Kubernetes answers all of them with one consistent model, and it does the work automatically.

Desired state in action

Imagine an online shop API that runs three replicas spread across three servers. One server loses power.

  1. Kubernetes notices the server stopped reporting and marks it as not ready.
  2. A controller sees that only two of the three replicas you asked for exist.
  3. It creates a replacement, and the scheduler places it on a healthy server.
  4. Once the new copy passes its health check, it starts receiving traffic.

Nobody was paged. You never wrote “if a server dies, do X”. You only said “I want three”, and the system keeps that promise.

Kubernetes vs Docker Compose

If you finished the Docker Tutorial, you may know Docker Compose. Both describe multi-container apps in YAML, but they work at different scales.

Docker ComposeKubernetes
Runs onOne machineMany machines (a cluster)
Machine diesEverything on it is downWorkloads move to other nodes
Rolling updatesManualBuilt in, with rollback
AutoscalingNoYes, on CPU, memory or custom metrics
Self-healingRestart policy onlyReplaces failed containers and nodes
Learning curveAn afternoonWeeks to feel comfortable
Best forLocal development, single serversProduction across several servers

Docker and Kubernetes are not rivals. Docker (or any OCI tool) builds the image; Kubernetes runs it. Since version 1.24 Kubernetes no longer uses Docker Engine as its runtime and talks to containerd or CRI-O instead, but your Docker-built images run unchanged because they follow the same OCI image standard.

The five objects you need on day one

Kubernetes has dozens of object types. You can do a lot with only five:

  • Cluster: the whole system, a control plane plus worker machines.
  • Node: one machine (virtual or physical) that runs your containers.
  • Pod: the smallest unit Kubernetes runs, one or more containers that share a network address.
  • Deployment: keeps a number of identical Pods running and replaces them gradually when you ship a new version.
  • Service: a stable name and IP address in front of a changing set of Pods.

Your first application

Save this as web.yaml. It holds two objects separated by ---: a Deployment that asks for three nginx Pods, and a Service that gives them one stable address.

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
---
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 80

Apply it on any cluster (lesson 3 shows how to create one in a minute) and watch self-healing happen:

$ kubectl apply -f web.yaml
deployment.apps/web created
service/web created
$ kubectl get pods
NAME                   READY   STATUS    RESTARTS   AGE
web-7c5b8d9f6d-4xk2p   1/1     Running   0          12s
web-7c5b8d9f6d-9hq7m   1/1     Running   0          12s
web-7c5b8d9f6d-tz8wl   1/1     Running   0          12s
$ kubectl delete pod web-7c5b8d9f6d-4xk2p
pod "web-7c5b8d9f6d-4xk2p" deleted
$ kubectl get pods
NAME                   READY   STATUS    RESTARTS   AGE
web-7c5b8d9f6d-9hq7m   1/1     Running   0          40s
web-7c5b8d9f6d-tz8wl   1/1     Running   0          40s
web-7c5b8d9f6d-wq5rn   1/1     Running   0          3s

You deleted a Pod and a new one appeared three seconds later with a different name. The Deployment saw two Pods where you asked for three and fixed it.

How a cluster is arranged

A cluster has two halves:

  • Control plane: the brain. The API server receives every request, etcd stores the cluster state, the scheduler chooses a node for each new Pod, and the controller manager runs the loops that fix differences between desired and actual state. On a cloud provider, a cloud controller manager also creates load balancers and disks.
  • Worker nodes: the muscle. Each node runs the kubelet (an agent that starts Pods), kube-proxy (network rules for Services) and a container runtime such as containerd.

Good to know: if the control plane goes down, Pods that are already running keep serving traffic. What stops is change: no deploys, no scaling and no self-healing until it comes back. Lesson 2 goes through every component.

Do you actually need Kubernetes?

Kubernetes is powerful and also expensive in attention. Be honest about your situation.

You probably do not need it yet if you run one or two servers, a small team ships a few times a month, or a managed platform (a PaaS, serverless functions, a container service) already covers your needs.

You probably do need it when you run many services that deploy independently, zero-downtime deploys and autoscaling are routine requirements, you manage several environments, or a platform team can own the cluster.

The classic failure is a small team that spends more time operating the cluster than building the product. Managed services such as EKS, GKE and AKS reduce that cost, but they do not remove it.

Quick reference

TaskCommand
Create or update objects from a filekubectl apply -f web.yaml
List Podskubectl get pods
Details and events of a Podkubectl describe pod <name>
Container logskubectl logs <name>
Shell inside a containerkubectl exec -it <name> -- sh
Change the number of replicaskubectl scale deployment web --replicas=5
Everything in the namespacekubectl get all
Machines in the clusterkubectl get nodes

Exercise

Which object gives a changing set of Pods one stable name and IP address?

  1. Deployment
  2. Node
  3. Service
  4. ReplicaSet
Show answer

Answer: C. Pods are replaced all the time and get new IPs. A Service selects them by label and keeps one stable virtual IP and DNS name in front of them.

Exercise

Your Docker Compose stack runs on a single server, and that server loses power. What happens to the app?

  1. It is down until the server comes back, because Compose cannot move containers to another machine
  2. Compose restarts it on a second server automatically
  3. restart: always moves the containers to a healthy host
  4. Docker Hub serves the app until the server returns
Show answer

Answer: A. Compose manages containers on one machine only. Rescheduling work onto other machines after a failure is exactly what a cluster orchestrator like Kubernetes adds.

Key takeaways

  • Kubernetes is declarative: you describe the desired state and controllers keep the cluster matching it.
  • It handles crashes, node failures, scaling, rolling updates and service discovery for you.
  • Docker Compose fits one machine; Kubernetes fits a fleet of machines.
  • Docker builds images, Kubernetes runs them, and OCI images work with both.
  • Start with five objects: Cluster, Node, Pod, Deployment and Service.
  • A control plane outage stops changes, not traffic.
  • If a managed platform covers your needs, Kubernetes may be more than you need today.

FAQ

What is Kubernetes in simple terms?

It is a system that runs your containers on a group of machines and keeps them in the state you described: the right number of copies, healthy, reachable and on the right version.

Should I learn Docker before Kubernetes?

Yes. Kubernetes runs container images, so you need to know how images are built, tagged and run. The Docker Tutorial covers everything you need in nine lessons.

Is Kubernetes overkill for a small project?

Often, yes. A single server with Docker Compose, or a managed platform, is cheaper to run and easier to debug. Learn Kubernetes anyway if your career points toward platform or DevOps work, because most companies at scale use it.

What does the name “k8s” mean?

It is a numeronym: the letter k, the eight letters “ubernete” and the letter s. People use it because it is shorter to type.

How long does it take to learn Kubernetes?

You can deploy and expose an application after a weekend with this course. Feeling comfortable debugging scheduling, networking and storage problems usually takes a few months of hands-on use.

↑ ↓ navigate Enter open Esc close