Quickly create K3s cluster and add K3s nodes with GUI Tool using AutoK3s (Installation guide in WSL)
Tags: kubernetes, wsl, docker, k3s
Published: 2022-07-10 22:12:30


AutoK3s is a new tool that will make it easy to install and manage Kubernetes clusters in many platforms.
In this tutorial, we will install Kubernetes in windows WSL with the use of:-
1. Ubuntu 22.04 WSL distro

What is amazing in this distro is that they have built in systemctl. If you chose another distro, you should use other methods to make docker and other services start.
2. Docker for Linux (without docker desktop)

As you may know, docker desktop is not free anymore, and I found it use more resources from the pc. 
3. K3S

K3s is a highly available, certified Kubernetes distribution designed for production workloads. The most important it is "Lightweight Kubernetes"
4. K3D 

K3d is a lightweight wrapper to run k3s in docker.
5. AutoK3s

AutoK3s is a lightweight tool for simplifying K3s cluster management. 

##Install a Linux distro 
it is recommended to use [Ubuntu 22.04](https://www.microsoft.com/store/productId/9PN20MSR04DW) in this tutorial. You can download it from Microsoft store.

It is advisable to upgrade.
```shell
sudo apt update && sudo apt upgrade
```

##Enable systemctl
in your WSL edit `/etc/wsl.conf`
```shell
sudo nano /etc/wsl.conf
```
add  the following line:
```shell
[boot]
command="/usr/libexec/wsl-systemd"
```

then close the terminal and restart WSL with the following command in your PowerShell
```ps
 wsl --shutdown
```
then you can start new WSL shell and check that systemctl is working
```shell
 ps aux
```
![systemctl](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hz72s3trxuhbp311xioj.jpg)
note that first line(PID 1) is systemctl 

then check systemctl status:
```shell
systemctl status
```
for me, I got 3 failed services:
- `ssh.service`
- `systemd-remount-fs.service`
- `systemd-sysusers.service`

to solve ssh service :
```shell
 apt-get install openssh-server openssh-client
 dpkg-reconfigure openssh-server
```

for the rest:
- comment out the line in the `/etc/fstab`:
```txt
#LABEL=cloudimg-rootfs   /        ext4   discard,errors=remount-ro       0 1
```
- open `/usr/lib/systemd/system/systemd-sysusers.service` and delete all lines, then add:
```txt
[Unit]
Description=Create System Users
Documentation=man:sysusers.d(5) man:systemd-sysusers.service(8)
DefaultDependencies=no
Conflicts=shutdown.target
After=systemd-remount-fs.service
Before=sysinit.target shutdown.target systemd-update-done.service
ConditionNeedsUpdate=/etc

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=systemd-sysusers
TimeoutSec=90

[Service]
LoadCredential=

```
Finally, restart and check the services again.


## Install Docker
the instructions for installing Docker in details from this [tutorial](https://dev.to/bowmanjd/install-docker-on-windows-wsl-without-docker-desktop-34m9), we can skip `Sharing dockerd` and adding script (because we use systemctl) in short this can be done with the following:

- Install dependencies 
```shell
sudo apt install --no-install-recommends apt-transport-https ca-certificates curl gnupg2
```
- Switch to legacy iptables 

```shell
update-alternatives --config iptables
```
 select iptables-legacy

- package repository configuration

First, temporarily set some OS-specific variables:
```shell
. /etc/os-release
```
Then, make sure that apt will trust the repo:
```shell
curl -fsSL https://download.docker.com/linux/${ID}/gpg | sudo tee /etc/apt/trusted.gpg.d/docker.asc
```
Then add and update the repo information so that apt will use it in the future:
```shell
echo "deb [arch=amd64] https://download.docker.com/linux/${ID} ${VERSION_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/docker.list
sudo apt update
```

- Install Docker
```shell
sudo apt install docker-ce docker-ce-cli containerd.io
```

- Add user to Docker group

```shell
sudo usermod -aG docker $USER
```

Finally, close that WSL window, and launch WSL again. And check if docker is working:
```shell
 docker run --rm hello-world
```


## Install K3D and AutoK3s

- Install K3D
```zsh
curl -s https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash
```

- Install AutoK3s
Download the binary from the release page.
```zsh
curl https://github.com/cnrancher/autok3s/releases/download/v0.5.1/autok3s_linux_amd64 --create-dirs -o ~/bin/autok3s
```
Make it executable
```zsh
chmod 755 ~/bin/autok3s
```
then add to path
```zsh
echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc
```
- install kube-explorer (optional)
```sh
curl https://github.com/cnrancher/kube-explorer/releases/download/v0.2.9/kube-explorer-linux-amd64 --create-dirs -o ~/bin/kube-explorer

chmod 755 $HOME/bin/kube-explorer
```

## start `autok3s` to deploy Kubernetes cluster

```sh
autok3s serve
```
Open [127.0.0.1:8080](127.0.0.1:8080) on your browser to start create, delete modify Kubernetes clusters.

![autok3s ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lsji13qn050dnd2xv906.jpg)
 
Select k3d as provide and give name to the cluster and specify how many mater/worker nodes you want.

This is a sample option I used to build a cluster with one master node and one worker node and limit their ram usage to 1 GB
![options](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2flx7c6dwnqt3vi42j6n.jpg)

You can add the explorer to the cluster 

![explorer ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9b4axm6p9lvsnxi1ro45.jpg)