timothystewart6
k3s-ansible
Jinja

The easiest way to bootstrap a self-hosted High Availability Kubernetes cluster. A fully automated HA k3s etcd install with kube-vip, MetalLB, and more. Build. Destroy. Repeat.

Last updated Jul 7, 2026
3.0k
Stars
1.2k
Forks
46
Issues
+3
Stars/day
Attention Score
95
Language breakdown
No language data available.
Files click to expand
README

Automated build of HA k3s Cluster with kube-vip and MetalLB

Fully Automated K3S etcd High Availability Install

This playbook will build an HA Kubernetes cluster with k3s, kube-vip and MetalLB via ansible.

This is based on the work from this fork which is based on the work from k3s-io/k3s-ansible. It uses kube-vip to create a load balancer for control plane, and metal-lb for its service LoadBalancer.

If you want more context on how this works, see:

📄 Documentation (including example commands)

📺 Watch the Video

📖 k3s Ansible Playbook

Build a Kubernetes cluster using Ansible with k3s. The goal is easily install a HA Kubernetes cluster on machines running:

  • [x] Debian (tested on version 11)
  • [x] Ubuntu (tested on version 22.04)
  • [x] Rocky (tested on version 9)
on processor architecture:
  • [X] x64
  • [X] arm64
  • [X] armhf

✅ System requirements

  • You will also need to install collections that this playbook uses by running ansible-galaxy collection install -r ./collections/requirements.yml (important❗)
  • netaddr package must be available to Ansible. If you have installed Ansible via apt, this is already taken care of. If you have installed Ansible via pip, make sure to install netaddr into the respective virtual environment.
  • server and agent nodes should have passwordless SSH access, if not you can supply arguments to provide credentials --ask-pass --ask-become-pass to each command.

🚀 Getting Started

🍴 Preparation

First create a new directory based on the sample directory within the inventory directory:

cp -R inventory/sample inventory/my-cluster

Second, edit inventory/my-cluster/hosts.ini to match the system information gathered above

For example:

[master]
192.168.30.38
192.168.30.39
192.168.30.40

[node] 192.168.30.41 192.168.30.42

[k3s_cluster:children] master node

If multiple hosts are in the master group, the playbook will automatically set up k3s in HA mode with etcd.

Finally, copy ansible.example.cfg to ansible.cfg and adapt the inventory path to match the files that you just created.

This requires at least k3s version 1.19.1 however the version is configurable by using the k3s_version variable.

If needed, you can also edit inventory/my-cluster/group_vars/all.yml to match your environment.

☸️ Create Cluster

Start provisioning of the cluster using the following command:

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

After deployment control plane will be accessible via virtual ip-address which is defined in inventory/groupvars/all.yml as apiserverendpoint

🔥 Remove k3s cluster

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

>You should also reboot these nodes due to the VIP not being destroyed

⚙️ Kube Config

To copy your kube config locally so that you can access your Kubernetes cluster run:

scp debian@master_ip:/etc/rancher/k3s/k3s.yaml ~/.kube/config
If you get file Permission denied, go into the node and temporarly run:
sudo chmod 777 /etc/rancher/k3s/k3s.yaml
Then copy with the scp command and reset the permissions back to:
sudo chmod 600 /etc/rancher/k3s/k3s.yaml

You'll then want to modify the config to point to master IP by running:

sudo nano ~/.kube/config
Then change server: https://127.0.0.1:6443 to match your master IP: server: https://192.168.1.222:6443

🔨 Testing your cluster

See the commands here.

Variables

| Role(s) | Variable | Type | Default | Required | Description | |---|---|---|---|---|---| | download | k3s_version | string | ❌ | Required | K3s binaries version | | k3sagent, k3sserver, k3sserverpost | apiserver_endpoint | string | ❌ | Required | Virtual ip-address configured on each master | | k3sagent | extraagent_args | string | null | Not required | Extra arguments for agents nodes | | k3sagent, k3sserver | groupnamemaster | string | null | Not required | Name othe master group | | k3sagent | k3stoken | string | null | Not required | Token used to communicate between masters | | k3sagent, k3sserver | proxy_env | dict | null | Not required | Internet proxy configurations | | k3sagent, k3sserver | proxyenv.HTTPPROXY | string | ❌ | Required | HTTP internet proxy | | k3sagent, k3sserver | proxyenv.HTTPSPROXY | string | ❌ | Required | HTTP internet proxy | | k3sagent, k3sserver | proxyenv.NOPROXY | string | ❌ | Required | Addresses that will not use the proxies | | k3sagent, k3sserver, reset | systemd_dir | string | /etc/systemd/system | Not required | Path to systemd services | | k3scustomregistries | customregistriesyaml | string | ❌ | Required | YAML block defining custom registries. The following is an example that pulls all images used in this playbook through your private registries. It also allows you to pull your own images from your private registry, without having to use imagePullSecrets in your deployments. If all you need is your own images and you don't care about caching the docker/quay/ghcr.io images, you can just remove those from the mirrors: section. | | k3sserver, k3sserverpost | ciliumbgp | bool | ~ | Not required | Enable cilium BGP control plane for LB services and pod cidrs. Disables the use of MetalLB. | | k3sserver, k3sserverpost | ciliumiface | string | ❌ | Not required | The network interface used for when Cilium is enabled | | k3sserver | extraserver_args | string | "" | Not required | Extra arguments for server nodes | | k3sserver | k3screatekubectlsymlink | bool | false | Not required | Create the kubectl -> k3s symlink | | k3sserver | k3screatecrictlsymlink | bool | true | Not required | Create the crictl -> k3s symlink | | k3sserver | kubevip_arp | bool | true | Not required | Enables kube-vip ARP broadcasts | | k3sserver | kubevip_bgp | bool | false | Not required | Enables kube-vip BGP peering | | k3sserver | kubevipbgprouterid | string | "127.0.0.1" | Not required | Defines the router ID for the kube-vip BGP server | | k3sserver | kubevipbgpas | string | "64513" | Not required | Defines the AS for the kube-vip BGP server | | k3sserver | kubevipbgppeeraddress | string | "192.168.30.1" | Not required | Defines the address for the kube-vip BGP peer | | k3sserver | kubevipbgppeeras | string | "64512" | Not required | Defines the AS for the kube-vip BGP peer | | k3sserver | kubevipbgppeers | list | [] | Not required | List of BGP peer ASN & address pairs | | k3sserver | kubevipbgppeersgroups | list | ['k3smaster'] | Not required | Inventory group in which to search for additional kubevipbgp_peers parameters to merge. | | k3sserver | kubevip_iface | string | ~ | Not required | Explicitly define an interface that ALL control nodes should use to propagate the VIP, define it here. Otherwise, kube-vip will determine the right interface automatically at runtime. | | k3sserver | kubeviptagversion | string | v0.7.2 | Not required | Image tag for kube-vip | | k3sserver | kubevipcloudprovidertagversion | string | main | Not required | Tag for kube-vip-cloud-provider manifest when enable | | k3sserver, k3serverpost | kubeviplbip_range | string | ~ | Not required | IP range for kube-vip load balancer | | k3sserver, k3sserverpost | metallbcontrollertag_version | string | v0.14.3 | Not required | Image tag for MetalLB | | k3sserver | metallbspeakertag_version | string | v0.14.3 | Not required | Image tag for MetalLB | | k3sserver | metallb_type | string | native | Not required | Use FRR mode or native. Valid values are frr and native | | k3sserver | retrycount | int | 20 | Not required | Amount of retries when verifying that nodes joined | | k3sserver | serverinit_args | string | ❌ | Not required | Arguments for server nodes | | k3sserverpost | bpflbalgorithm | string | maglev | Not required | BPF lb algorithm | | k3sserverpost | bpflbmode | string | hybrid | Not required | BPF lb mode | | k3sserverpost | calico_blocksize | int | 26 | Not required | IP pool block size | | k3sserverpost | calico_ebpf | bool | false | Not required | Use eBPF dataplane instead of iptables | | k3sserverpost | calico_encapsulation | string | VXLANCrossSubnet | Not required | IP pool encapsulation | | k3sserverpost | calico_natOutgoing | string | Enabled | Not required | IP pool NAT outgoing | | k3sserverpost | calico_nodeSelector | string | all() | Not required | IP pool node selector | | k3sserverpost | calico_iface | string | ~ | Not required | The network interface used for when Calico is enabled | | k3sserverpost | calico_tag | string | v3.27.2 | Not required | Calico version tag | | k3sserverpost | ciliumbgpmy_asn | int | 64513 | Not required | Local ASN for BGP peer | | k3sserverpost | ciliumbgppeer_asn | int | 64512 | Not required | BGP peer ASN | | k3sserverpost | ciliumbgppeer_address | string | ~ | Not required | BGP peer address | | k3sserverpost | ciliumbgpneighbors | list | [] | Not required | List of BGP peer ASN & address pairs | | k3sserverpost | ciliumbgpneighborsgroups | list | ['k3sall'] | Not required | Inventory group in which to search for additional ciliumbgpneighbors parameters to merge. | | k3sserverpost | ciliumbgplb_cidr | string | 192.168.31.0/24 | Not required | BGP load balancer IP range | | k3sserverpost | cilium_exportPodCIDR | bool | true | Not required | Export pod CIDR | | k3sserverpost | cilium_hubble | bool | true | Not required | Enable Cilium Hubble | | k3sserverpost | cilium_hubble | bool | true | Not required | Enable Cilium Hubble | | k3sserverpost | cilium_mode | string | native | Not required | Inner-node communication mode (choices are native and routed) | | k3sserverpost | cluster_cidr | string | 10.52.0.0/16 | Not required | Inner-cluster IP range | | k3sserverpost | enablebpfmasquerade | bool | true | Not required | Use IP masquerading | | k3sserverpost | kubeproxyreplacement | bool | true | Not required | Replace the native kube-proxy with Cilium | | k3sserverpost | metallbavailable_timeout | string | 240s | Not required | Wait for MetalLB resources | | k3sserverpost | metallbip_range | string | 192.168.30.80-192.168.30.90 | Not required | MetalLB ip range for load balancer | | k3sserverpost | metallbcontrollertagversion | string | v0.14.3 | Not required | Image tag for MetalLB | | k3sserverpost | metallbmode | string | layer2 | Not required | Metallb mode (choices are bgp and layer2) | | k3sserverpost | metallbbgpmyasn | string | ~ | Not required | BGP ASN configurations | | k3sserverpost | metallbbgppeerasn | string | ~ | Not required | BGP peer ASN configurations | | k3sserverpost | metallbbgppeeraddress | string | ~ | Not required | BGP peer address | | lxc | customrebootcommand | string | ~ | Not required | Command to run on reboot | | prereq | system_timezone | string | null | Not required | Timezone to be set on all nodes | | proxmoxlxc, resetproxmoxlxc | proxmoxlxcctids | list | ❌ | Required | Proxmox container ID list | | raspberrypi | state | string | present | Not required | Indicates whether the k3s prerequisites for Raspberry Pi should be set up (possible values are present and absent) |

Troubleshooting

Be sure to see this post on how to troubleshoot common problems

Testing the playbook using molecule

This playbook includes a molecule-based test setup. It is run automatically in CI, but you can also run the tests locally. This might be helpful for quick feedback in a few cases. You can find more information about it here.

Pre-commit Hooks

This repo uses pre-commit and pre-commit-hooks to lint and fix common style and syntax errors. Be sure to install python packages and then run pre-commit install. For more information, see pre-commit

🌌 Ansible Galaxy

This collection can now be used in larger ansible projects.

Instructions:

  • create or modify a file collections/requirements.yml in your project
collections:
  - name: ansible.utils
  - name: community.general
  - name: ansible.posix
  - name: kubernetes.core
  - name: https://github.com/timothystewart6/k3s-ansible.git
    type: git
    version: master
  • install via ansible-galaxy collection install -r ./collections/requirements.yml
  • every role is now available via the prefix technotim.k3sansible. e.g. technotim.k3sansible.lxc

Thanks 🤝

This repo is really standing on the shoulders of giants. Thank you to all those who have contributed and thanks to these repos for code and ideas:

🔗 More in this category

© 2026 GitRepoTrend · timothystewart6/k3s-ansible · Updated daily from GitHub