How to Set Up a CI/CD Pipeline in Kubernetes

Updated: February 1, 2024 By: Guest Contributor Post a comment

Introduction

Continuous Integration and Continuous Deployment (CI/CD) are key practices in modern software development that allow for the automated integration and testing of code changes, followed by automated deployment to production environments. Kubernetes, a powerful container orchestration system, provides an excellent platform for running CI/CD pipelines. In this tutorial, you will learn how to set up a basic CI/CD pipeline in a Kubernetes cluster.

Prerequisites

  • A Kubernetes cluster
  • kubectl command-line tool, configured to communicate with your cluster
  • A Docker Hub account, or a private container registry
  • Basic understanding of Kubernetes concepts (pods, services, deployments)
  • Basic familiarity with CI/CD principles

Tools for CI/CD in Kubernetes

There are several tools you can use to facilitate CI/CD in Kubernetes; in this tutorial, we’ll focus on Jenkins, Helm, and Skaffold for simplicity and because they have extensive community support.

Installing Jenkins

First, install Jenkins, which will be the centerpiece of our CI/CD pipeline:


# Install Jenkins using Helm
helm repo add jenkinsci https://charts.jenkins.io
helm repo update
helm install jenkins jenkinsci/jenkins

Please check Jenkins documentation for the most recent instructions.

Setting Up a Jenkins Pipeline

Once Jenkins is running, you’ll need to configure a Jenkins pipeline. Create a ‘Jenkinsfile’ in your project’s root directory which defines your pipeline stages:


pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'echo Building...'
                sh './build.sh'
            }
        }
        stage('Test') {
            steps {
                sh 'echo Testing...'
                sh './test.sh'
            }
        }
        stage('Deploy') {
            steps {
                sh 'echo Deploying...'
                sh './deploy.sh'
            }
        }
    }
}

For the sake of this tutorial, we’re using shell scripts within our Jenkins pipeline for simplicity. In a real-world scenario, you’d likely be building container images and using test frameworks.

Building and Pushing Container Images

During the build phase, you should build your application’s Docker image:


sh 'docker build -t myapp:${BUILD_NUMBER} .'
sh 'docker push myapp:${BUILD_NUMBER}'

Here, we’re tagging our Docker image with the Jenkins build number. Pushing the image to a registry ensures that it can be pulled during deployment.

Deploying to Kubernetes

For deployment, you can use Helm or kubectl. Here’s an example of how to deploy your app using Helm:


helm upgrade myapp ./chart --install --set image.tag=${BUILD_NUMBER}

Replace ‘./chart’ with the path to your Helm chart. This command updates the app deployment with the new image tag.

Automating the Pipeline

The final part of setting up the CI/CD pipeline is automation. Jenkins supports triggers that can start the pipeline automatically whenever a change is pushed to the source control repository.


triggers {
    pollSCM('* * * * *')
}

The ‘pollSCM’ directive tells Jenkins to poll the source control system at the specified schedule—in this case, every minute.

Conclusion

In this tutorial, we’ve shown you how to set up a CI/CD pipeline using Jenkins, Docker, and Kubernetes. This pipeline includes building your application, running tests, and deploying to a Kubernetes cluster. You’ll need to adjust the commands and scripts to fit your specific application and workflow, but this gives you a place to start. As you progress, consider advanced practices such as canary deployments, integration with other testing tools, and optimizing your pipeline for faster feedback and builds.

Remember, CI/CD is not just about tools and automation; it’s also about the culture and practices that you build around these tools. Always strive for small, frequent, and reliable updates to your applications to better serve your users and stay ahead in the game.