29 Kubernetes with k3s
A three-node k3s cluster: one server and two agents, with kubectl working from the Mac.
What you practice
- Bootstrapping k3s with a shared token
- Reaching the API server from the Mac through a port forward
- Scheduling and node failure
- The same cluster driven by
kubectl applyand by Terraform's Kubernetes provider
Topology
| Node | Image | CPUs / memory | Addresses | Role |
|---|---|---|---|---|
server | debian-13 | 2 / 2GiB | mgmt 10.129.0.11 | k3s server (control plane) |
agent1 | debian-13 | 2 / 1536MiB | mgmt 10.129.0.12 | k3s agent |
agent2 | debian-13 | 2 / 1536MiB | mgmt 10.129.0.13 | k3s agent |
Before you start
bash
vz image pull disk debian-13Run it
bash
cd examples/29-k3s-cluster
vz validate
vz up
vz node ls
ssh lab@10.129.0.11 # password: labk3s downloads its binary and images on first boot: allow two or three minutes.
Try it
On the Mac:
- kubectl:
brew install kubectl - terraform:
brew install hashicorp/tap/terraform(or OpenTofu:brew install opentofu, then usetofuinstead ofterraform)
bash
ssh lab@10.129.0.11 'cat /etc/rancher/k3s/k3s.yaml' > kubeconfig # its server is https://127.0.0.1:6443
export KUBECONFIG=$PWD/kubeconfig
kubectl get nodes -o widekubectl
bash
kubectl apply -f k8s/hello.yaml
kubectl get pods -o wide # spread over the nodes
curl -s http://localhost:30080 | head -4 # the NodePort, forwarded to the Mac
vz node pause agent2 # after a few minutes its pods move
kubectl get nodes
vz node resume agent2Terraform
bash
cd terraform
terraform init
terraform apply # namespace demo, deployment and service whoami
curl -s http://localhost:30081 # whoami prints which pod answered
kubectl -n demo scale deployment whoami --replicas=4
terraform plan # Terraform sees the drift and wants 2 again
terraform destroyNotes
The token is in init/*/user-data: fine for a lab, never for a real cluster.
Clean up
bash
vz down
vz destroy --yes # deletes the overlays and state of this lab