Overview
This guide provides comprehensive instructions for using Ansible to set up a Kubernetes or OpenShift cluster, automating the process to reduce manual intervention, ensure consistency, and scale easily.
Why Use Ansible for Cluster Management?
Ansible offers the following advantages when managing clusters:
Automation: Reduces manual configuration, speeds up deployment.
Consistency: Ensures all nodes are configured identically.
Scalability: Easily add or remove nodes as needed.
Idempotency: Re-running playbooks does not cause duplicate actions, maintaining the desired state.
Prerequisites
Before proceeding, ensure that:
Ansible is Installed: Install with
pip install ansible
.Server Access: Access to multiple servers (either virtual machines or bare-metal) where the Kubernetes/OpenShift nodes will be deployed.
SSH Access: Ensure SSH access is configured for these servers.
Required Packages: Necessary packages, such as
curl
,docker
, andkubelet
, are installed on all nodes.Inventory File: An Ansible inventory file is created to define your cluster nodes.
Step-by-Step Guide to Set Up a Kubernetes Cluster Using Ansible
Step 1: Create the Ansible Inventory File
Create an inventory file named inventory.ini
to define the nodes in the Kubernetes cluster.
Example inventory.ini
:
[k8s_cluster]
master ansible_host=192.168.1.10
node1 ansible_host=192.168.1.11
node2 ansible_host=192.168.1.12
[k8s_cluster:vars]
ansible_ssh_user=your_user
Step 2: Write the Ansible Playbook
Create an Ansible playbook called k8s-setup.yml
with tasks to install and configure Kubernetes components.
Sample k8s-setup.yml
---
- name: Set up Kubernetes Cluster
hosts: k8s_cluster
become: true
tasks:
- name: Install required packages
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
state: present
- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add Docker repository
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable
state: present
- name: Install Docker
apt:
name: docker-ce
state: latest
- name: Enable and start Docker
service:
name: docker
state: started
enabled: true
- name: Install Kubernetes components
apt:
name:
- kubelet
- kubeadm
- kubectl
state: latest
- name: Disable swap (required for Kubernetes)
command: swapoff -a
when: ansible_os_family == "Debian"
- name: Initialize Kubernetes master
command: kubeadm init --pod-network-cidr=10.244.0.0/16
when: inventory_hostname == "master"
register: k8s_init
ignore_errors: true
- name: Copy Kubernetes admin config to user home
command: "{{ item }}"
with_items:
- mkdir -p $HOME/.kube
- cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
- chown $(id -u):$(id -g) $HOME/.kube/config
when: inventory_hostname == "master"
- name: Install Flannel network plugin
command: kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/k8s-manifests/kube-flannel.yml
when: inventory_hostname == "master"
- name: Join worker nodes to the cluster
command: "{{ hostvars['master']['k8s_init']['stdout_lines'][-1] }}"
when: inventory_hostname != "master"
Explanation of the Playbook Tasks
Install Required Packages: Installs essential packages like
apt-transport-https
,curl
, andca-certificates
.Docker Installation: Adds the Docker GPG key and repository, installs Docker, and enables the Docker service.
Install Kubernetes Components: Installs
kubelet
,kubeadm
, andkubectl
.Disable Swap: Kubernetes requires that swap be disabled on all nodes.
Initialize Kubernetes Master Node: Runs
kubeadm init
on the master node, using a specified Pod network CIDR for Flannel.Copy Kubernetes Config to Admin Directory: Copies the Kubernetes configuration to the user’s home directory on the master node.
Install Flannel Network Plugin: Applies Flannel network configuration to the cluster.
Join Worker Nodes: Executes the join command on worker nodes using the command output from the master’s initialization.
Step 3: Run the Ansible Playbook
To execute the playbook and set up your cluster, run:
ansible-playbook -i inventory.ini k8s-setup.yml
This will connect to the specified hosts in your inventory and complete the setup of a Kubernetes cluster.
Step 4: Verify the Cluster
To verify your Kubernetes cluster, run the following command on the master node:
kubectl get nodes
This command should list all nodes in the cluster, showing them in a “Ready” state.
Customizing for OpenShift Clusters
For OpenShift clusters, use the OpenShift Ansible Installer and make modifications to the playbook, such as adjusting for specific OpenShift requirements and using the appropriate OpenShift commands.
Benefits of Using Ansible for Cluster Management
Consistency: Ensures that each node is configured identically every time.
Automation: Reduces repetitive tasks, allowing for fast deployment and updates.
Scalability: Easily modify the inventory and add or remove nodes as needed.
Integration: Seamlessly integrates with other infrastructure automation workflows, making it easier to manage Kubernetes/OpenShift clusters alongside other infrastructure.
Conclusion
Using Ansible to set up and manage Kubernetes or OpenShift clusters simplifies deployment, minimizes human error, and creates a scalable, consistent environment.