Kubernetes Field Guide · Chapter 13 of 16
Running your own cluster: K3s on bare metal
Managed Kubernetes hides the control plane and charges you for the convenience. Sometimes that is exactly right; sometimes the platform itself is the constraint. Learn what K3s is, how to run it highly available on your own servers, and the real trade from a migration off Azure AKS.
- 40 min
- Advanced
- Hands-on
- Free
By Joshua Sequeira, founding engineer running Kubernetes in production. Updated .
By the end you can
0 of 5
Live lab in this chapter
A real-feeling terminal and a live cluster map, right in your browser. Every command block below has a ▶ Run in lab button.
Managed or self-run
On EKS, AKS or GKE, the provider runs the control plane (chapter 2) and much of the plumbing. You pay a cluster fee and hyperscaler prices for nodes, load balancers, disks and traffic. On your own servers, all of that becomes yours. Switch between them:
Interactive
Who is on call for what?
jobs that are yours:
Self-running is the right call when the platform blocks something you need, or when the bill is large enough that engineering time is cheaper. It is the wrong call for a small team that just wants to ship an ordinary web app.
What K3s is
K3s is a fully conformant Kubernetes distribution packaged as a single small binary. Everything you have used so far works unchanged. What makes it practical for small teams is what it bundles:
| Component | K3s default | Note |
|---|---|---|
| Container runtime | containerd | Same as most clusters |
| Network plugin | Flannel, with a network policy controller | Network policies work out of the box |
| Ingress | Traefik | Disable it with --disable traefik to bring your own |
| Load balancer | ServiceLB | Exposes LoadBalancer Services on node IPs |
| Storage | local-path provisioner | Data lives on one node's disk |
| Datastore | SQLite (one server) or embedded etcd (HA) | Or an external Postgres or MySQL |
Servers, agents and high availability
K3s has servers (control plane, and by default they run workloads too) and agents (workers only). For high availability, run three servers with embedded etcd, so the cluster survives losing any one of them, and put a stable address in front of the API.
curl -sfL https://get.k3s.io | sh -s - server --cluster-init --tls-san k8s.example.com
sudo cat /var/lib/rancher/k3s/server/node-token
--cluster-init- Start embedded etcd so more servers can join.
--tls-san- Add the load-balanced API hostname to the certificate.
node-token- The shared secret other nodes use to join.
curl -sfL https://get.k3s.io | K3S_TOKEN=<token> sh -s - server --server https://k8s.example.com:6443
curl -sfL https://get.k3s.io | K3S_URL=https://k8s.example.com:6443 K3S_TOKEN=<token> sh -
Why three, and not two? etcd only accepts a change when a majority of servers agree. Pick a number of servers and stop some:
Interactive · High availability
How many servers do you need?
Click a server to stop it. Agents (workers) keep running pods either way.
Day two is where the work is:
- Backups. With embedded etcd, K3s takes scheduled snapshots. Copy them off the cluster (to S3 or R2), and practise a restore before you need one.
- Upgrades. Upgrade one minor version at a time, servers first. The system-upgrade-controller automates this with plans you keep in Git.
- Certificates. K3s certificates last a year and are renewed when the service restarts close to expiry. A cluster that never restarts can still expire.
- Storage. local-path dies with its node. Keep state in managed services, or add replicated storage such as Longhorn.
From AKS to bare-metal K3s
The trade was explicit: a self-managed K3s fleet made version upgrades and certificate rotation the team's job, and spreading services across Cloudflare and Hetzner meant more dashboards, credentials and failure domains to watch. Read the full case study.
What you save, what you take on
| You stop paying for | You start paying with |
|---|---|
| The managed control plane fee | Running and upgrading the control plane yourself |
| Hyperscaler compute prices | Hardware failures, replacements and capacity planning |
| Per-load-balancer and per-disk charges | Building load balancing and storage yourself |
| Much of the egress bill | On-call for layers the provider used to own |
A useful test before moving: price the engineering hours per month the new platform will need, add them to the new bill, and compare. If the gap is still large, or the managed platform cannot do what you need at all, self-running is worth it.
Hands-on: a multi-node K3s cluster with k3d
The lab is docked at the bottom of your screen.
Live lab · no install
A multi-node K3s cluster
Create it, route to an app with the built-in Traefik, then pull the plug on a node.
- Tasks0/6
Create a three-node K3s cluster
k3d runs K3s nodes as Docker containers: one server, two agents.
k3d cluster create learn-k3s --agents 2 -p "8081:80@loadbalancer"Look at what came in the box
Tick "system" above the map: Traefik, a service load balancer, local-path storage, metrics-server and CoreDNS, all preinstalled.
kubectl get nodes kubectl get pods -ADeploy an app across the agents
kubectl create deployment shop --image=hashicorp/http-echo:1.0.0 --replicas=4 -- /http-echo -text=shop-on-k3s kubectl expose deployment shop --port=80 --target-port=5678Route to it with the bundled Traefik
No Helm install needed: Traefik is the default IngressClass, and port 8081 is mapped to it.
kubectl create ingress shop --rule="/*=shop:80" curl localhost:8081Pull the plug on an agent
Stop one node's container. Watch the map: NotReady, then its pods are replaced on the other nodes (compressed from about five minutes).
docker stop k3d-learn-k3s-agent-1 kubectl get nodesFour replicas again, all on healthy nodes. That is the job of the node lifecycle controller.
Bring it back and drain it properly
For planned maintenance, drain first so pods move before the node goes.
docker start k3d-learn-k3s-agent-1 kubectl drain k3d-learn-k3s-agent-0 --ignore-daemonsets kubectl uncordon k3d-learn-k3s-agent-0- Lab complete. Keep experimenting, or press Reset to start again.
- ready
- starting / not ready
- failing
- terminating
Prefer your own machine? The same steps on a local cluster
k3d runs K3s nodes as Docker containers, just as kind does for upstream Kubernetes.
brew install k3d
k3d cluster create learn-k3s --agents 2 -p "8081:80@loadbalancer"
kubectl get nodes
kubectl get pods -A
NAME STATUS ROLES AGE VERSION
k3d-learn-k3s-agent-0 Ready <none> 30s v1.33.4+k3s1
k3d-learn-k3s-agent-1 Ready <none> 30s v1.33.4+k3s1
k3d-learn-k3s-server-0 Ready control-plane,master 35s v1.33.4+k3s1--agents 2- One server and two agents.
-p "8081:80@loadbalancer"- Maps localhost:8081 to the built-in Traefik, so Ingresses work without port-forward.
get pods -A- Look for traefik, svclb, local-path-provisioner, metrics-server and coredns: the bundle from the table above.
Your kubectl now points at this cluster. Switch back to the kind cluster any time with kubectl config use-context kind-learn.
Mini mission
Route to an app with the bundled Traefik
On the K3s cluster, deploy the shop echo app from chapter 8 and reach it on http://localhost:8081 through an Ingress, with no Helm installs.
Reveal one safe solution
kubectl create deployment shop --image=hashicorp/http-echo:1.0.0 -- /http-echo -text=shop-on-k3s
kubectl expose deployment shop --port=80 --target-port=5678
kubectl create ingress shop --rule="/*=shop:80"
curl localhost:8081
K3s's Traefik is the default IngressClass, so the Ingress needs no class name. When you are done, delete the cluster with k3d cluster delete learn-k3s.
Check yourself
Quiz
0 of 4 answered