Skip to content

Phase 2: Install the K3s cluster

Purpose

Install one K3s server and two agents, then prove that the API, node registration, cluster DNS, metrics, and Pod networking are working.

1. Recheck the control-plane identity

On the intended server:

hostnamectl --static
getent hosts k8s-cp-01
getent hosts k8s-worker-01
getent hosts k8s-worker-02

The hostname must return k8s-cp-01, not localhost.localdomain.

2. Create the server configuration

Create the K3s configuration directory and edit the server configuration:

sudo mkdir -p /etc/rancher/k3s
sudo vi /etc/rancher/k3s/config.yaml

The file records these decisions:

  • Pod CIDR: 10.244.0.0/16
  • Service CIDR: 10.96.0.0/16
  • SELinux support enabled
  • Kubernetes secrets encryption enabled
  • administrator kubeconfig mode restricted to 0600

Keeping persistent options in /etc/rancher/k3s/config.yaml avoids losing them if the installer is run again without the original command-line flags.

3. Install the K3s server

curl -sfL https://get.k3s.io | \
  sudo env INSTALL_K3S_CHANNEL=stable sh -s - server

Then check the service and recent logs:

sudo systemctl status k3s --no-pager
sudo journalctl -u k3s -n 100 --no-pager
sudo /usr/local/bin/k3s --version

The installed lab version was v1.36.4+k3s1.

4. Resolve the command-path problem

The first sudo k3s kubectl ... commands returned k3s: command not found. The binary existed at /usr/local/bin/k3s, but the effective command search path was worth checking explicitly:

echo "$PATH"
sudo sh -c 'echo "$PATH"'
command -v k3s
sudo ls -l /usr/local/bin/k3s

Using the absolute path worked consistently:

sudo /usr/local/bin/k3s kubectl get nodes

For the root-owned kubeconfig used in this lab, I created a shell alias for the current session:

alias kubectl='sudo /usr/local/bin/k3s kubectl'

The alias must be recreated in a new shell unless it is deliberately added to the user's shell profile.

5. Validate the API and cluster networks

These are the exact checks used after installing the server:

sudo /usr/local/bin/k3s kubectl get --raw='/readyz'; echo
sudo /usr/local/bin/k3s kubectl cluster-info

sudo /usr/local/bin/k3s kubectl get nodes \
  -o custom-columns='NAME:.metadata.name,READY:.status.conditions[?(@.type=="Ready")].status,INTERNAL-IP:.status.addresses[?(@.type=="InternalIP")].address,POD-CIDR:.spec.podCIDR'

sudo /usr/local/bin/k3s kubectl get service kubernetes \
  -o custom-columns='NAME:.metadata.name,CLUSTER-IP:.spec.clusterIP'

The expected evidence was:

  • /readyz returned ok
  • the Kubernetes API was listening on https://127.0.0.1:6443
  • the server received an address from the Pod CIDR
  • the kubernetes Service used 10.96.0.1

6. Correct the original hostname mistake

The first installation registered the node as localhost.localdomain. Because there were no workloads yet, the cleanest fix was to reinstall before joining workers:

sudo /usr/local/bin/k3s-uninstall.sh
sudo hostnamectl set-hostname k8s-cp-01
sudo reboot

After the reboot, recreate or confirm /etc/rancher/k3s/config.yaml, run the server installation command again, and verify:

hostnamectl --static
sudo /usr/local/bin/k3s kubectl get nodes

The server then appeared as k8s-cp-01.

7. Obtain the worker join token

On the control plane, display the token only when ready to use it:

sudo cat /var/lib/rancher/k3s/server/agent-token

The K3s documentation commonly refers to server/node-token; K3s also exposes the agent token used here. Treat either value as a cluster credential. It was not copied into Git, a screenshot, shell script, or this public site.

8. Join each worker

Run the following separately on k8s-worker-01 and k8s-worker-02. Entering the token with read -s avoids putting its value in shell history:

read -rsp "Paste K3s agent token: " K3S_JOIN_TOKEN
echo

curl -sfL https://get.k3s.io | sudo env \
  INSTALL_K3S_CHANNEL=stable \
  K3S_URL="https://k8s-cp-01:6443" \
  K3S_TOKEN="$K3S_JOIN_TOKEN" \
  INSTALL_K3S_EXEC="agent --selinux" \
  sh -

unset K3S_JOIN_TOKEN

Check the service on each worker:

sudo systemctl status k3s-agent --no-pager
sudo journalctl -u k3s-agent -n 100 --no-pager

If joining fails, test the dependency chain from that worker:

getent hosts k8s-cp-01
curl -k https://k8s-cp-01:6443/livez
sudo firewall-cmd --list-all
sudo journalctl -u k3s-agent --since '-10 minutes' --no-pager

9. Verify and label the nodes

Back on the control plane:

kubectl get nodes
kubectl get nodes -o wide

The working state was:

NAME            STATUS   ROLES           VERSION
k8s-cp-01       Ready    control-plane   v1.36.4+k3s1
k8s-worker-01   Ready    worker          v1.36.4+k3s1
k8s-worker-02   Ready    worker          v1.36.4+k3s1

K3s agents did not initially display a worker role, so I added the worker label that was later also used by the application node selector:

kubectl label node k8s-worker-01 node-role.kubernetes.io/worker=worker
kubectl label node k8s-worker-02 node-role.kubernetes.io/worker=worker

Keep normal application Pods off the server node:

kubectl taint node k8s-cp-01 \
  node-role.kubernetes.io/control-plane=true:NoSchedule

Check labels and taints:

kubectl get nodes --show-labels
kubectl describe node k8s-cp-01 | sed -n '/Taints:/p'

The worker label and matching Deployment selector became important during the rollout troubleshooting in Phase 6.

10. Validate cluster components

kubectl get pods -A -o wide
kubectl get --raw='/livez'; echo
kubectl get --raw='/readyz'; echo
kubectl top nodes
kubectl get events -A --sort-by='.lastTimestamp' | tail -n 20

The healthy result included:

  • CoreDNS Running
  • metrics-server Running
  • local-path-provisioner Running
  • Traefik Running
  • Traefik install jobs Completed
  • one ServiceLB Pod on each node
  • a different /24 Pod range allocated to each node from 10.244.0.0/16

Completed install jobs show 0/1 Completed; that is expected and is not a failed application Pod.

11. Confirm basic failure visibility

These commands became the standard first checks for the rest of the lab:

kubectl get nodes
kubectl get pods -A -o wide
kubectl get events -A --sort-by='.lastTimestamp'

If a node is NotReady, inspect the service and logs on that node before changing manifests. If nodes are Ready but a Pod is Pending, describe the Pod and read the scheduler events.

Completion checks

  • API live and ready.
  • Three correctly named nodes Ready.
  • Workers labelled for their role and application workload.
  • Control plane tainted against ordinary application Pods.
  • CoreDNS and metrics-server working.
  • Pod CIDRs allocated without overlap.
  • System Pods Running or legitimately Completed.

Official references

Next: create the repository and deploy the application.