Setting Up a Multi-Node Kubernetes Cluster Automatically with Jenkins

Ayushmaan Srivastav
4 min readOct 25, 2024

--

Creating a multi-node Kubernetes cluster can be complex, but automating the process with Jenkins can make it quick, repeatable, and manageable. In this guide, we’ll walk through the steps to create a Jenkins job that provisions a multi-node Kubernetes cluster.

Prerequisites

  1. Jenkins Installed: Ensure Jenkins is up and running, with administrator access.
  2. Ansible Installed on Jenkins Server: We’ll use Ansible for provisioning nodes and configuring Kubernetes.
  3. Provisioned VMs or Instances: Virtual machines or cloud instances (e.g., AWS, Google Cloud) are needed for the master and worker nodes.
  4. SSH Access Configured: Ensure SSH access between Jenkins and the nodes.

Step-by-Step Guide

Step 1: Install Jenkins Plugins

  1. In Jenkins, go to Manage JenkinsManage Plugins.
  2. Search for and install Ansible and SSH plugins for easy node provisioning.

Step 2: Create an Ansible Playbook to Set Up the Kubernetes Cluster

Create an Ansible playbook to install and configure Kubernetes across nodes. Save this file as k8s-cluster-setup.yml:

# k8s-cluster-setup.yml
---
- hosts: all
become: yes
tasks:
- name: Update apt repositories
apt:
update_cache: yes

- name: Install Docker
apt:
name: docker.io
state: present

- name: Install Kubernetes dependencies
apt:
name: ['apt-transport-https', 'ca-certificates', 'curl']
state: present

- name: Add Kubernetes apt key
command: curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -

- name: Add Kubernetes repository
apt_repository:
repo: deb https://apt.kubernetes.io/ kubernetes-xenial main
state: present

- name: Install kubelet, kubeadm, and kubectl
apt:
name: ['kubelet', 'kubeadm', 'kubectl']
state: present

- hosts: master
become: yes
tasks:
- name: Initialize Kubernetes cluster on master node
command: kubeadm init --pod-network-cidr=192.168.0.0/16
register: init_output

- name: Save kubeadm join command for workers
shell: echo "{{ init_output.stdout_lines | join(' ') }}" > /tmp/kubeadm_join_cmd.sh

- name: Copy Kubernetes config to default user
command: "{{ item }}"
loop:
- mkdir -p $HOME/.kube
- cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
- chown $(id -u):$(id -g) $HOME/.kube/config

- hosts: workers
become: yes
tasks:
- name: Join the Kubernetes cluster
command: bash /tmp/kubeadm_join_cmd.sh

This playbook will:

  1. Install Docker on all nodes.
  2. Install Kubernetes tools (kubeadm, kubelet, kubectl) on all nodes.
  3. Initialize the master node and create a join command for worker nodes.
  4. Join worker nodes to the master.

Step 3: Create the Jenkins Job

  1. Go to the Jenkins dashboard, click on New Item, enter a job name (e.g., “Multi-Node-K8s-Cluster-Setup”), and select Freestyle project.
  2. Under Build Environment, check Provide Node & SSH access if using the SSH plugin to configure node connections.
  3. Go to Build and add a build step with Invoke Ansible Playbook.
  4. In the playbook configuration:
  • Specify the path to the k8s-cluster-setup.yml file.
  • Set the inventory file that lists master and worker IPs (e.g., hosts).
  • Choose Credentials for SSH access if prompted.

Step 4: Create the Ansible Inventory File for Hosts

Create an inventory file that Ansible will use to know which nodes to configure.

# inventory
[master]
192.168.1.100 ansible_user=root ansible_ssh_private_key_file=/path/to/private/key

[workers]
192.168.1.101 ansible_user=root ansible_ssh_private_key_file=/path/to/private/key
192.168.1.102 ansible_user=root ansible_ssh_private_key_file=/path/to/private/key

Step 5: Set up Network Plugin for Kubernetes

To allow pods to communicate across the cluster, configure a network plugin. Add the following lines to the Ansible playbook under the master section:

- name: Apply network plugin
command: kubectl apply -f https://docs.projectcalico.org/v3.14/manifests/calico.yaml

The Calico network plugin will allow pod-to-pod communication and external access.

Step 6: Save and Run the Jenkins Job

  1. Click Save to save your Jenkins job configuration.
  2. Go back to the main job page and click Build Now to run the job.

Step 7: Monitor the Output

  1. Click Console Output to view the progress.
  2. Check that each node is being configured with Docker, Kubernetes, and that the master node is initialized.
  3. Once complete, verify that the nodes have joined the cluster by running:
kubectl get nodes

Verifying Cluster Setup

On the master node, check the status of the nodes and pods to ensure the cluster is running correctly:

kubectl get nodes
kubectl get pods --all-namespaces

This command should display all nodes and the pods running within your cluster, showing the worker nodes successfully joined to the master.

Benefits of Automating Kubernetes Cluster Setup with Jenkins

  1. Consistency: Automated setups ensure all nodes have consistent configurations.
  2. Scalability: Easily scale up by adding nodes to the inventory file.
  3. Time-Saving: Reduces manual steps, saving time and reducing errors.
  4. Reusability: Jenkins jobs can be reused for additional environments (e.g., staging, production).

Conclusion

With this setup, Jenkins automatically creates a multi-node Kubernetes cluster, providing a scalable and automated environment for deploying containerized applications. This configuration is ideal for companies looking to streamline their CI/CD pipeline with minimal manual intervention, allowing DevOps teams to focus on more critical tasks.

--

--

No responses yet