Creating a Kubernetes/OpenShift Cluster Using Ansible: A Step-by-Step Guide

Ayushmaan Srivastav
4 min readOct 26, 2024

--

Setting up a Kubernetes or OpenShift cluster can be a complex and time-consuming task, especially in production environments. Fortunately, with the power of Ansible, we can automate the process and simplify cluster management. In this blog, we’ll explore how to create a Kubernetes/OpenShift cluster using Ansible, step by step.

Why Use Ansible for Cluster Management?

Ansible offers numerous benefits for managing Kubernetes/OpenShift clusters:

  • Automation: Reduces manual intervention and speeds up deployment.
  • Consistency: Ensures uniformity across different environments.
  • Scalability: Easily add or remove nodes from the cluster.
  • Idempotency: Ansible playbooks can be re-run without causing side effects, maintaining the desired state.

Prerequisites

Before you start, ensure the following:

  1. Ansible Installed: Make sure you have Ansible installed on your local machine. You can install it using pip:
pip install ansible

2. Access to Servers: You need access to multiple servers where the Kubernetes/OpenShift nodes will be deployed. These can be virtual machines or bare metal servers.

3. SSH Access: Ensure SSH access is configured for these servers.

4. Required Packages: Install necessary packages like curl, docker, and kubelet on all nodes.

5. Inventory File: Create an Ansible inventory file to define your nodes.

Step-by-Step Guide to Create a Kubernetes Cluster Using Ansible

Step 1: Create the Ansible Inventory File

Create a file named inventory.ini to define your cluster nodes.

[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

Next, create an Ansible playbook to install and configure Kubernetes. Create a file named 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:

  1. Installing Required Packages: The playbook first installs necessary packages, including Docker and Kubernetes components.
  2. Docker Installation: It adds the Docker GPG key and repository, installs Docker, and ensures that the Docker service is enabled and started.
  3. Kubernetes Components: It installs kubelet, kubeadm, and kubectl, which are essential for Kubernetes.
  4. Disabling Swap: Disables swap as it is not supported by Kubernetes.
  5. Cluster Initialization: The master node initializes the cluster with kubeadm init, specifying the Pod network CIDR for Flannel.
  6. Kube Config: It copies the admin configuration to the user’s home directory on the master node.
  7. Network Plugin Installation: It installs the Flannel network plugin for Kubernetes.
  8. Joining Worker Nodes: Worker nodes join the cluster using the command provided during the initialization of the master node.

Step 3: Run the Ansible Playbook

With the inventory and playbook ready, you can now run the playbook:

ansible-playbook -i inventory.ini k8s-setup.yml

This command will connect to the specified hosts in your inventory and execute the tasks defined in the playbook, resulting in a fully functional Kubernetes cluster.

Step 4: Verify the Cluster

After the playbook execution completes, you can verify your cluster by running the following command from the master node:

kubectl get nodes

This should list all nodes in the cluster, indicating they are in a “Ready” state.

Creating an OpenShift Cluster

If you want to create an OpenShift cluster instead of Kubernetes, you can use the OpenShift Ansible Installer. The process is similar but requires the OpenShift installer and some modifications to the playbook and commands.

Conclusion

Automating the creation of a Kubernetes/OpenShift cluster with Ansible significantly simplifies the deployment process and ensures consistency across environments. With just a few lines of YAML, you can set up a complete cluster ready for deployment.

--

--

Responses (1)