Context
Last week I resumed my journey into Kubernetes by setting up a K3s cluster in my Home Lab. Previously, I managed services using Docker Compose and Portainer, which was fun but required more manual effort to add new nodes.
Having acquired a Dell OptiPlex 3070 and installed Proxmox, I decided to test various approaches to streamline my setup.
Requirements
- At least 3 nodes: one master node and two agent nodes
- Host machine able to connect to all nodes
- Ansible and Git installed on your host machine
Guide
Step 1: Clone the Repository
Clone the k3s-ansible repository from GitHub:
git clone [email protected]:k3s-io/k3s-ansible.git
Step 2: Navigate to Directory
Enter the newly created directory:
cd k3s-ansible
Step 3: Duplicate Inventory File
Create your own inventory file by duplicating the sample:
cp inventory-sample.yml inventory.yml
Step 4: Configure Inventory
Add your server
and agent
hosts in the inventory file:
---
k3s_cluster:
children:
server:
hosts:
192.16.35.11:
agent:
hosts:
192.16.35.12:
192.16.35.13:
Tips:
- To overwrite the default SSH user, add to
vars
:vars: ansible_user: worker
- To use password-based SSH login:
However, using public key for authentication is highly recommended.
vars: ansible_password: PASSWORD ansible_become_password: PASSWORD
Step 5: Set K3s Token
Generate and set a secure token:
vars:
token: aaaaaa
Tip: Generate a random secure token using:
openssl rand -base64 64
Step 6: Run Ansible Playbook
Execute the playbook to set up the K3s cluster:
ansible-playbook playbooks/site.yml -i inventory.yml
Expected output:
PLAY RECAP *********************************************************************
192.16.35.11 : ok=25 changed=0 unreachable=0 failed=0 skipped=62 rescued=0 ignored=0
192.16.35.12 : ok=20 changed=0 unreachable=0 failed=0 skipped=40 rescued=0 ignored=0
192.16.35.13 : ok=20 changed=0 unreachable=0 failed=0 skipped=40 rescued=0 ignored=0
Result
To verify the setup, log in to the master node and run:
kubectl get nodes
Output should list all three nodes (one master, two agents):
NAME STATUS ROLES AGE VERSION
k3s-master Ready control-plane,master 1m v1.22.5+k3s1
k3s-agent-1 Ready <none> 1m v1.22.5+k3s1
k3s-agent-2 Ready <none> 1m v1.22.5+k3s1
Conclusion
You have successfully set up a K3s cluster using Ansible! With your K3s cluster running, you can now explore deploying applications and setting up a CI/CD pipeline.
Happy Kubernetes-ing!