This Ansible playbook is designed to automate the deployment of Kubernetes in a production environment, using HAProxy as the front-facing load balancer to distribute traffic among Kubernetes nodes for high availability and scalability.
Before running this playbook, ensure you have the following prerequisites met:
- Ansible installed on your control machine.
- SSH access configured to all target nodes.
- Target nodes have internet access for downloading necessary packages.
- Python installed on all nodes for Ansible modules to execute properly.
The playbook relies on inventory and variable files for configuration to match your specific environment requirements.
Update the inventory/k8s-servers.ini
file to include the IP addresses or hostnames of your Kubernetes nodes under the appropriate groups. For example:
Customize variables in the inventory/group_vars/all.yml
file (or any other variable file you're using) to fit your deployment settings, such as Kubernetes version, network settings, and HAProxy configuration.
Change your inventory
file to inventory/k8s-servers.ini
and put your hosts in it
For Install kubernetes you need some preinstall task, for preinstall follow these steps
ansible-playbook -i inventory/k8s-servers.ini kubernetes.yml --become --become-method=sudo -t preinstall
After that you should install prerequired packages for kubernetes:
ansible-playbook -i inventory/k8s-servers.ini kubernetes.yml --become --become-method=sudo -t k8s
For initialize kubernetes, follow these steps: (It's for new cluster)
ansible-playbook -i inventory/k8s-servers.ini kubernetes.yml --become --become-method=sudo -t init_k8s
For join master(s) node to cluster, follow these steps:
ansible-playbook -i inventory/k8s-servers.ini kubernetes.yml --become --become-method=sudo -t join_master
For join worker(s) node to cluster, follow these steps:
ansible-playbook -i inventory/k8s-servers.ini kubernetes.yml --become --become-method=sudo -t join_worker
ansible-playbook -i inventory/k8s-servers.ini kubernetes.yml --become --become-method=sudo -t lb
and for finilize use
ansible-playbook -i inventory/k8s-servers.ini kubernetes.yml --become --become-method=sudo -t postinstall
And also you can limit your task to specific host:
ansible-playbook -i inventory/k8s-servers.ini kubernetes.yml --become --become-method=sudo -t join_worker -l k8s-test-worker1
Finaly you should run postinstall task:
ansible-playbook -i inventory/k8s-servers.ini kubernetes.yml --become --become-method=sudo -t postinstall