Skip to content

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 apply and by Terraform's Kubernetes provider

Topology ​

NodeImageCPUs / memoryAddressesRole
serverdebian-132 / 2GiBmgmt 10.129.0.11k3s server (control plane)
agent1debian-132 / 1536MiBmgmt 10.129.0.12k3s agent
agent2debian-132 / 1536MiBmgmt 10.129.0.13k3s agent

Before you start ​

bash
vz image pull disk debian-13

Run it ​

bash
cd examples/29-k3s-cluster
vz validate
vz up
vz node ls
ssh lab@10.129.0.11        # password: lab

k3s 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 use tofu instead of terraform)
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 wide

kubectl ​

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 agent2

Terraform ​

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 destroy

Notes ​

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