Installing A Local Kubernetes
Tags: kubernetes, tutorial, devops, beginners
Published: 2023-01-16 01:18:35

## Why

There are good reasons to follow along and get a local Kubernetes cluster: 

1. learn more about containers and orchestration
1. experimentation is a great way to learn
1. gain new skills and understanding

I will be writing more about CI/CD tooling that targets Kubernetes, and starting with documenting how I created my local cluster makes what I'm writing about easier for others to reproduce.

## What's Kubernetes?

[Kubernetes](https://kubernetes.io/) is an Open Source container orchestration system. Kubernetes' API provides a uniform control layer for managing containerized services. There is a lot of great material available that explains what Kubernetes is. See `Further Reading` at the end for some suggestions.

After five years managing physical servers, then another four years working with VM clusters, the value of [Linux Containers](https://linuxcontainers.org/)(LXC) and their eventual productization as Docker appealed to me.

Three years of creating and evolving internal tooling to manage hosting and configuration of Dockerized microservices ended when I discovered Kubernetes in the fall of 2016.

At that time, I migrated a pricy Heroku deployment to a self-managed Kubernetes cluster in AWS that expanded our capabilities, reduced maintenance efforts, increased capacity, and slashed our hosting costs by more than half.

## How

Today there are quite a few ways to get a Kubernetes experience on a local machine. Here are a few that I'm most familiar with and have tried at one point or another:

1. [Docker for Desktop](https://www.docker.com/products/kubernetes/)
1. [microk8s](microk8s.io)
1. [minikube](https://minikube.sigs.k8s.io/docs/start/)
1. [k3s](https://k3s.io/)

### microk8s

microk8s is Canonical's packaged Kubernetes cluster purpose-built for more straightforward installs. Like Rancher Lab's k3s, Canonical built microk8s for local development, edge computing, and IoT applications.

## My Environment

For this post, I chose my Windows desktop running the following:
 * Windows 10.0.19044.2364
 * WSL2 1.0.3.0
 * Debian GNU/Linux 11 (bullseye)
 * Kernel Linux 5.15.79.1-microsoft-standard-WSL2

Some of the steps I share are specific to my environment. You may have to adjust the approach to work for you if you're running a different Linux distribution or version.

### Prerequisites
 
My WSL Debian container required two changes before I could install microk8s. I needed to enable systemd and install Canonical's Snap package manager.

#### Enable systemd

You can enable systemd by creating (or adding to) the file `/etc/wsl.conf`:

```shell
[boot]
systemd=true
```

You'll need to restart WSL after this:
 1. Close your WSL terminal window
 1. Run `wsl.exe --shutdown` from a PowerShell terminal
 1. Re-open your WSL terminal window

#### Install Snap

Now that systemd is up, install Snap with the following set of commands:

```shell
sudo apt update
sudo apt install snapd
```

### Get microk8s

To install microk8s:

```shell
sudo snap install microk8s --classic
```

### System Setup

I use zsh, so I needed to:
 1. add Snap's bin folder to my path
 1. add an alias for microk8s. 

I edited my `~/.zshrc` file to source the alias file and add Snap's bin folder to my path:

```shell
source ~/.zsh_aliases
PATH=$PATH:/snap/bin
```

I created an alias file `~/.zsh_aliases` and added the contents:

```shell
alias m8s'microk8s'
alias k8s='m8s kubectl'
```

Finally, I sourced my changes back into the current console session with the following:

```shell
source ~/.zshrc
```

To verify everything is working, issue the command:

```shell
k8s get nodes
```

You should see something like the following:

```shell
NAME            STATUS   ROLES    AGE   VERSION
computer-name   Ready    <none>   9h    v1.26.0
```

## Up Next

Now that I have a working cluster, the next thing I'll write about is to add some essential features like role based authentication (RBAC) and a dashboard.

## References

[Kubernetes on Windows with microk8s and wsl2](https://ubuntu.com/blog/kubernetes-on-windows-with-microk8s-and-wsl-2)

[How To Enable systemd in WSL](https://www.xda-developers.com/how-enable-systemd-in-wsl/)

[Installing Snap on Debian](https://snapcraft.io/docs/installing-snap-on-debian)

## Further Reading

[Smooth Sailing with Kubernetes](https://cloud.google.com/kubernetes-engine/kubernetes-comic)

[The Illustrated Children's Guide to Kubernetes](https://www.cncf.io/phippy/the-childrens-illustrated-guide-to-kubernetes/)

[What is Kubernetes Architecture?](https://www.vmware.com/topics/glossary/content/kubernetes-architecture.html#:~:text=Kubernetes%20is%20an%20architecture%20that,one%20or%20more%20compute%20nodes.)