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".
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
[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
Step-2 : Extract the node-token for adding agent nodes
sudo cat /var/lib/rancher/k3s/server/node-token
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 -
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.
All about k3s - Lightweight Kubernetes
Abhinav Dubey ・ Aug 11 '21
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.
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
Step-2 : Create a single node cluster.
k3d cluster create demo-cluster
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
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.
Step-1 : Download k3sup
curl -sLS https://get.k3sup.dev | sh
Step-2 : Install k3sup
cd /usr/local/bin
sudo install k3sup /usr/local/bin/
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
Step-4 : Export kubeconfig file
Execute the following command from the directory where you have executed the above command.
export KUBECONFIG=`pwd`/kubeconfig
Step-5 : Switch Context to default and start firing your kubectl commands.
kubectl config set-context default
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
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)
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.
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