DEV Community

Cover image for Different Ways of Creating k3s Cluster
Abhinav Dubey
Abhinav Dubey

Posted on

Different Ways of Creating k3s Cluster

Creating a Kubernetes cluster was quite a tedious task until Rancher Labs introduced k3s - Lightweight Kubernetes. K3s is nothing but a lightweight kubernetes, where the developers has removed millions of lines of code from straight Kubernetes and wrapped it in a binary less than 100mb. Yes, you read it right, "millions of lines of codes".
Waao-meme
In straight Kubernetes, we have different ways of creating a k8s cluster such as by using kops, eksctl, kubeadm, kubectl, etc, we also have few different ways of creating k3s cluster. In this blog we'll discuss different ways through which we can create k3s cluster.

1. With k3s

The traditional way of creating a k3s cluster is by using k3s itself as a server and as agent. We can create single as well as multi node or HA cluster using k3s.

[Note] There's no prerequisite for creating a cluster using k3s, you just need to have a system with minimum 512MB RAM and 1 core CPU

K3s single node cluster

Creating a single node k3s cluster or setting up k3s server is just a piece of cake. We need deploy a single command and it will create k3s server for you.

Please execute the following command and its done. Just with single command we can create our k3s single node cluster.

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

[Note: We will not go in-depth of commands and flags used, if you want to know, please let me know in the comment section]

K3s HA Cluster

Well in a single command we have created our k3s cluster single node cluster, but for High Availability cluster, we have to fire 3 commands which is pretty less as compared to straight Kubernetes.

Please follow the steps below to create your HA cluster.
Step-1 : Install k3s cluster in our server

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

Step-2 : Extract the node-token for adding agent nodes

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

Step-3 : Add worker nodes to the master created. This command should be fired on another instance/VM to add it with the master. Replace my-server with master IP which we created above and node-token with the token extracted from above command.

curl -sfL https://get.k3s.io | K3S_URL=https://my-server:6443 K3S_TOKEN=node-token sh -
Enter fullscreen mode Exit fullscreen mode

Hurray! We are done with the k3s HA Setup as well. For futhur in-depth intuition about the k3s, please refer to my previous article.

Using k3d

One of the most popular and second method of creating k3s cluster is by using k3d. By the name itself it suggests, k3s-in-docker. I have contributed an article as guest author for a community where I wrote about k3d. Please refer to this link to get brief insights of this wonderful tool.
Now let's directly jump into creating our k3s cluster using k3d.

[Note : For using k3d you must have docker installed in your system]

k3d single node cluster

Creating a single node k3s cluster using k3d is like having a conversation with your dad.
Call-meme
Before installing your cluster, you need to install k3d on your system. Please execute the following commands to install k3s and create your single node cluster.

Step-1 : Install k3d

wget -q -O - https://raw.githubusercontent.com/rancher/k3d/main/install.sh | bash
Enter fullscreen mode Exit fullscreen mode

Step-2 : Create a single node cluster.

k3d cluster create demo-cluster
Enter fullscreen mode Exit fullscreen mode

and its done. Just this single command can create your single node k3s cluster in seconds.

k3d HA Cluster

Now let's install k3s HA Cluster using k3d. Well its just a cup of tea. Here's how easily you can install a HA Cluster using k3d.

k3d cluster create --servers 3 --image your/docker-image
Enter fullscreen mode Exit fullscreen mode

Its done. This is how easily you can spin up your cluster using k3d. Fore more detailed information about k3d HA Cluster, please refer to rancher lab's official blog

[Note : We will not discuss the tools & commands in depth as it is beyond the scope of this blog, but if you want depth intuition, please let me know through comments]

Using k3sup

Now, let's move into our third tool K3sup, called as ketcup developed by Alex Ellis. K3sup is really an amazing tool which helps us to create a k3s cluster. I have written a twitter thread around it, feel free to check it out.

k3sup single node cluster

Let's create our single node k3s cluster using k3sup. It require few commands to setup k3sup and then it automates the process of k3s cluster.
I would recommend to create an AWS ec2 instance or any other VM before getting started with the commands.
Typing-meme
Step-1 : Download k3sup

curl -sLS https://get.k3sup.dev | sh
Enter fullscreen mode Exit fullscreen mode

Step-2 : Install k3sup

cd /usr/local/bin
sudo install k3sup /usr/local/bin/
Enter fullscreen mode Exit fullscreen mode

Step-3 : Install k3s Cluster in Instance/VM.
Replace the SERVER_IP with your instance/Vm IP and ec2-user with your username. In case of ec2 instance, default will be the same as given.

k3sup install --ip SERVER_IP --user ec2-user
Enter fullscreen mode Exit fullscreen mode

Step-4 : Export kubeconfig file
Execute the following command from the directory where you have executed the above command.

export KUBECONFIG=`pwd`/kubeconfig
Enter fullscreen mode Exit fullscreen mode

Step-5 : Switch Context to default and start firing your kubectl commands.

kubectl config set-context default
Enter fullscreen mode Exit fullscreen mode

and here we go. We are done with the single node k3s cluster setup using k3sup. The beauty of k3sup is that from localhost itself you can run all the commands in your k3s cluster created on server through establishing a ssh connection with the instance.

k3sup HA Cluster

Finally, let move ahead and setup our HA Cluster using k3sup. Well the installation and the process of setting up a k3s cluster would be the same as that in case of single node. We just have to execute a single command and it will join agent nodes to the server running up there.

Please execute the following command to add multiple nodes to your k3s cluster running using k3sup.
Here AGENT_IP will be replaced with IP of Agent Node and SERVER_IP with IP of server and ec2-user with username.

k3sup join --ip AGENT_IP --server-ip SERVER_IP --user ec2-user
Enter fullscreen mode Exit fullscreen mode

Now if we try to list down the nodes using kubectl get nodes, we can see the agent/worker node has been added in our cluster.

For other configurations such as adding external database or load balancers, please refer to this github repository.

Top comments (2)

Collapse
 
davistran86 profile image
davistran86

Hi, nice article :) . I would like to ask if I can change the default pod eviction time when a node down ? I don't know how to configure that using k3sup.

Collapse
 
abhinavd26 profile image
Abhinav Dubey

Hey, thanks for the question.
I have not encountered any as such use-case till now with k3sup.
I am not sure about it, let me dig a bit, meanwhile this could be helpful for you I guess -
github.com/alexellis/k3sup