Creating a Kubernetes or OpenShift cluster using Ansible involves several steps. Below, I'll provide a general outline for setting up a Kubernetes cluster, along with an example playbook to get you started. For OpenShift, the process is similar but requires specific OpenShift installation tools and configurations.
Prerequisites
Ansible installed on your local machine.
SSH access to the target servers (nodes) where you want to deploy the cluster.
Necessary system packages installed (e.g., Docker, Kubernetes tools).
Steps to Create a Kubernetes Cluster Using Ansible
Prepare the Inventory File: Define your master and worker nodes.
Create the Playbook: Write an Ansible playbook that:
Installs required packages.
Configures the Kubernetes cluster.
Initializes the master node.
Joins worker nodes.
Example Inventory File (inventory.ini
)
[kubernetes-master]
master ansible_host=<master-ip>
[kubernetes-worker]
worker1 ansible_host=<worker1-ip>
worker2 ansible_host=<worker2-ip>
[kubernetes:children]
kubernetes-master
kubernetes-worker
Example Ansible Playbook (k8s_setup.yml
)
---
- name: Set up Kubernetes Cluster
hosts: kubernetes
become: true
tasks:
- name: Install required packages
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
state: present
when: ansible_os_family == "Debian"
- name: Add Kubernetes APT repository
apt_repository:
repo: "deb https://apt.kubernetes.io/ kubernetes-xenial main"
state: present
- name: Install Kubernetes components
apt:
name:
- kubelet
- kubeadm
- kubectl
state: present
when: ansible_os_family == "Debian"
- name: Disable swap (required by Kubernetes)
command: swapoff -a
- name: Initialize Kubernetes master
command: kubeadm init --pod-network-cidr=10.244.0.0/16
when: inventory_hostname == "master"
register: kubeadm_init
- name: Create admin config
command: >
mkdir -p $HOME/.kube &&
cp /etc/kubernetes/admin.conf $HOME/.kube/config &&
chown $(id -u):$(id -g) $HOME/.kube/config
when: inventory_hostname == "master"
- name: Install Flannel CNI
command: kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/k8s-manifest.yaml
when: inventory_hostname == "master"
- name: Join worker nodes
command: kubeadm join {{ hostvars['master']['ansible_host'] }}:6443 --token {{ kubeadm_init.stdout_lines[2].split()[1] }} --discovery-token-ca-cert-hash {{ kubeadm_init.stdout_lines[2].split()[4] }}
when: inventory_hostname != "master"
Running the Playbook
Save your inventory file and playbook in a directory.
Execute the playbook with the following command:
ansible-playbook -i inventory.ini k8s_setup.yml
OpenShift Setup
For an OpenShift cluster, you would typically use the OpenShift Installer rather than Ansible alone. OpenShift can also be deployed using Ansible playbooks, but it requires the OpenShift Ansible repository.
Note:
Ensure that your servers meet the Kubernetes prerequisites (e.g., network settings, resource limits).
Modify the
pod-network-cidr
in the Kubernetes playbook according to the network plugin you are using (Flannel, Calico, etc.).This example uses Ubuntu as the operating system; modify the package manager and commands accordingly if using a different OS.