DEV Community

Cover image for Local Kubernetes Cluster with K3s on Mac M1
Chillar Anand
Chillar Anand

Posted on • Updated on

Local Kubernetes Cluster with K3s on Mac M1

Update: This article is outdated. I wrote an updated article on setting up k8s anywhere with single command.

Introduction

Kubernetes(k8s)1 is an open-source system for managing large scale containerized applications. K3s2 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

Enter fullscreen mode Exit fullscreen mode

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 multipass3. 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

Enter fullscreen mode Exit fullscreen mode

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

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

We can even mount Mac directories on the VM.

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

Enter fullscreen mode Exit fullscreen mode

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 -

Enter fullscreen mode Exit fullscreen mode

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 Lens4 to manage k8s cluster.

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 -

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

$ multipass delete k3s k3s-worker
$ multipass purge

Enter fullscreen mode Exit fullscreen mode

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

  2. https://k3s.io/

  3. https://github.com/canonical/multipass

  4. https://k8slens.dev/

Top comments (1)

Collapse
 
jianwu profile image
jianwu

Thanks for the information. it works, except I have to change the script:

curl -sfL https://get.k3s.io | K3S_URL=https://192.168.205.3:6443 K3S_TOKEN="K10ce4....fe7b4d19" sh -

Without \_, e.g.: K3S\_URL