Local Kubernetes Cluster with K3s on Mac M1
Tags: kubernetes, devops, k3s
Published: 2022-10-11 07:18:42


Update: This article is outdated. I wrote an updated article on [setting up k8s anywhere with single command](https://avilpage.com/2023/03/setup-k8s-anywhere-k3d.html).


#### Introduction

{% youtube vi5eRgBMs90 %}

Kubernetes(k8s)<sup id="fnref:k8s"><a href="https://avilpage.com/2022/10/local-kubernetes-with-k3s-on-mac.html#fn:k8s">1</a></sup> is an open-source system for managing large scale containerized applications. K3s<sup id="fnref:k3s"><a href="https://avilpage.com/2022/10/local-kubernetes-with-k3s-on-mac.html#fn:k3s">2</a></sup> is lightweight K8s in a single binary file. However, K3s won't work directly on Mac as it needs systemd/OpenRC.

```
$ curl -sfL https://get.k3s.io | sh -

[ERROR]Can not find systemd or openrc to use as a process supervisor for k3s

```

To setup k8s/k3s on Mac, we need to setup a Linux layer on top of Mac. An easy way to spin up Linux VMs on Mac M1 is to use multipass<sup id="fnref:multipass"><a href="https://avilpage.com/2022/10/local-kubernetes-with-k3s-on-mac.html#fn:multipass">3</a></sup>. In this article, lets see how to setup K3s on Mac using multipass

#### K3s Setup

Install multipass with brew by running the following command.

```
$ brew install --cask multipass

```

Once it is installed, spin up a new VM by specifying memory and disk space.

```
$ multipass launch --name k3s --mem 4G --disk 40G

```

Once VM is launched, we can see VM details.

```
$ multipass info k3s
Name: k3s
State: Running
IPv4: 192.168.64.4
                10.42.0.0
                10.42.0.1
Release: Ubuntu 22.04.1 LTS
Image hash: 78b5ca0da456 (Ubuntu 22.04 LTS)
Load: 1.34 2.10 1.70
Disk usage: 3.7G out of 38.6G
Memory usage: 1.2G out of 3.8G
Mounts: /Users/chillaranand/test/k8s => ~/k8s
                    UID map: 503:default
                    GID map: 20:default

```

We can even mount Mac directories on the VM.

```
$ multipass mount ~/test/k8s k3s:~/k8s

```

This will be useful when we are making changes on host directories and want to apply changes on the cluster which is inside VM.

Now, we can install k3s by running the install script inside the VM.

```
$ multipass shell k3s

ubuntu@k3s:~$ curl -sfL https://get.k3s.io | sh -

```

This will setup a k3s cluster on the VM. We can use kubectl and deploy applications on this cluster.

By default, k3s config file will be located at `/etc/rancher/k3s/k3s.yaml`. With this config file, we can use Lens<sup id="fnref:lens"><a href="https://avilpage.com/2022/10/local-kubernetes-with-k3s-on-mac.html#fn:lens">4</a></sup> to manage k8s cluster.

![](https://avilpage.com/images/k8s-mac-m1.png)

Lets find out IP of the VM & k8s token so that we can spin up a new VM and add it to this cluster.

```
# get token & ip of k3s
$ multipass exec k3s sudo cat /var/lib/rancher/k3s/server/node-token
$ multipass info k3s | grep -i ip


$ multipass launch --name k3s-worker --mem 2G --disk 20G

$ multipass shell k3s-worker

ubuntu@k3s-worker:~$ curl -sfL https://get.k3s.io | K3S\_URL=https://192.168.64.4:6443 K3S\_TOKEN="hs48af...947fh4::server:3tfkwjd...4jed73" sh -

```

We can verify if the node is added correctly from k3s VM.

```
ubuntu@k3s:~$ kubectl get nodes
NAMESTATUSROLESAGEVERSION
k3sReadycontrol-plane,master15hv1.24.6+k3s1
k3s-workerReady<none>7m15sv1.24.6+k3s1

```

Once we are done with experimenting k3s, we can delete the VMs.

```
$ multipass delete k3s k3s-worker
$ multipass purge

```

#### Conclusion

multipass is a great tool to spin up Linux VMs on Mac with single command. K3s is better tool to setup k8s cluster locally for development and testing.

Even though we have mentioned this tutorial is meant for Mac M1, it should work fine on any Linux distribution as well.

* * *

1. [https://en.wikipedia.org/wiki/Kubernetes](https://en.wikipedia.org/wiki/Kubernetes) [↩](https://avilpage.com/2022/10/local-kubernetes-with-k3s-on-mac.html#fnref:k8s "Jump back to footnote 1 in the text")

2. [https://k3s.io/](https://k3s.io/) [↩](https://avilpage.com/2022/10/local-kubernetes-with-k3s-on-mac.html#fnref:k3s "Jump back to footnote 2 in the text")

3. [https://github.com/canonical/multipass](https://github.com/canonical/multipass) [↩](https://avilpage.com/2022/10/local-kubernetes-with-k3s-on-mac.html#fnref:multipass "Jump back to footnote 3 in the text")

4. [https://k8slens.dev/](https://k8slens.dev/) [↩](https://avilpage.com/2022/10/local-kubernetes-with-k3s-on-mac.html#fnref:lens "Jump back to footnote 4 in the text")