Skip to content

Instantly share code, notes, and snippets.

@nicc777
Forked from lucj/k3s-multipass.sh
Last active March 21, 2024 19:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicc777/0f620c9eb2958f58173224f29b23a2ff to your computer and use it in GitHub Desktop.
Save nicc777/0f620c9eb2958f58173224f29b23a2ff to your computer and use it in GitHub Desktop.
Setup a k3s kubernetes cluster using Multipass VMs
#!/usr/bin/env bash
# Refer to https://github.com/k3s-io/k3s/releases for your prefered release
export INSTALL_K3S_VERSION="v1.24.10+k3s1"
for node in node1 node2 node3;do
multipass launch -n $node -c 2 -m 4G
done
# Init cluster on node1
multipass exec node1 -- bash -c "curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=$INSTALL_K3S_VERSION sh -"
# Get node1's IP
IP=$(multipass info node1 | grep IPv4 | awk '{print $2}')
# Get Token used to join nodes
TOKEN=$(multipass exec node1 sudo cat /var/lib/rancher/k3s/server/node-token)
# Join node2
multipass exec node2 -- \
bash -c "curl -sfL https://get.k3s.io | K3S_URL=\"https://$IP:6443\" K3S_TOKEN=\"$TOKEN\" sh -"
# Join node3
multipass exec node3 -- \
bash -c "curl -sfL https://get.k3s.io | K3S_URL=\"https://$IP:6443\" K3S_TOKEN=\"$TOKEN\" sh -"
# Get cluster's configuration
multipass exec node1 sudo cat /etc/rancher/k3s/k3s.yaml > k3s.yaml
# Set node1's external IP in the configuration file
#sed -i '' "s/127.0.0.1/$IP/" k3s.yaml # BASH
sed -i "s/127.0.0.1/$IP/" k3s.yaml # ZSH
# We'r all set
echo
echo "K3s cluster is ready !"
echo
echo "Run the following command to set the current context:"
echo "$ export KUBECONFIG=$PWD/k3s.yaml"
echo
echo "and start to use the cluster:"
echo "$ kubectl get nodes"
echo

Deploy HAProxy as Load Balancer into the Cluster (via Ingress) (2024-01-14)

Even though this deployment runs on a single host, you may still want to use a load balancer for convenience.

The following ocnfiguration in /etc/haproxy/haproxy.cfg work for me - BUT - replace the IP addresses with the IP addressess from the output of multipass list. I found that using host names defined in /etc/hosts does not work:

defaults
  mode tcp
  timeout client 10s
  timeout connect 5s
  timeout server 10s 

frontend traefik_insecure
  mode tcp
  bind :9080
  default_backend traefik_ingress_insecure

frontend traefik_secure
  mode tcp
  bind :9443
  default_backend traefik_ingress_secure

backend traefik_ingress_insecure
  mode tcp
  balance leastconn
  server s1 10.15.174.173:30524
  server s2 10.15.174.177:30524
  server s3 10.15.174.176:30524
  server s4 10.15.174.89:30524

backend traefik_ingress_secure
  mode tcp
  balance leastconn
  server s1 10.15.174.173:31479
  server s2 10.15.174.177:31479
  server s3 10.15.174.176:31479
  server s4 10.15.174.89:31479

Restart the service:

sudo systemctl restart haproxy.service

From Local K3s Cluster Made Easy With Multipass authored by Luc Juggery

Step 1 - Install Multipass

As per the installation instructions...

Step 2 - Create VM's

$ multipass launch -n node1 -c 2 -m 4G ; multipass launch -n node2 -c 2 -m 4G ; multipass launch -n node3 -c 2 -m 4G

$ multipass list
Name         State             IPv4             Image
node3        Running           192.168.64.13    Ubuntu 18.04 LTS
node2        Running           192.168.64.12    Ubuntu 18.04 LTS
node1        Running           192.168.64.11    Ubuntu 18.04 LTS

$ ps aux | grep -i "[h]yperkit"
... com.docker.hyperkit ...
... com.canonical.multipass/bin/hyperkit...
... com.canonical.multipass/bin/hyperkit...
... com.canonical.multipass/bin/hyperkit...

Note: The last command is related to Hyperkit on OSX

Step 3 - Init K3s

$ multipass exec node1 -- bash -c "curl -sfL https://get.k3s.io | sh -"
  
$ TOKEN=$(multipass exec node1 sudo cat /var/lib/rancher/k3s/server/node-token)
$ IP=$(multipass info node1 | grep IPv4 | awk '{print $2}')

Step 4 - Join the other nodes to the cluster

$ multipass exec node2 -- bash -c "curl -sfL https://get.k3s.io | K3S_URL=\"https://$IP:6443\" K3S_TOKEN=\"$TOKEN\" sh -"

$ multipass exec node3 -- bash -c "curl -sfL https://get.k3s.io | K3S_URL=\"https://$IP:6443\" K3S_TOKEN=\"$TOKEN\" sh -"

Step 5 - Get cluster configuration

$ multipass exec node1 -- sudo kubectl get nodes
NAME    STATUS   ROLES    AGE     VERSION
node1   Ready    master   5m31s   v1.16.2-k3s.1
node3   Ready    <none>   21s     v1.16.2-k3s.1
node2   Ready    <none>   45s     v1.16.2-k3s.1

$ multipass exec node1 sudo cat /etc/rancher/k3s/k3s.yaml > k3s.yaml

$ sed -i '' "s/127.0.0.1/$IP/" k3s.yaml

$ export KUBECONFIG=$PWD/k3s.yaml

$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
node1 Ready master 10h v1.16.2-k3s.1
node2 Ready <none> 10h v1.16.2-k3s.1
node3 Ready <none> 10h v1.16.2-k3s.1

How to expose the Traefik Dashboard (2024-01-14)

Edit the trafik deployment and add the following command line arguments:

--api=true
--api.insecure=true
--accesslog=true

Apply the following yaml, replacing __HOST__ with your prefered FQDN value:

---
apiVersion: traefik.io/v1alpha1
kind: ServersTransport
metadata:
  name: traefik-dashboard-transport
  namespace: kube-system
spec:
  serverName: traefik-dashboard
  insecureSkipVerify: true
---
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
  name: dashboard
  namespace: kube-system
spec:
  entryPoints:
  - web
  - websecure
  routes:
  - match: (PathPrefix(`/dashboard`) || Host(`__HOST__`))
    kind: Rule
    services:
    - name: api@internal
      kind: TraefikService
  - match: (PathPrefix(`/api`) || Host(`__HOST__`))
    kind: Rule
    services:
    - name: api@internal
      kind: TraefikService
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment