Rancher & MicroK8s
Tags: microk8s, rancher, docker, ubuntu
Published: 2023-06-02 12:10:33

## Introduction

This blog is an "answer" to the [The New Stack article](https://thenewstack.io/can-rancher-deliver-on-making-kubernetes-easy/) and the goal is to see how few changes, based on the [Rancher official documentation](https://ranchermanager.docs.rancher.com/), could help run the example in the article.

## Prerequisites

I'm working on Windows and to have a comparable environment, I created an Hyper-V virtual machine (VM) instead of using WSL2.

Here's the configuration details:

* VM: 2 cores / 4Go Ram / 60Go disk
* OS: Ubuntu server 22.04
* Option: OpenSSH server installed

I decided to not install MicroK8s from the installer and will instead make it part of the environment configuration steps (see below).

**Memory consumption**

One of the points pointed out by the author, is that "Rancher" requires at least 4Go RAM as mentioned in the [docs](https://ranchermanager.docs.rancher.com/pages-for-subheaders/installation-requirements#docker).

So while there's some sort of misunderstanding in the requirements, I've taken up to the challenge and created the VM with only 4Go.

> _**Note:** the misunderstanding is that the requirements are only for Rancher on Docker, **in a standalone** server. The author tried to put everything in the same server. This means that only the Rancher requirements were "looked into", and discarded the MicroK8s' own requirements._

For the time being, let's see what a fresh install of Ubuntu server uses:

![RAM usage after a fresh install](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nmsaivjmdt704l3s19df.png)

As displayed, the OS footprint is about 200 Mo, which represents only 5% of the RAM available.

## Configuration

Once the OS is installed, we can SSH with the user and password created during the installation.

### MicroK8s

The first part will be to update the system and install MicroK8s:

```bash
# Update the repositories
sudo apt update

# Update the packages
sudo apt upgrade -y

# Install MicroK8s
sudo snap install microk8s --classic

# [Optional] Install Kubectl
sudo snap install kubectl --classic
## [Option 2] Create an alias to `microk8s kubectl`
sudo snap alias microk8s.kubectl mk

# Add the user to the microk8s group
sudo usermod -a -G microk8s $USER
sudo chown -R $USER ~/.kube

# Apply the group change by starting a new session

# [Optional] Create the .kube directory
mkdir ~/.kube

# Get the Kubeconfig
microk8s config > ~/.kube/config
```

> _**Attention:** MicroK8s shows different cluster IP depending on which `kubectl` is used._
>
> ![MicroK8s kubectl vs kubectl cluster IP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vey8elm4pffqwv0e1rxf.png)

> _**Attention 2:** As of May 2023, the version installed by default is 1.26.4 from the channel 1.26/stable. However the current version of Rancher Manager, 2.7.3, only supports Kubernetes 1.25+. Source: [Support matrix | SUSE](https://www.suse.com/suse-rancher/support-matrix/all-supported-versions/rancher-v2-7-3/#el-48910)_

**Memory consumption**

As stated in [MicroK8s official documentation]([MicroK8s - Get started](https://microk8s.io/docs/getting-started)), the cluster doesn't need much RAM, about 500Mo, which fully correlates with the RAM usage once MicroK8s has been installed:

![RAM usage after MicroK8s install](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/52gnq3remnt6lb66f3up.png)

As displayed, the current RAM used is around 700Mo which represents the 200Mo from the OS and the 500Mo from MicroK8s. So we're about 20% of RAM usage, which seems plenty to continue.

### Docker

The second part and which relates now to the article, is the installation of Docker which will be used to run the Rancher container.

```bash
# Install the dependencies
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release -y

# Install the Docker repository GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# Add the Docker repository
echo "deb [arch="$(dpkg --print-architecture)" signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Update the repositories
sudo apt update

# Install docker
sudo apt-get install docker-ce docker-ce-cli containerd.io -y

# Add the user to the docker group
sudo usermod -aG docker $USER

# Apply the group change by starting a new session
```

**Memory consumption**

Docker install doesn't consume much, around 20Mo only. This is normal as the daemon is idle with no containers running:

![RAM usage after Docker install](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8li1lgfjebb1ys0tfrk1.png)

Let's try to run a "minimal" Nginx container from the [Chainguard image](https://github.com/chainguard-images/images/tree/main/images/nginx#usage):

![RAM usage with a Nginx container running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zj416x5b39aeuc219osz.png)

Even with such minimal container running, the RAM usage is about 90Mo.

> _**Note:** before continuing, I stopped all the containers running and rebooted the server. This brought bach the RAM usage to 790+Mo_

### Rancher Manager

Here's the final component, and so far the RAM usage seems to handle pretty well the workloads.

I'll start a new container based on the latest Rancher version, instead of 2.4 as described in the article. The reason why the author faced issues between Rancher and MicroK8s is due to the version mismatch.

The current version of Rancher, 2.7.3, only supports Kubernetes versions up to 1.25 (as described in the official [Rancher Support Matrix](https://www.suse.com/suse-rancher/support-matrix/all-supported-versions/rancher-v2-7-3/#el-48910). However, the default MicroK8s version is 1.26.

I'll explain later how we can easily change the MicroK8s version, but let's go back to the Rancher container.

The command to start the container is the following:

```shell
# Start a new Rancher container
docker run -d --name=rancher-server --restart=unless-stopped -p 80:80 -p 443:443 --privileged rancher/rancher:latest
```

Once the container started, Rancher is available via the web on the address of the server. Example: https://192.168.0.31

**Memory consumption**

The Rancher container contains a K3s single node cluster, the containerD runtime and Rancher Manager installed. This is reflected in the memory usage:

![RAM usage with Rancher container running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fwgah458zvdnewr29m4h.png)

As displayed, this is a clear jump in RAM usage as it went from almost 800Mo to 2Go+, which represents 50% of the total RAM.

Still, there's plenty of RAM to continue and perform the last task.

## Add MicroK8s to Rancher

As stated above, one of the issue the author ran into was due to a version mismatch. 

The current default version installed by MicroK8s is 1.26. So to ensure the compatibility between Rancher and MicroK8s, the version needs to be downgraded to 1.25.

Thanks to the concept of `snap channels`, this can be achieved in one command:

```shell
# [Optional] Check the current MicroK8s version
microk8s version

# Change the channel to the 1.25 stable
sudo snap refresh microk8s --channel=1.25/stable

# [Optional] Check the current MicroK8s version
microk8s version
```

![Change the MicroK8s channel to 1.25](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/835jy5unsbfz0ytqj0ib.png)

Now that all the components have compatible versions, MicroK8s can be imported into Rancher in the web interface as follow:

* In the welcome page, once logged in, click on `Import Existing`

![Import Existing cluster in Rancher](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6178aii8vuweys6went1.png)

* Choose `Generic`

![Import a Generic cluster](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ntfmrmos45nlrcwpog58.png)

* Write a name, all lowercase, and click `Create`

![Provide a name and click create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/83lnfd1hqjfv371dbg6b.png)

* On the registration page, click on the second option `curl --insecure ...` to copy the text

![Run the registration command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nu0aqdsezkosox4d32q3.png)

> _**Note:** Based on how Rancher was installed with the Docker command, it created a self-signed certificate. This means that MicroK8s will not have the corresponding certificate and would generate an error if the first option was used_

* Once the registration completes, the node will appear as active

![Details of the imported cluster node](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e0al09jb0s39u3bsnw24.png)

**Memory consumption**

After all the tasks were done, and which were successful, the final RAM usage is around 2.5Go, which represents 60%+:

![RAM usage after MicroK8s cluster was imported in Rancher](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o6enzsz95lybvzpnjhsh.png)

## Conclusion

As stated in a tweet, this article comes with a bitter-sweet taste for me.

In one end, I can totally relate to the "learning in public" by writing blog posts, trying new technology and making (many) mistakes while doing so.

However when I face specific issues that others don't seem to have, then I tend to ask for help on different community channels, and of course I go back to the products/projects official documentation.

This is also where I don't relate **at all**, with the article as a whole and have some questions about the publishing process of The New Stack. The article ends abruptly by stating: Portainer is better, use it!

Well, because the article is somewhat incorrect in certain points, again due to the lack of documentation checking, the article actually brought a shade on both Portainer and MicroK8s which never asked for it (I got DMs from both companies).

So here's my advice to anyone reading this blog post: please continue learning in public, ask questions and if you face issues ask for help. But sincerely, avoid doing unwanted comparisons and READ THE DOCS! I'm a Tech Writer, if my docs are not good/complete, open an issue, they're ALL open sourced.

> The Corsair 🏴‍☠️