Skip to content
System design on Kubernetes · Chapter 2 of 20 Day 0: The ground – from the legacy system to k3s

Day 0: The ground – from the legacy system to k3s

Goal: Tear down the three VMs of the predecessor course, build a k3s cluster with three zones from them, set up a registry and build kurz and zaehler as container images · Duration: approx. 75 min · Prerequisite: three Ubuntu VMs (or the lab from “System design hands-on”)

Briefing

  • In “System design hands-on”, the URL shortener kurz grew from a single instance into a distributed system: HAProxy in front, Valkey as a cache, PostgreSQL with a replica and PgBouncer, NATS for clicks, a counter service, rate limits, timeouts, failover via keepalived. All by hand, with systemd and configuration files.
  • This course moves the same system to Kubernetes. Every component gets its Kubernetes form, and you measure every decision again with k6. Wherever the predecessor course has a number, you compare.
  • The ground is the same three VMs, now as a k3s cluster: s1 is the server (control plane) and workstation, s2 and s3 are agents. Each VM is its own zone. That way you can practise a zone outage on Day 13 by shutting down a VM.
  • You don’t need the predecessor course. Drill 1 tears down its lab in case it is still running; with three fresh VMs, you skip it.
VM IP Role Zone In the lab
s1 172.16.0.126 k3s server, registry, k6, workstation zone-a 4 vCPU, 11 GB
s2 172.16.0.228 k3s agent zone-b 2 vCPU, 4 GB
s3 172.16.0.83 k3s agent zone-c 2 vCPU, 4 GB

Where the components of the legacy system move to:

Legacy system (VM) Kubernetes form Day
kurz as a systemd service Deployment, Service, probes 1
HAProxy with health checks Gateway API with Envoy Gateway 3
Valkey on s3 Valkey via operator 5
PostgreSQL with replica and PgBouncer CloudNativePG with replicas and pooler 6, 7
keepalived failover Failover by the operator 7
NATS JetStream and zaehler NATS via Helm, streams via NACK, zaehler as a Deployment 8
a second kurz instance by hand HPA and KEDA 9, 10
Rate limits in HAProxy Rate limits in the gateway 11
Timeouts and retries in code and HAProxy Linkerd 12

Drill 1: Tearing down the legacy system

Only if the lab from the predecessor course is still running. First back up what you want to keep: the configuration and the data. After the predecessor course, the database holds a good 2.7 million links.

# on s3
# How many links does the old database have?
sudo -u postgres psql -d kurz -Atc "select count(*) from links"
# Back up the kurz database in custom format (pg_restore reads it on Day 1)
sudo -u postgres pg_dump -Fc -d kurz -f /tmp/kurz.dump
# Size of the backup
ls -la /tmp/kurz.dump

Expected:

2727215
-rw-rw-r-- 1 postgres postgres 31654924 Nov 24 15:46 /tmp/kurz.dump

Fetch the dump to s1 and back up the old configuration of all three nodes in ~/sd-archiv – Day 1 restores the dump from there; you only need the archives if you want to look something up later:

# on s1
# Folder for the dump and the old configuration
mkdir -p ~/sd-archiv
# Fetch the dump from s3
scp s3:/tmp/kurz.dump ~/sd-archiv/
# Back up each node's configuration as an archive (tar skips folders that don't exist there)
for h in s1 s2 s3; do
  ssh $h "sudo tar czf - --ignore-failed-read /etc/haproxy /etc/keepalived /etc/nats /etc/pgbouncer /etc/postgresql /etc/valkey /etc/systemd/system/kurz.service /etc/systemd/system/kurz.service.d 2>/dev/null" > ~/sd-archiv/sd-etc-$h.tgz
done
# Contents of the folder
ls -l ~/sd-archiv

Expected (abridged):

-rw-rw-r-- 1 ubuntu ubuntu 31654924 Nov 24 15:47 kurz.dump
-rw-r--r-- 1 ubuntu ubuntu     2321 Nov 24 15:46 sd-etc-s1.tgz
-rw-r--r-- 1 ubuntu ubuntu    15391 Nov 24 15:47 sd-etc-s2.tgz
-rw-r--r-- 1 ubuntu ubuntu    15071 Nov 24 15:47 sd-etc-s3.tgz

Then stop the services and remove the packages that would occupy ports:

# on s1
# Stop kurz and HAProxy and keep them from starting at boot
sudo systemctl disable --now kurz haproxy
# Remove the unit file and drop-ins of kurz
sudo rm -rf /etc/systemd/system/kurz.service /etc/systemd/system/kurz.service.d
# Remove HAProxy including its configuration
sudo apt-get purge -y -qq haproxy
# Make systemd forget the deleted units
sudo systemctl daemon-reload
# on s2
# Stop and disable all services of the legacy system on s2
sudo systemctl disable --now kurz zaehler nats keepalived postgresql
# Remove the custom units of kurz, zaehler and NATS
sudo rm -rf /etc/systemd/system/kurz.service* /etc/systemd/system/zaehler.service* /etc/systemd/system/nats.service*
# Remove the keepalived and PostgreSQL packages
sudo apt-get purge -y -qq keepalived "postgresql*"
# Delete data, configuration and the binaries installed by hand
sudo rm -rf /var/lib/postgresql /etc/postgresql /usr/local/bin/nats-server /usr/local/bin/kurz /usr/local/bin/zaehler
# Make systemd forget the deleted units
sudo systemctl daemon-reload
# Is anything still listening on the ports of PostgreSQL, NATS or kurz?
ss -tlnp | grep -E ":(5432|4222|8080)" || echo frei
# on s3
# Stop and disable all services of the legacy system on s3
sudo systemctl disable --now valkey pgbouncer keepalived postgresql
# Remove the keepalived, PgBouncer and PostgreSQL packages
sudo apt-get purge -y -qq keepalived pgbouncer "postgresql*"
# Delete data, configuration, the Valkey unit and the Valkey binaries
sudo rm -rf /var/lib/postgresql /etc/postgresql /etc/systemd/system/valkey.service* /usr/local/bin/valkey-* /etc/valkey /var/lib/valkey
# Make systemd forget the deleted units
sudo systemctl daemon-reload
# Is anything still listening on the ports of PostgreSQL, PgBouncer or Valkey?
ss -tlnp | grep -E ":(5432|6432|6379)" || echo frei

Expected on s2 and on s3:

frei

(frei means “free”: no process listens on these ports any more.) The source code of kurz and zaehler stays in ~/kurz and ~/zaehler on s1. If you no longer have it, it is in the companion repo of the predecessor course (github.com/itdrills/system-design-zum-anfassen).

Save your progress? Sign up for free

Drill 2: The k3s server

You install k3s with its official installer. It creates the binary, the systemd unit and the kubeconfig. Read it first; it is a shell script with a good 1200 lines:

# on s1
# Download the installer without running it
curl -sfL https://get.k3s.io -o /tmp/k3s-install.sh
# How long is it? (then read it with less)
wc -l /tmp/k3s-install.sh

Expected:

1218 /tmp/k3s-install.sh

Three switches for the server: without Traefik (Envoy Gateway comes on Day 3), with the zone label, and with a kubeconfig your user is allowed to read.

# on s1
# k3s server in a fixed version: without Traefik, with zone label, kubeconfig readable by everyone
curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=v1.37.2+k3s1 sh -s - server \
  --disable traefik --node-label topology.kubernetes.io/zone=zone-a --write-kubeconfig-mode 644

Expected (end):

[INFO]  systemd: Creating service file /etc/systemd/system/k3s.service
[INFO]  systemd: Enabling k3s unit
Created symlink /etc/systemd/system/multi-user.target.wants/k3s.service → /etc/systemd/system/k3s.service.
[INFO]  systemd: Starting k3s

k3s ships kubectl; you add Helm, and Helm needs the kubeconfig as a variable:

# on s1
# Architecture of the VM (amd64 or arm64) for the download
ARCH=$(dpkg --print-architecture)
# Download the Helm archive and unpack only the binary to /tmp
curl -sL https://get.helm.sh/helm-v4.3.2-linux-${ARCH}.tar.gz | tar xz -C /tmp linux-${ARCH}/helm
# Install the binary to /usr/local/bin
sudo install /tmp/linux-${ARCH}/helm /usr/local/bin/helm
# Set the kubeconfig for every new shell ...
echo "export KUBECONFIG=/etc/rancher/k3s/k3s.yaml" >> ~/.bashrc
# ... and for this one
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
# Check the versions
k3s --version | head -1
helm version --short

Expected:

k3s version v1.37.2+k3s1 (f6973cb8)
v4.3.2+g8346aaa

Save your progress? Sign up for free

Drill 3: The agents

An agent needs the address of the server and its token. You read the token on s1:

# on s1
# Token the agents use to join the server
sudo cat /var/lib/rancher/k3s/server/node-token

The output starts with K10. Put it into a file on s2 and s3 that only root can read, and pass it to the installer via K3S_TOKEN_FILE:

# on s2 (on s3 the same with zone-c)
# Create an empty file that only root may read
sudo install -m 600 /dev/null /root/k3s-token
# Write the token into it
echo 'K10…' | sudo tee /root/k3s-token >/dev/null   # the whole token from s1 instead of K10…
# Install k3s as an agent: server address, token from the file, zone label
curl -sfL https://get.k3s.io | sudo INSTALL_K3S_VERSION=v1.37.2+k3s1 K3S_URL=https://172.16.0.126:6443 \
  K3S_TOKEN_FILE=/root/k3s-token sh -s - agent --node-label topology.kubernetes.io/zone=zone-b

Expected (last line):

[INFO]  systemd: Starting k3s-agent

Happened during testing: The first attempt passed the token as K3S_TOKEN=… in the middle of a nested SSH call and aborted with hostname contains invalid characters without creating an agent. With the token in a file, it worked right away. The file has a second advantage: the token does not end up in the shell history.

Back on s1:

# on s1
# Nodes with their zone
kubectl get nodes -L topology.kubernetes.io/zone
# What is already running in the cluster?
kubectl get pods -A

Expected:

NAME   STATUS   ROLES           AGE   VERSION        ZONE
s1     Ready    control-plane   14m   v1.37.2+k3s1   zone-a
s2     Ready    <none>          12m   v1.37.2+k3s1   zone-b
s3     Ready    <none>          11m   v1.37.2+k3s1   zone-c
NAMESPACE     NAME                                      READY   STATUS      RESTARTS   AGE
kube-system   coredns-577d995dff-cxkxk                  1/1     Running     0          14m
kube-system   helm-install-gateway-api-crd-4lkrs        0/1     Completed   0          14m
kube-system   local-path-provisioner-56c6584955-6qngz   1/1     Running     0          14m
kube-system   metrics-server-778c4649b4-69hrc           1/1     Running     0          14m

Three nodes, three zones. k3s ships CoreDNS, a metrics-server and local-path as storage: volumes are directories on the node where the pod runs. That is enough for the lab, but it has consequences you will feel on Day 7. And it ships something you did not order:

# on s1
# Helm releases in all namespaces
helm list -A
# Version and channel of the Gateway API CRDs from their annotations
kubectl get crd gateways.gateway.networking.k8s.io -o jsonpath='{.metadata.annotations.gateway\.networking\.k8s\.io/bundle-version}{" "}{.metadata.annotations.gateway\.networking\.k8s\.io/channel}{"\n"}'

Expected:

NAME            NAMESPACE   REVISION    UPDATED                                 STATUS      CHART                   APP VERSION
gateway-api-crd kube-system 1           2026-11-24 15:51:10.930819708 +0000 UTC deployed    gateway-api-crd-1.6.301 v1.6.3
v1.6.3 standard

k3s installs the Gateway API CRDs in version 1.6.3, channel standard. Remember this for Day 3.

Save your progress? Sign up for free

Drill 4: A registry for your own images

kurz and zaehler are not on any public registry. You build them on s1 with Docker and push them into your own registry, from which all nodes pull.

# on s1
# Docker from the Ubuntu packages
sudo apt-get install -y -qq docker.io
# Add user ubuntu to the docker group (takes effect at the next login)
sudo usermod -aG docker ubuntu
# Trust your own registry without TLS
echo '{"insecure-registries": ["172.16.0.126:5000"]}' | sudo tee /etc/docker/daemon.json
# Restart Docker so it reads the setting
sudo systemctl restart docker
# Registry as a container on port 5000, restarts after every reboot
sudo docker run -d --restart=always --name registry -p 5000:5000 registry:3
# List of repositories in the registry
curl -s http://172.16.0.126:5000/v2/_catalog

Expected (after Docker’s download lines):

{"repositories":[]}

The registry speaks HTTP without TLS. Docker allows that only for addresses listed under insecure-registries; otherwise docker push fails with http: server gave HTTP response to HTTPS client. The nodes need to know this too. For that, k3s reads /etc/rancher/k3s/registries.yaml, on every node:

# on s1, s2 and s3
# Directory for the k3s configuration
sudo mkdir -p /etc/rancher/k3s
# Mirror entry: pull images from 172.16.0.126:5000 over HTTP
printf 'mirrors:\n  "172.16.0.126:5000":\n    endpoint:\n      - "http://172.16.0.126:5000"\n' | sudo tee /etc/rancher/k3s/registries.yaml
# Restart k3s so it reads the file
sudo systemctl restart k3s        # on s2 and s3: k3s-agent

Caution: A registry without TLS and without authentication belongs only in an isolated lab. In any other network, anyone can push images into it that your cluster then runs.

Save your progress? Sign up for free

Drill 5: kurz and zaehler as images

A Dockerfile with two stages: in the first, Go builds the program; the second contains only the binary on a distroless image without a shell or package manager, running as user nonroot.

# on s1
# Working directory of the course; copy the source code of both programs into it
mkdir -p ~/sdk && cp -r ~/kurz ~/zaehler ~/sdk/
# Remove old binaries from the predecessor course
rm -f ~/sdk/kurz/kurz ~/sdk/zaehler/zaehler
# Change into it
cd ~/sdk
# Write the same Dockerfile for both programs; only the name changes
for app in kurz zaehler; do
cat > $app/Dockerfile <<EOF
FROM golang:1.27.4 AS bau
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY *.go ./
RUN CGO_ENABLED=0 go build -o /$app .

FROM gcr.io/distroless/static-debian13:nonroot
COPY --from=bau /$app /$app
USER nonroot
ENTRYPOINT ["/$app"]
EOF
done
# Look at the result for kurz
cat kurz/Dockerfile

Expected:

FROM golang:1.27.4 AS bau
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY *.go ./
RUN CGO_ENABLED=0 go build -o /kurz .

FROM gcr.io/distroless/static-debian13:nonroot
COPY --from=bau /kurz /kurz
USER nonroot
ENTRYPOINT ["/kurz"]

rm -f removes the old binaries from the predecessor course so they don’t end up in the image. CGO_ENABLED=0 builds a static binary that runs without a C library; only then does it work on distroless/static. Build and push:

# on s1
# Build both images; the name contains the registry
sudo docker build -q -t 172.16.0.126:5000/kurz:1.0 kurz
sudo docker build -q -t 172.16.0.126:5000/zaehler:1.0 zaehler
# Push them to the registry
sudo docker push -q 172.16.0.126:5000/kurz:1.0
sudo docker push -q 172.16.0.126:5000/zaehler:1.0
# Size of the images
sudo docker images --format "{{.Repository}}:{{.Tag}} {{.Size}}" | grep 5000
# Does the registry know both?
curl -s http://172.16.0.126:5000/v2/_catalog

Expected (hashes differ; Docker warns that the legacy builder is deprecated):

sha256:a83efef70b7f1fea5dc413984da0122bb056a0214611b1b95a0de2d1089ea894
sha256:4ff1e2cc94fd33c8c208f811bcdec11f982a69d5dd186989388880e5cee28d68
172.16.0.126:5000/kurz:1.0
172.16.0.126:5000/zaehler:1.0
172.16.0.126:5000/zaehler:1.0 31MB
172.16.0.126:5000/kurz:1.0 33.3MB
{"repositories":["kurz","zaehler"]}

About 33 MB for kurz, a good two thirds of it the Go binary including the pgx and NATS libraries.

Save your progress? Sign up for free

Drill 6: Can the cluster pull?

A test pod on s3, that is, on a node without Docker and without a local copy of the image:

# on s1
# Start a single pod with kurz:1.0 pinned to s3
kubectl run zugprobe --image=172.16.0.126:5000/kurz:1.0 --restart=Never --overrides='{"spec":{"nodeName":"s3"}}'
# Give the node time to pull and start
sleep 15
# Events of the pod: pulling, creating, starting
kubectl get events --field-selector involvedObject.name=zugprobe -o custom-columns=GRUND:.reason,MELDUNG:.message --no-headers
# Last log line of kurz
kubectl logs zugprobe | tail -1
# Delete the test pod again
kubectl delete pod zugprobe

Expected (abridged):

pod/zugprobe created
Pulling   Pulling image "172.16.0.126:5000/kurz:1.0"
Pulled    Successfully pulled image "172.16.0.126:5000/kurz:1.0" in 1.789s (1.79s including waiting). Image size: 9820736 bytes.
Created   Container created
Started   Container started
    172.16.0.83:5432 (s3): dial error: dial tcp 172.16.0.83:5432: connect: connection refused
pod "zugprobe" deleted from default namespace

The image arrives from s1 in under two seconds. Compressed, it is less than 10 MB. And kurz crashes: without KURZ_DB, it uses its default, the old database on s3:5432, and you tore that down in Drill 1. That is exactly where Day 1 begins.

Save your progress? Sign up for free

Exercise: What is where?

Answer each with one command on s1:

  1. On which node does CoreDNS run?
  2. Which zones does the cluster have, and which node belongs to which?
  3. Which images are in the registry?

Solution

# on s1
# 1: CoreDNS pods with their node (column NODE)
kubectl -n kube-system get pods -l k8s-app=kube-dns -o wide
# 2: Nodes with the zone label as a separate column
kubectl get nodes -L topology.kubernetes.io/zone
# 3: Repositories in the registry
curl -s http://172.16.0.126:5000/v2/_catalog

Save your progress? Sign up for free

Challenge

A colleague asks: “Why not simply mount the binaries from ~/kurz into an Ubuntu container with a hostPath? That saves the image.” Name three reasons against it, each with a reference to this course.

Solution

  1. The binary has to be on every node the pod can land on. On Days 9 and 13, the scheduler spreads pods across all zones; an image is pulled by each node itself from the registry.
  2. Versions. kurz:1.0 is a fixed, immutable unit. A rollout (Day 4) switches from one tag to the next and can go back; an overwritten binary on disk cannot.
  3. Attack surface and privileges. hostPath opens up the node’s file system, and an Ubuntu image brings a shell and a package manager. The distroless image contains only the binary and runs as nonroot.

Save your progress? Sign up for free

Checkpoint

# on s1
# Three nodes ready, each in its zone
kubectl get nodes -L topology.kubernetes.io/zone --no-headers | awk '{print $1, $2, $NF}'
# Both images in the registry
curl -s http://172.16.0.126:5000/v2/_catalog
# k6 from the predecessor course is still there
k6 version

Expected:

s1 Ready zone-a
s2 Ready zone-b
s3 Ready zone-c
{"repositories":["kurz","zaehler"]}
k6 v2.4.0 (commit/dd9395906d, go1.26.11, linux/amd64)

Interview note

  • Why Kubernetes for a system that already ran on three VMs? Not because of the three VMs, but because of what the predecessor course built by hand: health checks, failover, scaling, rollouts. Kubernetes and its operators turn these into declared states that heal themselves.
  • What doesn’t change? Physics. Latency, saturation, replication lag and Little’s Law apply in the cluster just the same. That’s why this course measures everything again.

Enterprise note

  • k3s is a full, certified Kubernetes in a single binary. For production, the server needs high availability (three servers with embedded etcd); here s1 is a single point of failure, and that is a topic on Day 13.
  • A real registry (Harbor, the registry of your Git platform) has TLS, authentication, vulnerability scans and retention rules. Images are built in CI, not on a node.

Contents

More