Skip to content
← All work

Kubernetes the hard way, automated

Kubernetes from Scratch

A five-VM Kubernetes cluster built the hard way on Apple Silicon Macs, with no kubeadm. Ansible lays down the PKI, etcd, control plane, kubelets and networking, and checks the cluster's health between stages.

  • Kubernetes
  • Ansible
  • Terraform
  • Cilium
Cluster topology: kubectl on a laptop reaches haproxy on lb-1, which fronts the API servers on master-1 and master-2; Cilium connects them to worker-1 and worker-2, all running as Tart VMs on an Apple Silicon Mac.
VMs, from bare Macs
5VMs, from bare Macs
Ansible roles
21Ansible roles
retrying health gates
7retrying health gates
kubeadm
0kubeadm

Why I built it

kubeadm hides the parts of Kubernetes that break in production: certificates, etcd, the order components start in, and how a kubelet earns its identity. Building every piece by hand is the most direct way to learn what each one needs.

What it does

  • Prepares Apple Silicon Macs as VM hosts and installs Orchard to manage Tart VMs on them.
  • Bakes one Tart image per node with a static IP, then has Terraform create five VMs: master-1, master-2, worker-1, worker-2 and lb-1.
  • Builds a cluster certificate authority and signs a certificate for every component.
  • Installs etcd, the control plane, containerd and kubelets from upstream binaries as systemd units.
  • Puts haproxy on lb-1 in front of both API servers on port 6443.
  • Installs Cilium, CoreDNS and metrics-server, and gives the laptop a working kubectl.

How it works

Four playbooks and one Terraform apply run in order. bootstrap.yml prepares each Mac (Command Line Tools, a sudo user, optionally Tailscale). prerequisite.yml installs the Orchard controller, worker and CLI, sets up the Homebrew environment and creates a service account for Terraform. node-images.yml bakes a Tart image per node with its static IP. Terraform, in the separate k8sfs-terraform repo, then creates the VMs through Orchard's API. Finally site.yml builds the cluster with 14 roles.

Inventory comes from two places. In the DHCP environment, inventory/orchard_inv.py asks Orchard's REST API for /v1/vms and each VM's /v1/vms/<name>/ip, and groups hosts by name prefix. The static-IP environment uses a plain static inventory.

Build order. Playbooks run in sequence: bootstrap.yml for Command Line Tools, a sudo user and optional Tailscale; prerequisite.yml for the Orchard controller, worker, CLI and a Terraform service account; node-images.yml to bake one Tart image per node with static IPs; terraform apply from k8sfs-terraform to create the five VMs through Orchard's API; then site.yml. site.yml runs 14 roles in order: cluster_hosts, cluster_ssh, cluster_tls, cluster_config, cluster_encryption, etcd, control_plane, container_runtime, worker, cluster_rbac, kubectl_access, cluster_cni, cluster_dns and metrics_server. Seven roles end on a health gate: etcdctl member list after etcd, componentstatuses after the control plane, get nodes after the workers, get nodes from the laptop after kubectl access, Cilium rollout and Gateway Programmed after the CNI, CoreDNS rollout after DNS, and kubectl top nodes after metrics-server.

Certificates

master-1 creates a 10-year KUBERNETES-CA and a separate front-proxy CA with the community.crypto collection. It signs certificates for admin, the controller manager, the scheduler, the API server (with SANs for both masters and the load balancer), the API server's kubelet client, the etcd server, the service-account key pair and the front-proxy client. Secrets are encrypted at rest with a randomly generated key.

Kubelets don't get a pre-made certificate. They join with TLS bootstrapping: a bootstrap token lets each kubelet request its own certificates, the serving CSRs are approved automatically, and the play waits until every node's CSR is approved before it moves on. The two masters also run kubelets, tainted NoSchedule so workloads stay on the workers.

Who signs what. master-1 creates both certificate authorities with community.crypto. KUBERNETES-CA, valid for 10 years, signs kube-apiserver with SANs for both masters and lb-1, etcd-server, apiserver-kubelet-client, service-account for pod tokens, kube-controller-manager, kube-scheduler and admin for the laptop's kubectl. A separate front-proxy CA signs front-proxy-client for the aggregation layer that metrics-server uses. Kubelets on all four nodes join with TLS bootstrapping: a bootstrap token, CSRs to the API server, serving CSRs auto-approved while the play waits for every node, then the node registers and the gate runs kubectl get nodes.

Health gates

Seven checks retry until they pass, so a broken stage stops the run where it broke instead of failing three roles later: etcdctl member list, componentstatuses, kubectl get nodes on the cluster, kubectl get nodes again from the laptop, the Cilium rollout plus the Gateway reaching Programmed, the CoreDNS rollout, and kubectl top nodes.

Why this stack

Tart runs Apple's Virtualization framework, so the VMs are native on Apple Silicon, and Orchard adds a REST API that both Terraform and the dynamic inventory can use. Ansible fits a build where order matters and each stage can be run on its own by tag. Cilium covers pod networking, LoadBalancer IPs through L2 announcements and Gateway API in one install, which suits a lab with no cloud load balancer.

The hard part

Ordering. Certificates must exist before etcd, etcd before the API servers, the API servers before kubelets can bootstrap, and kubelets before the CNI can roll out. When a stage fails quietly, the error shows up much later somewhere unrelated. The retrying health gates turned that into fast, local failures: each stage proves the cluster works before the next one starts.

Results

The playbooks and one Terraform apply take bare Macs to a five-VM cluster with two control-plane nodes behind a load balancer, Cilium networking with Gateway API, cluster DNS and resource metrics, reachable from the laptop with kubectl.

What I'd do differently

  • etcd has two members, which is not highly available. Losing either member loses quorum. A third control-plane node, or a three-member etcd, fixes that.
  • The single haproxy is a single point of failure. Two load balancers sharing a virtual IP (keepalived) would remove it.
  • Monitoring stops at metrics-server. Prometheus and Grafana, with alerts on etcd and the API server, are the next layer.