Automated deployment and management of a K3s Kubernetes cluster using Ansible. This project enables easy setup and teardown of a multi-node K3s cluster, connecting to an external database, and dedicated control plane and agent nodes.
- Automated K3s cluster deployment and uninstallation
- Multi-node support with control plane and agent nodes
- External database integration
- Comprehensive templating for K3s server and agent configurations
- Detailed status reporting and health checks
- Idempotent operations - safe to re-run
-
Configure your inventory and variables (see Configuration section)
-
Install K3s cluster:
ansible-playbook -i hosts k3s-install.yaml -K
-
Uninstall K3s cluster (if needed):
ansible-playbook -i hosts k3s-uninstall.yaml -K
-
Configure your inventory in
hosts:[control_plane] cp1 ansible_host=192.168.1.2 cp2 ansible_host=192.168.1.3 [agents] agent1 ansible_host=192.168.1.4 agent2 ansible_host=192.168.1.5
-
Set K3s version in
group_vars/all.yaml:k3s_version: "v1.31.5+k3s1"
-
Configure K3s server settings in
templates/k3s-server-config.yaml.j2:- Uncomment and set values as needed
- Permanent flags (cannot be changed after first boot):
datastore-endpoint,cluster-cidr,service-cidr,flannel-backend,token, etc. - CNI options: Set
flannel-backend: "none"to use Cilium or Calico instead of Flannel - Component flags: Disable traefik, servicelb, etc. as needed
-
Configure K3s agent settings in
templates/k3s-agent-config.yaml.j2:- Uncomment and set values as needed
- Configure
serverandtokento join the cluster
-
(Optional) For external etcd with TLS, place certificates in
certs/directory:certs/ ├── ca-bundle.crt ├── etcd-client.crt └── etcd-client.keyThese will be automatically copied to
/etc/rancher/k3s/certs/on control plane nodes.
ansible-k3s/
├── certs/ # (Optional) etcd TLS certificates
│ ├── ca-bundle.crt
│ ├── etcd-client.crt
│ └── etcd-client.key
├── group_vars/
│ └── all.yaml # K3s version configuration
├── templates/
│ ├── k3s-server-config.yaml.j2 # K3s server configuration template
│ └── k3s-agent-config.yaml.j2 # K3s agent configuration template
├── hosts # Ansible inventory file
├── k3s-install.yaml # Cluster installation playbook
├── k3s-uninstall.yaml # Cluster uninstallation playbook
├── LICENSE
└── README.md
-
Check node status:
kubectl get nodes
-
Check cluster health:
kubectl get --raw /healthz
Contributions are welcome! Please feel free to submit a Pull Request.
If you encounter any issues or need support, please file an issue on the GitHub repository.
This project is licensed under the GNU GENERAL PUBLIC LICENSE v3.0 - see the LICENSE file for details.