The Rancher k3s project has made it possible to run Kubernetes Clusters on Edge and IoT devices with ease. K3s provides an easy way to run a Lightweight Kubernetes cluster with half the memory, all in a binary of less than 100 MB. Alpine Linux on the other side is a Lightweight operating system that has gained lots of traction in microservices space.
This guide will help you setup a Lightweight Kubernetes cluster on Alpine Linux with k3s. If you want to try this in a Lab, checkout our guide on using Vagrant.
Setup Kubernetes on Alpine Linux with k3s
k3s is installed through a bash script that performs all the actions in the background without user input. Open the console of your Alpine Linux and run the command below.
$ curl -sfL https://get.k3s.io | sudo sh -
Command execution output:
[INFO] Finding release for channel stable [INFO] Using v1.18.2+k3s1 as release [INFO] Downloading hash https://github.com/rancher/k3s/releases/download/v1.18.2+k3s1/sha256sum-amd64.txt [INFO] Downloading binary https://github.com/rancher/k3s/releases/download/v1.18.2+k3s1/k3s [INFO] Verifying binary download [INFO] Installing k3s to /usr/local/bin/k3s [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/rancher/k3s/k3s.env [INFO] openrc: Creating service file /etc/init.d/k3s [INFO] openrc: Enabling k3s service for default runlevel [INFO] openrc: Starting k3s * Caching service dependencies ... [ ok ] * Mounting cgroup filesystem ... [ ok ] * Starting k3s ... [ ok ]
Confirm a list of running services:
$ rc-status Runlevel: default crond [ started ] acpid [ started ] haveged [ started ] net-online [ started ] k3s [ started 00:01:22 (0) ] qemu-guest-agent [ started ] sshd [ started ] Dynamic Runlevel: hotplugged Dynamic Runlevel: needed/wanted sysfs [ started ] fsck [ started ] root [ started ] localmount [ started ] cgroups [ started ] Dynamic Runlevel: manual
Check Cluster nodes status:
$ sudo kubectl get nodes NAME STATUS ROLES AGE VERSION myalpine Ready master 2m14s v1.18.2+k3s1
View cluster config:
$ sudo kubectl cluster-info Kubernetes master is running at https://127.0.0.1:6443 CoreDNS is running at https://127.0.0.1:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy Metrics-server is running at https://127.0.0.1:6443/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
Notice that a kubeconfig file is written to /etc/rancher/k3s/k3s.yaml and additional utilities, such as kubectl, crictl, k3s-killall.sh, and k3s-uninstall.sh are added by default.
$ sudo crictl ps CONTAINER IMAGE CREATED STATE NAME ATTEMPT POD ID c422071177c71 aa764f7db3051 2 minutes ago Running traefik 0 dce7e443a00d3 00a260c8ca596 897ce3c5fc8ff 2 minutes ago Running lb-port-443 0 8396062f10e99 f8cee92686f79 897ce3c5fc8ff 2 minutes ago Running lb-port-80 0 8396062f10e99 a24528e2b6d48 c4d3d16fe508b 2 minutes ago Running coredns 0 4ab79ffe4b375 a928d07727c8a 9dd718864ce61 3 minutes ago Running metrics-server 0 06052271c42cf 5708f0490b3ef 9d12f9848b99f 3 minutes ago Running local-path-provisioner 0 1a91feb1e22d1
Add additional nodes to the Cluster
To install on worker nodes we should pass:
- K3S_TOKEN or K3S_CLUSTER_SECRET environment variables
You can locate the K3S_TOKEN at /var/lib/rancher/k3s/server/node-token on your server:
$ sudo cat /var/lib/rancher/k3s/server/node-token K102a068f91219cd8647bb55815bd2e6734afaa513562ed47b47d1d14000a3eea35::server:2021cc18e3e53df19c2ee830cddda0a5
The command to add additional node will look similar to this:
curl -sfL https://get.k3s.io | K3S_URL=https://myserver:6443 K3S_TOKEN=XXX sh -
How To Uninstall k3s on Alpine Linux
If you want to remove k3s on your Alpine Linux machine, run the command:
$ sudo k3s-uninstall.sh
Sample execution output:
...... + iface=vethad0f82d8 + '[' -z vethad0f82d8 ] + ip link delete vethad0f82d8 + read ignore iface ignore + iface=veth3b5de91e + '[' -z veth3b5de91e ] + ip link delete veth3b5de91e + read ignore iface ignore + iface=veth99b636ef + '[' -z veth99b636ef ] + ip link delete veth99b636ef + read ignore iface ignore + ip link delete cni0 + ip link delete flannel.1 + rm -rf /var/lib/cni/ + iptables-restore + grep -v CNI- + grep -v KUBE- + iptables-save + which systemctl + which rc-update /sbin/rc-update + rc-update delete k3s default * service k3s removed from runlevel default + rm -f /etc/init.d/k3s + rm -f /etc/rancher/k3s/k3s.env + trap remove_uninstall EXIT + '[' -L /usr/local/bin/kubectl ] + rm -f /usr/local/bin/kubectl + '[' -L /usr/local/bin/crictl ] + rm -f /usr/local/bin/crictl + '[' -L /usr/local/bin/ctr ] + rm -f /usr/local/bin/ctr + rm -rf /etc/rancher/k3s + rm -rf /var/lib/rancher/k3s + rm -rf /var/lib/kubelet rm: can't remove '/var/lib/kubelet': Resource busy + rm -f /usr/local/bin/k3s + rm -f /usr/local/bin/k3s-killall.sh + remove_uninstall + rm -f /usr/local/bin/k3s-uninstall.sh
Visit project documentation page for more reading.