While working on creating my own kubernetes cluster I initially wanted to go with the official documentation for the installation process. But then I thought why not explore other distributions that can even simplify and reduce the time of the whole deployment process. This is when I came across k3s which claims to be an easier way of installing and managing kubernetes.

K3s makes use of containerd as the default container platform but I opted to go with docker since I want to become more familiar with it. Note that at this time docker is still considered experimental with k3s as written in the official documentation but I still went on with it anyway. In the following steps I also included how you can execute the kubectl commands without the need of sudo and how they can be executed from a remote machine.

Installation on master node

I have the following setup at the moment where my cluster has a node subnet of 20.0.0.0/24.

alt text

Before anything else, modify the hosts on file on each node to reflect the IP and hostnames of the your entire cluster:

127.0.0.1 localhost
20.0.0.10 k8s-master m
20.0.0.11 k8s-worker-1 w1
20.0.0.12 k8s-worker-2 w2

alt text

Update and upgrade ubuntu packages:

sudo apt update
sudo apt upgrade
sudo reboot

Install required tools and docker on both master and worker nodes. First add the docker repository.

sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

Install docker:

sudo apt update && sudo apt install -y docker-ce

Run docker and enable persistence in case of server reboot:

sudo systemctl start docker
sudo systemctl enable docker

Check if docker is running:

sudo systemctl status docker

Install k3s on the master node:

curl -sfL https://get.k3s.io | sh -s - --docker

Check status of k3s:

sudo systemctl status k3s

alt text

Check if node is running:

sudo kubectl get node -o wide

alt text

As an optional step, unblock ports used by kubernetes. By default firewall will be inactive but in case you have enabled it previously, then you need to perform the following:

sudo ufw allow 6443/tcp
sudo ufw allow 443/tcp

You can also check the status of your firewall with:

ufw status

Disable with:

ufw disable

Extract the token to be used for joining worker nodes:

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

alt text

Installation on worker node

Next is to install k3s on the worker nodes. Here you will need the master node IP address as well as the token from the previous step.

curl -sfL http://get.k3s.io | K3S_URL=https://<master_IP>:6443 K3S_TOKEN=<join_token> sh -s - --docker

alt text

Verify all nodes are joined to the cluster

sudo kubectl get nodes -o wide

alt text

Now you can try to deploy a test nginx pod

sudo kubectl run --image nginx nginx-test
sudo kubectl get pod nginx-test -o wide

alt text

Permit non-root user to execute kubectl commands

Now if you try to execute kubectl commands without sudo, you would probably face the error below.

alt text

This is because kubernetes will be installed with root credentials and any other user to execute kubectl will have to be permitted. The easy way is to copy the k3s.yaml file in the /etc/rancher/k3s directory to your user’s kube config file. First create the directory (with your non-root user) if you don’t have it yet. Note of the user and group variables:

mkdir ~/.kube
cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
chown <user>:<group> ~/.kube/config

Then you have to export the kube config every time so to do this automatically upon login modify your .bashrc and add the line:

export KUBECONFIG=~/.kube/config

For you to be able to execute kubectl commands with just typing ‘k’ you can also add the following:

source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl k

Lastly if you want to be able to execute kubectl commands from a remote machine outside your cluster just ensure you have the same kube config file in the previous step and change the server IP from localhost to the actual one.

alt text

alt text