Automating Raspberry Pi K3s provisioning with Ansible

In this article, we will be looking at how we can automate most of the steps involved in installing a Raspberry Pi+K3s cluster by using Ansible.

I explained how to set up a Kubernetes cluster on a Raspberry Pi in my previous article. In case you missed it, you can find it from the following link:

🔗 Building your own private Kubernetes cluster on a Raspberry PI 4 with K3S

If you have a fresh bunch of Raspberry Pis, you might want to follow Step 0 through to Step 8 in the above. However, it’s a one-off setup, so please proceed to the following sections if you already have done them.

In a nutshell, this is what we are trying to automate.


Step 1 – Installing Ansible

So what do we need to get started? You need one or more RPis with Raspbian installed (or an OS supported by K3s). We can get to our desired state by using an Ansible playbook.

Ansible is an industry-leading IT automation tool to provision and manage your infrastructure in a declarative form such YAML.

The easiest way to get the steps to installing Ansible would be to head over to the official docs. For most OSs, it can be just installed via pip.

python -m pip install –user ansible

💡 Note: Use the latest version of Python installed on your machine. In my case, I have it set to python3 since Mac OS comes with an older version of Python installed.

Depending on your system and the internet connection, it will a bit of take time to install Ansible. Sometimes even if you installed it, it would still say that it can’t find ansible command. If that’s the case, feel free to follow this link. If you are on a Mac, you could also run a brew install ansible to install ansible.


Step 2 – K3s Ansible Playbook

Without writing everything from the ground up, we will be using the k3s-ansible repo.

git clone [email protected]:k3s-io/k3s-ansible.git

Once you have cloned the repo, you can run the following command to create a new playbook for your RPi cluster provisioning. Make sure to replace my-cluster with the desired name for your cluster. In my case, I called it rpi-cluster

cp -R inventory/sample inventory/my-cluster

Next, we need to map out the nodes and master them in the inventory/my-cluster/hosts.ini file. Update the [master] and [node] attributes accordingly.

[master]
10.0.0.100

[node]
10.0.0.101

[k3s_cluster:children]
master
node

Under [node] attribute you could even specify a range of IPs if they are consecutive like so.

[node]
10.0.0.[101:105]

Check the inventory/my-cluster/group_vars/all.yml to set variables such as k3s_version and ansible_user, depending on your environment.

Step 3 – Running the Ansible Playbook

ansible-playbook site.yml -i inventory/rpi-cluster/hosts.ini –ask-pass


You can now SSH into the Pi master or nodes and run kubectl commands within it.

if you want to uninstall everything that got installed as part of the playbook:

ansible-playbook reset.yml -i inventory/rpi-cluster/hosts.ini –ask-pass

Conclusion

This article looked at how we can automate provisioning our Raspberry Pi with Ansible. If you are new to DevOps and want to automate provisioning systems, this is a great start. A special shoutout to @itwars for putting this together and doing a lot of heavy lifting creating these scripts.

References

https://github.com/k3s-io/k3s-ansible

Leave a Reply

Your email address will not be published. Required fields are marked *