Introduction
Kubernetes has become the go-to solution for container orchestration, allowing developers to easily deploy, manage, and scale containerized applications. However, setting up a Kubernetes cluster can be a complex process, involving numerous steps and configurations. This is where Ansible, an open source automation tool, comes in. By automating the deployment process, Ansible can make building a Kubernetes cluster much easier.
In this guide we will walk you through the process of creating a Kubernetes cluster using Ansible. It is assumed that all nodes in the K8 cluster are running the Ubuntu Linux operating system.
Prerequisites
Before we dive into the setup process, make sure you have the following prerequisites:
- Ansible - Ansible must be installed on the control machine. The control machine is where you will run your Ansible scripts and can be your local machine or a server.
- Target nodes: These are the machines where Kubernetes will be installed. You will need at least two nodes: one for the master node and one for a worker node. Make sure these nodes are running a compatible Linux distribution.
- SSH access: Ansible communicates with target nodes via SSH. SSH access must be configured for all target nodes from the control machine.
Step 1: Installing Ansible
To get started, set up Ansible on your control machine. The process involves installing Ansible, which varies depending on the operating system. Use the following commands depending on your distribution:
sudo apt-get update
sudo apt-get install ansible
Step 2: Set up your inventory file
After installing Ansible, proceed with configuring the inventory file. Create a file called host.ini in your working directory, which will enumerate all the target nodes along with their IP addresses. To improve clarity, organize them into groups such as masters and workers. Here is an example:
[masters]
master ansible_host=192.168.1.100
[workers]
worker1 ansible_host=192.168.1.101
worker2 ansible_host=192.168.1.102
Step 3: Configuring the K8s Playbook Cluster
This playbook will help you set up a Kubernetes cluster using Ubuntu systems. Includes tasks to disable the firewall, disable swapping, configure network settings, add the Docker repository, install Docker and its components, adjust container runtime settings, add the Kubernetes repository, and install Kubernetes components (kubelet, kubeadm, kubectl). Specifically, kubectl is installed only on nodes identified as master. The playbook demonstrates a comprehensive approach to preparing systems for Kubernetes, focusing on required services, security configurations, and essential Kubernetes components.
Create a setup-playbook.yaml file and add the following content:
- name: Initialize master and worker nodes
hosts: all
tasks:
- name: disable UFW firewall for labs
service:
name: ufw
state: stopped
enabled: false
- name: Disable SWAP
shell: |
swapoff -a
- name: Disable SWAP in fstab
lineinfile:
path: /etc/fstab
regexp: '^.*swap.*$'
line: '#\0'
backrefs: yes
- name: ensure net.bridge.bridge-nf-call-ip6tables is set to 1
sysctl:
name: net.bridge.bridge-nf-call-iptables
value: '1'
state: present
reload: yes
- name: Installation of apt-utils
apt:
name: apt-transport-https
state: present
update_cache: yes
- name: Adding Docker GPG key
ansible.builtin.apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Adding Docker Repository
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable
state: present
- name: Installation of Docker
apt:
name: "{{ item }}"
state: present
loop:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-compose
- name: Setting value of SystemdCgroup
shell: |
containerd config default | sudo tee /etc/containerd/config.toml | grep SystemdCgroup
sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml
- name: Starting Service of Docker
service:
name: docker
state: started
enabled: yes
- name: Add Kubernetes apt repository
apt_repository:
repo: deb https://apt.kubernetes.io/kubernetes-xenial main
state: present
- name: Install kubelet and kubeadm
apt:
name: "{{ item }}"
state: present
loop:
- kubeadm
- kubelet
- name: start kubelet
service:
name: kubelet
enabled: yes
state: started
- name: install kubectl
apt:
name: kubectl
state: present
when: "'masters' in group_names"
Run the playbook by running:
ansible-playbook -i hosts.ini setup-playbook.yml
Step 4: Configuring the Master Node
This Ansible playbook is designed to configure the master node of a Kubernetes cluster. Initialize the Kubernetes cluster with specific network configurations, create a .kube directory, and copy the kube configuration file to the user's home directory for cluster management. Also install the Calico networking plugin to manage the pod network, ensuring that pods can communicate with each other across nodes. The playbook automates the initial setup process, simplifying the deployment of a Kubernetes cluster.
Create a file named master-playbook.yml and add the contents below:
- name: Configuration of master node
hosts: masters
tasks:
- name: initialize K8S cluster
shell: kubeadm init --pod-network-cidr=172.16.0.0/16 --apiserver-advertise-address=192.168.100.5 --ignore-preflight-errors=all
- name: create.kube directoryi and copy kube config file
shell: "{{ item }}"
loop:
- mkdir -p $HOME/.kube
- cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
- chown $(id -u):$(id -g) $HOME/.kube/config
- name: install Pod network
become: yes
shell: kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/calico.yaml >> pod_network_setup.txt
args:
chdir: $HOME
creates: pod_network_setup.txt
Run the playbook with the command:
ansible-playbook -i hosts.ini master-playbook.yml
Step 5: Configure the worker node playbook
This playbook is used to automate the process of merging Kubernetes nodes. It runs on all nodes, generating a join command on the master node and distributing it to the worker nodes. Initially, it retrieves the join command using kubeadm token create and stores it. Then, set a fact with this command to use on worker nodes. Finally, worker nodes use this command to join the Kubernetes cluster, bypassing preflight checks. This playbook simplifies the cluster expansion process by automating the node merging process.
Create a file named worker-playbook.yml and add the contents below:
- name: Generating token on master node and token deployment to worker node
hosts: all
gather_facts: false
tasks:
- name: get join command
shell: kubeadm token create --print-join-command
register: join_command_raw
when: "'masters' in group_names"
- name: set join command
set_fact:
join_command: "{{ join_command_raw.stdout_lines[0] }}"
when: "'masters' in group_names"
- name: join cluster
shell: "{{ hostvars['master'].join_command }} --ignore-preflight-errors all >> node_joined.txt"
args:
chdir: $HOME
creates: node_joined.txt
when: "'workers' in group_names"
Run the playbook with the command:
ansible-playbook -i hosts.ini worker-playbook.yml
Step 5: Verify the cluster
Once you run the playbooks, you can verify that your Kubernetes cluster is configured correctly.
On the master node, run:
kubectl get nodes
You should see all nodes listed with their status as Ready.
Conclusion
By following these steps, you have successfully configured a Kubernetes cluster using Ansible. This method offers a scalable and repeatable process, making it easier to manage and automate the deployment of Kubernetes clusters. As you become more familiar with Ansible and Kubernetes, you can further customize your playbooks to fit your specific requirements, such as adding more worker nodes or configuring advanced features. Happy orchestration!