Cloud Native 13 min read

Integrating Kubernetes into CI/CD Pipelines with Jenkins, Ansible, and HAProxy

This tutorial explains how to incorporate Kubernetes into an existing CI/CD pipeline using Jenkins, Ansible, Docker, and HAProxy to achieve high‑availability microservice deployments, seamless production code changes, and scalable container management for both large and small software companies.

Top Architect
Top Architect
Top Architect
Integrating Kubernetes into CI/CD Pipelines with Jenkins, Ansible, and HAProxy

Both large and small software companies deploy thousands of container instances daily, requiring management of scaling complexity. This tutorial explains how to integrate Kubernetes into an existing CI/CD pipeline to achieve high availability and seamless production code changes.

The container‑based microservice architecture changes how development and operations teams test and deploy applications. Tools required include Kubernetes, Jenkins with shared libraries, Git, HAProxy, and Ansible.

CI/CD Installation describes setting up a Jenkins server with Docker, Ansible and kubectl, a three‑node Kubernetes cluster, and an HAProxy load balancer.

Jenkinsfile for the sample shoppingapp microservice defines stages Build, Test, Publish, Pull Playbook Repo, and Deploy, using shared library functions dockerbuild , testcase , imagepush , gitcheckout , and deploytok8s . The code snippets are shown below.

@Library('jenkins-shared-library') _
pipeline {
   agent any
   environment {
       app = 'shoppingapp'
       service = 'shoppingapp-home'
       registryCredential = 'dockerhub'
       dockerImage = ''
       imageid = "deepanmurugan/shoppingapp-home:$BUILD_NUMBER"
   }
   stages {
       stage('Build') {
            steps {
        script {
            dockerImage = dockerbuild(imageid)
        }
        }
        }
       stage('Test') {
           steps {
        testcase()
           }
       }
       stage('Publish') {
           steps{
               script {
            imagepush(imageid)
               }
           }
       }
       stage('Pull Playbook Repo') {
        steps {
        dir('/tmp/ansible-playbooks/') {
            gitcheckout(
                branch: "master",
                repoUrl: "https://github.com/deepanmurugan/Ansible_Playbook.git"
            )
        }
    }
       }
       stage ('Deploy') {
           steps {
           dir('/tmp/ansible-playbooks/') {
               script{
            deploytok8s(imageid,app,service)
               }
           }
           }
       }
   }
}

Shared‑library functions dockerbuild , testcase , imagepush , gitcheckout , and deploytok8s are defined as simple wrappers around Docker, shell commands, and Ansible.

def call(String dockerImage) {
  script {
    docker.build "${dockerImage}"
  }
}

The Dockerfile builds a Python Flask app on an Alpine base image.

FROM python:3.7.3-alpine3.9
RUN mkdir -p /app
WORKDIR /app
COPY ./src/requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt
COPY ./src/ /app
ENV FLASK_APP=server.py
CMD flask run -h 0.0.0.0 -p 5000

Ansible playbook deploy_k8s.yml creates Kubernetes Deployment, Service, and Ingress resources using templates.

- hosts: localhost
  user: ubuntu
  tasks:
  - name: Deploy the service
    k8s:
      state: present
      definition: "{{ lookup('template', 'k8s/{{app_name}}/{{service_name}}/deployment.yml') | from_yaml }}"
      validate_certs: no
      namespace: default
  - name: Deploy the application
    k8s:
      state: present
      validate_certs: no
      namespace: default
      definition: "{{ lookup('template', 'k8s/{{app_name}}/{{service_name}}/service.yml') | from_yaml }}"
  - name: Deploy the Ingress
    k8s:
      state: present
      validate_certs: no
      namespace: default
      definition: "{{ lookup('template', 'k8s/{{app_name}}/common/ingress.yml') | from_yaml }}"

The Kubernetes cluster consists of one master and two worker nodes, with Traefik as the Ingress controller. HAProxy balances traffic across the nodes.

frontend http_front
bind *:80
mode http
default_backend http_back

backend http_back
balance roundrobin
server kube 172.31.35.122:32365 check
server kube 172.31.40.13:32365 check

The end‑to‑end workflow is: developers push code → GitHub webhook triggers Jenkins → Jenkins builds Docker image, pushes to Docker Hub, and runs Ansible playbook to deploy the image to Kubernetes, creating Deployment, Service, and Ingress.

To add a new microservice, create a new repository, copy the existing Jenkins job, adjust the service_name , update the ingress path, and the pipeline will automatically provision the new Deployment and Service.

DockerCI/CDmicroservicesKubernetesJenkinsAnsibleHAProxy
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.