DEV Community

Cover image for How to set up k3s on Ubuntu server
Cheulong Sear
Cheulong Sear

Posted on

How to set up k3s on Ubuntu server

What is k3s?

Prerequisite

  1. Servers
  2. Fixed Registration Address
  3. Option: Worker node

Create the control plane

run this script to create a control plane

curl -sfL https://get.k3s.io | sh -
Enter fullscreen mode Exit fullscreen mode

Create the worker node

To join a worker node to your existing k3s control plane, you need two things from the control-plane node:

  1. The node token
  2. The control-plane node’s IP or hostname

1. Get the node token (on the control-plane node)

Run this on the machine where you installed k3s with your command:

sudo cat /var/lib/rancher/k3s/server/node-token
Enter fullscreen mode Exit fullscreen mode

Copy this value.

2. Join the worker node

On the worker node, run:

curl -sfL https://get.k3s.io | K3S_URL=https://<CONTROL_PLANE_IP>:6443 K3S_TOKEN=<NODE_TOKEN> sh -
Enter fullscreen mode Exit fullscreen mode

Example:

curl -sfL https://get.k3s.io | \
  K3S_URL=https://192.168.1.10:6443 \
  K3S_TOKEN=K10f3a9c7c0f2e7c8d9b3b1a2c3d4e5f6::server:abcdef1234567890 \
  sh -
Enter fullscreen mode Exit fullscreen mode

Verify the worker joined

Back on the control-plane node:

kubectl get nodes
Enter fullscreen mode Exit fullscreen mode

You should see something like:

NAME           STATUS   ROLES                  AGE   VERSION
master-1       Ready    control-plane,master   5m    v1.xx.x+k3s
worker-1       Ready    <none>                  1m    v1.xx.x+k3s
Enter fullscreen mode Exit fullscreen mode

Optional copy kubeconfig to personal pc

On the control-plane node

sudo cp /etc/rancher/k3s/k3s.yaml /tmp/k3s.yaml
sudo chown $USER:$USER /tmp/k3s.yaml
Enter fullscreen mode Exit fullscreen mode

On your Ubuntu Desktop

scp user@192.0.0.1:/tmp/k3s.yaml ~/.kube/config
chmod 600 ~/.kube/config

Enter fullscreen mode Exit fullscreen mode

Edit ONLY the server line

nano ~/.kube/config

Enter fullscreen mode Exit fullscreen mode

change

server: https://127.0.0.1:6443

# to

server: https://<control-plane-ip>:6443

Enter fullscreen mode Exit fullscreen mode

verification

kubectl config get-contexts
kubectl cluster-info
kubectl get nodes

Enter fullscreen mode Exit fullscreen mode

Troubleshooting

hostname collusion

On control plane, Run :

sudo systemctl status k3s-agent
Enter fullscreen mode Exit fullscreen mode

if you see 403 unable to verify password for node ubuntu-server

you might need to change worker-node's hostname
on worker node

# change hostname to worker-1

sudo systemctl stop k3s-agent || true
sudo /usr/local/bin/k3s-agent-uninstall.sh || true
sudo rm -rf /var/lib/rancher/k3s
sudo rm -rf /etc/rancher

sudo hostnamectl set-hostname worker-1
exec bash
Enter fullscreen mode Exit fullscreen mode

On the CONTROL PLANE — remove the old node entry

kubectl delete node ubuntu-server || true
Enter fullscreen mode Exit fullscreen mode

Also verify the token:

sudo cat /var/lib/rancher/k3s/server/node-token
Enter fullscreen mode Exit fullscreen mode

Rejoin the worker with the correct token
On the worker node:

curl -sfL https://get.k3s.io | \
  K3S_URL=https://<CONTROL_PLANE_IP>:6443 \
  K3S_TOKEN=<NODE_TOKEN> \
  sh -

sudo systemctl restart k3s-agent
Enter fullscreen mode Exit fullscreen mode

=== Done ===

Leave a comment if you have any questions.

===========
Please keep in touch
Portfolio
Linkedin
Github
Youtube

Top comments (0)