Different Ways of Creating k3s Cluster
Tags: kubernetes, opensource, github, devops
Published: 2021-10-07 03:43:18

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iqh6pe35ifg6ul2unzfe.gif)
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.
```bash
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
```bash
curl -sfL https://get.k3s.io | sh -s - --write-kubeconfig-mode 644
```
**Step-2 :** Extract the node-token for adding agent nodes
```bash
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.
```bash
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.
{% link https://dev.to/abhinavd26/all-about-k3s-lightweight-kubernetes-3ell %}

## 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](https://collabnix.com/top-10-kubernetes-tool-you-need-for-2021-part-2/) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l99jjbjow352tcp240xa.gif)
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
```bash
wget -q -O - https://raw.githubusercontent.com/rancher/k3d/main/install.sh | bash
```

**Step-2 :** Create a single node cluster.
```bash
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.
```bash
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](https://rancher.com/blog/2020/set-up-k3s-high-availability-using-k3d)

[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.
{% twitter 1442892364519858181 %}

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pasbsdxlzmyb3xq0o91x.gif)
**Step-1 :** Download k3sup
```bash
curl -sLS https://get.k3sup.dev | sh
```

**Step-2 :** Install k3sup
```bash
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.
```bash
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.
```bash
export KUBECONFIG=`pwd`/kubeconfig
```
**Step-5 :** Switch Context to default and start firing your kubectl commands.
```bash
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.
```bash
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](https://github.com/alexellis/k3sup).