Install a multi-master K3S cluster with embedded etcd

2 min read | by Jordi Prats

K3s has added full support for embedded etcd as of release v1.19.5+k3s1, so now we can create a multi-master cluster without the need of having an external MySQL database

To install a cluster with the embedded etcd we can check the docs on the rancher website. For this example we are going to use tree master nodes as follows:

  • rpi-n01: 10.12.16.8
  • rpi-n02: 10.12.16.9
  • rpi-n03: 10.12.16.10

We need to create a token for all the nodes to join the cluster, it can be any string, for example raspi_cluster_token_1234. We are going to bootstrap the cluster as follows:

curl -sfL https://get.k3s.io | sh -s server --cluster-init --token "raspi_cluster_token_1234"
# kubectl --kubeconfig /etc/rancher/k3s/k3s.yaml  get nodes
NAME      STATUS   ROLES                       AGE   VERSION
rpi-n03   Ready    control-plane,etcd,master   71s   v1.21.5+k3s2

On the other nodes we can install K3S using the following command:

curl -sfL https://get.k3s.io | K3S_TOKEN="raspi_cluster_token_1234" sh -s server --server https://10.12.16.10:6443

We need to do this one by one, the cluster will not allow two nodes trying to join the cluster at the same time

Once all the nodes have been installed, we can check again the cluster using kubectl get nodes to check that all of them have successfully joined the cluster and it's status is Ready:

# kubectl --kubeconfig /etc/rancher/k3s/k3s.yaml get nodes
NAME      STATUS   ROLES                       AGE     VERSION
rpi-n01   Ready    control-plane,etcd,master   17s     v1.21.5+k3s2
rpi-n02   Ready    control-plane,etcd,master   9m23s   v1.21.5+k3s2
rpi-n03   Ready    control-plane,etcd,master   31m     v1.21.5+k3s2

Finally, we'll need to update the first node unit file to remove the --cluster-init flag. We can do it like this:

sed -e '/server \\/,$d' -e 's@ExecStart=.*@ExecStart=/usr/local/bin/k3s server@' -i /etc/systemd/system/k3s.service
systemctl daemon-reload

Posted on 08/11/2021