This repository contains Ansible playbooks and roles to automate the provisioning and management of a local Kubernetes cluster using kubeadm. It is designed to bootstrap a cluster from scratch on bare metal servers or local virtual machines.
.
├── inventory.yml # Defines your control plane and worker nodes (IPs/Hostnames)
├── requirements.yml # Ansible Galaxy collection dependencies
├── roles
│ ├── common # Dependencies (Containerd, swap settings, kernel modules)
│ ├── control-plane # Initializes the cluster (kubeadm init) and networking (Flannel)
│ └── worker # Joins nodes to the cluster (kubeadm join)
└── site.yml # Main playbook entry point
Before running the playbooks, ensure the following:
- Ansible Installed: You need Ansible installed on your control machine.
- Target Machines: You should have at least 2 Linux VMs (Ubuntu/Debian) ready.
- SSH Access: Passwordless SSH access (keys) configured from your control machine to the target nodes.
- Sudo Privileges: The user connecting via SSH must have passwordless sudo privileges.
Edit the `inventory.yml file to match your local network setup.
Crucial: You must update the ansible_user and ansible_ssh_private_key_file variables to match your environment:
vars:
ansible_ssh_private_key_file: ~/.ssh/your_key.pub
ansible_user: your_usernameInstall the required Ansible collections (Posix and Community General) defined in requirements.yml:
ansible-galaxy install -r requirements.ymlVerify that Ansible can talk to your nodes:
ansible all -i inventory.yml -m pingExecute the main playbook to set up the cluster:
ansible-playbook -i inventory.yml site.ymlNote: This process may take several minutes depending on your internet connection speed, as it downloads required binaries and container images.
- Disables Swap (required by Kubelet).
- Installs Container Runtime (e.g., Containerd or Docker).
- Installs
kubelet,kubeadm, andkubectl. - Configures necessary kernel modules and sysctl params.
- Runs
kubeadm initon the primary node. - Sets up the
.kubeconfig directory for the user. - Installs the Pod Network Addon (e.g., Calico or Flannel).
- Generates the join command for workers.
- Retrieves the join token from the control plane.
- Runs
kubeadm jointo connect the worker to the cluster.
Once the playbook finishes, SSH into your control plane node and run:
kubectl get nodesYou should see your control plane and worker nodes with a status of Ready.