K3S on your dev box with multipass
Tags: kubernetes, k3s, tips
Published: 2022-05-26 07:17:41

In this post we will understand how to get a working kubernetes instance on your mac (or other development machine

##### Step 1: Install multipass
[Multipass](https://multipass.run/) is a lightweight VM manager Linux, windows and Mac. It is useful for creating a Ubuntu sandbox quickly without VirtualBox, Parallels or other VM managers.

```bash
❯ brew install multipass
Running `brew update --preinstall`...
==> Downloading https://github.com/canonical/multipass/releases/download/v1.9.0/multipass-1.9.0+mac-Darwin.pkg
==> Downloading from https://objects.githubusercontent.com/github-production-release-asset-2e65be/114128199/8e737c39-9608-4e94-a230-641237a83b7a?X-Amz
######################################################################## 100.0%
==> Installing Cask multipass
==> Running installer for multipass; your password may be necessary.
Package installers may write to any location; options such as `--appdir` are ignored.
Password:
installer: Package name is multipass
installer: Installing at base path /
installer: The install was successful.
🍺  multipass was successfully installed!

❯ multipass version
multipass   1.9.0+mac
multipassd  1.9.0+mac
```

##### Step 2: Launch a Ubuntu VM instance
```bash
❯ multipass launch --name k3s-demo --cpus 2 --mem 4g --disk 20g
Launched: k3s-demo

❯ multipass info k3s-demo
Name:           k3s-demo
State:          Running
IPv4:           192.168.64.3
Release:        Ubuntu 20.04.4 LTS
Image hash:     147e0cea207e (Ubuntu 20.04 LTS)
Load:           0.90 0.26 0.09
Disk usage:     1.3G out of 19.2G
Memory usage:   158.4M out of 3.8G
Mounts:         --
```

##### Step 3: Install k3s
```
❯ multipass exec k3s-demo -- bash -c "curl -sfL https://get.k3s.io | K3S_KUBECONFIG_MODE="644" sh -"

[INFO]  Finding release for channel stable
[INFO]  Using v1.23.6+k3s1 as release
[INFO]  Downloading hash https://github.com/k3s-io/k3s/releases/download/v1.23.6+k3s1/sha256sum-arm64.txt
[INFO]  Downloading binary https://github.com/k3s-io/k3s/releases/download/v1.23.6+k3s1/k3s-arm64
[INFO]  Verifying binary download
[INFO]  Installing k3s to /usr/local/bin/k3s
[INFO]  Skipping installation of SELinux RPM
[INFO]  Creating /usr/local/bin/kubectl symlink to k3s
[INFO]  Creating /usr/local/bin/crictl symlink to k3s
[INFO]  Creating /usr/local/bin/ctr symlink to k3s
[INFO]  Creating killall script /usr/local/bin/k3s-killall.sh
[INFO]  Creating uninstall script /usr/local/bin/k3s-uninstall.sh
[INFO]  env: Creating environment file /etc/systemd/system/k3s.service.env
[INFO]  systemd: Creating service file /etc/systemd/system/k3s.service
[INFO]  systemd: Enabling k3s unit
Created symlink /etc/systemd/system/multi-user.target.wants/k3s.service → /etc/systemd/system/k3s.service.
[INFO]  systemd: Starting k3s

```

##### Step 4: Configure k3s to be used from host
```bash
# Get the instance IP
❯ K3S_IP=$(multipass info k3s-demo | grep IPv4 | awk '{print $2}')

❯ echo $K3S_IP
192.168.64.3

# Get the admin yaml
❯ multipass exec k3s-demo sudo cat /etc/rancher/k3s/k3s.yaml > ${HOME}/k3s.conf

# Update the IP address to the one mapped by multipass
❯ sed -i "s/127.0.0.1/${K3S_IP}/" ${HOME}/k3s.conf

❯ chmod 600 ${HOME}/k3s.conf

```

##### Step 5: Verify that everything works
```bash
❯ export KUBECONFIG=${HOME}/k3s.conf

❯ kubectl get nodes
NAME       STATUS   ROLES                  AGE   VERSION
k3s-demo   Ready    control-plane,master   13m   v1.23.6+k3s1

❯ kubectl run test-pod --image=nginx --restart=Never
pod/test-pod created

❯ kubectl get pods
NAME       READY   STATUS    RESTARTS   AGE
test-pod   1/1     Running   0          88s

❯ kubectl expose pod/test-pod --port=80 --type=NodePort
service/test-pod exposed

❯ kubectl get svc
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE   SELECTOR
kubernetes   ClusterIP   10.43.0.1     <none>        443/TCP        25m   <none>
test-pod     NodePort    10.43.73.25   <none>        80:31388/TCP   5s    run=test-pod

❯ curl -I ${K3S_IP}:31388
HTTP/1.1 200 OK
Server: nginx/1.21.6
Date: Thu, 26 May 2022 07:17:02 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 25 Jan 2022 15:03:52 GMT
Connection: keep-alive
ETag: "61f01158-267"
Accept-Ranges: bytes

```


---
##### multipass housekeeping
- List all instances
```bash
❯ multipass list
Name                    State             IPv4             Image
k3s-demo                Running           192.168.64.3     Ubuntu 20.04 LTS
k3s-demo2               Running           192.168.64.4     Ubuntu 20.04 LTS

```
- Delete an instance
```bash
❯ multipass delete k3s-demo2

```
- Purge and recover resources from deleted instances
```bash
❯ multipass purge

```