Sling Academy
Home/DevOps/How to Set Up a CI/CD Pipeline in Kubernetes

How to Set Up a CI/CD Pipeline in Kubernetes

Last updated: February 01, 2024

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.

Next Article: Using GitHub Actions with Kubernetes: A Developer’s Guide

Previous Article: Kubernetes – Managing Dependencies and Versions in Helm

Series: Kubernetes Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide