Skip to main content
Installing K3s with Ansible
Overview

Installing K3s with Ansible

1 min read

This covers deploying K3s across your VMs using Ansible. We’re using a fork of TechnoTim’s k3s-ansible repo.

Prerequisites

Install Ansible on your management machine:

Debian/Ubuntu:

Terminal window
sudo apt update && sudo apt install -y ansible

macOS:

Terminal window
brew install ansible

Clone the repo:

Terminal window
git clone https://github.com/meroxdotdev/k3s-ansible

Configuration

Terminal window
cd k3s-ansible
cp ansible.example.cfg ansible.cfg
ansible-galaxy install -r ./collections/requirements.yml
cp -R inventory/sample inventory/my-cluster

inventory/my-cluster/hosts.ini — set your node IPs:

[master]
10.57.57.30
10.57.57.31
10.57.57.32
[node]
10.57.57.33
10.57.57.34
10.57.57.35
[k3s_cluster:children]
master
node

inventory/my-cluster/group_vars/all.yml — key fields to edit:

  • ansible_user: default VM user is ubuntu
  • system_timezone: set to your timezone, e.g. Europe/Bucharest
  • Networking: comment out #flannel_iface: eth0 and use calico_iface: "eth0" for better network policies. Flannel works too if you want something simpler.
  • apiserver_endpoint: 10.57.57.100 — an unused IP on your LAN, acts as the VIP for the K3s control plane
  • k3s_token: any alphanumeric string
  • metal_lb_ip_range: 10.57.57.80-10.57.57.90 — a range on your LAN, outside your DHCP pool, not used by anything else. This is how K3s services get exposed to your network.
Note

Make sure SSH key authentication is working between your management machine and all VMs before running the playbook.

Deploy

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

Once done, pull the kubeconfig and verify:

Terminal window
scp ubuntu@10.57.57.30:~/.kube/config .
mkdir -p ~/.kube
mv config ~/.kube/
kubectl get nodes

Next Steps

Proceed to the Traefik Setup guide.

Share this post