Skip to Content

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 , founding engineer running Kubernetes in production. Updated .

bare-metal-1k3s serverAPI serverSQLite or etcdschedulercontrollersTraefikCoreDNSlocal-pathcontainerdbare-metal-2k3s agentpodpodpodbare-metal-3k3s agentpodpodpodtoken
The chapter in one pictureK3s is all of Kubernetes in one small binary, with the parts you need already installed.

By the end you can

0 of 5

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:

    ComponentK3s defaultNote
    Container runtimecontainerdSame as most clusters
    Network pluginFlannel, with a network policy controllerNetwork policies work out of the box
    IngressTraefikDisable it with --disable traefik to bring your own
    Load balancerServiceLBExposes LoadBalancer Services on node IPs
    Storagelocal-path provisionerData lives on one node's disk
    DatastoreSQLite (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.

    On the first server
    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.
    On servers 2 and 3, then on each agent
    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 forYou start paying with
    The managed control plane feeRunning and upgrading the control plane yourself
    Hyperscaler compute pricesHardware failures, replacements and capacity planning
    Per-load-balancer and per-disk chargesBuilding load balancing and storage yourself
    Much of the egress billOn-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

    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.

    kind-learn
    1. Tasks0/6
    2. 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"
    3. 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 -A
    4. Deploy 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=5678
    5. Route 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:8081
    6. Pull 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 nodes

      Four replicas again, all on healthy nodes. That is the job of the node lifecycle controller.

    7. 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
    8. Lab complete. Keep experimenting, or press Reset to start again.
    ↑↓ history · Tab complete · Ctrl+C stop
    Cluster map live
    • 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.

    Terminal
    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
    Terminal
    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

    1. 1. Why run 3 K3s server nodes instead of 2 for high availability?
    2. 2. Which of these does K3s NOT include by default?
    3. 3. In the AKS to K3s migration described here, what drove the move?
    4. 4. What is the biggest hidden cost of a self-run cluster?

    Further reading