Deploy a k3s Kubernetes Cluster with Ansible

Deploy a k3s Kubernetes Cluster with Ansible

In this post we will deploy Kubernetes with k3s to a 6 node Raspberry Pi Cluster using Ansible.

Im using the code from github.com/rancher/k3s-ansible and we will be using the instructions from their repository.

I have forked that repository for myself which is available at github.com/ruanbekker/k3s-ansible if you want to see what I've changed for my setup.

First clone the repository:

$ git clone https://github.com/rancher/k3s-ansible
$ cd k3s-ansible

Copy their sample directory to something of your own:

$ cp -R inventory/sample inventory/my-cluster

Then edit the inventory/my-cluster/hosts.ini file and populate your node information for the master and agent nodes.

For my case I'm using version v1.18.9+k3s1, using the username pi and extra server args, so I edited inventory/my-cluster/group_vars/all.yml and populated the following:

k3s_version: v1.18.9+k3s1  
ansible_user: pi  
ansible_ssh_private_key_file: ~/.ssh/id_rsa  
ansible_python_interpreter: /usr/bin/python3  
systemd_dir: /etc/systemd/system  
master_ip: "{{ hostvars[groups['master'][0]]['ansible_host'] | default(groups['master'][0]) }}"  
extra_server_args: "--tls-san k3s.mydomain.com --write-kubeconfig-mode 644"  
extra_agent_args: ""  

Then deploy k3s to your cluster using ansible:

$ ansible-playbook site.yml -i inventory/my-cluster/hosts.ini

Copy the config to your workstation:

$ scp pi@k3s-master-ip:~/.kube/config ~/.kube/config

Then test it out to view the nodes:

$ kubectl get nodes --output wide
NAME     STATUS   ROLES    AGE     VERSION        INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                         KERNEL-VERSION   CONTAINER-RUNTIME  
rpi-07   Ready    <none>   7m18s   v1.18.9+k3s1   192.168.0.121   <none>        Raspbian GNU/Linux 10 (buster)   5.4.51-v7l+      containerd://1.3.3-k3s2  
rpi-05   Ready    master   7m44s   v1.18.9+k3s1   192.168.0.119   <none>        Raspbian GNU/Linux 10 (buster)   5.4.51-v7l+      containerd://1.3.3-k3s2  
rpi-02   Ready    <none>   6m53s   v1.18.9+k3s1   192.168.0.116   <none>        Raspbian GNU/Linux 10 (buster)   5.4.51-v7+       containerd://1.3.3-k3s2  
rpi-03   Ready    <none>   6m50s   v1.18.9+k3s1   192.168.0.117   <none>        Raspbian GNU/Linux 10 (buster)   5.4.51-v7+       containerd://1.3.3-k3s2  
rpi-06   Ready    <none>   7m17s   v1.18.9+k3s1   192.168.0.120   <none>        Raspbian GNU/Linux 10 (buster)   5.4.51-v7l+      containerd://1.3.3-k3s2  
rpi-01   Ready    <none>   6m46s   v1.18.9+k3s1   192.168.0.115   <none>        Raspbian GNU/Linux 10 (buster)   5.4.51-v7+       containerd://1.3.3-k3s2