“Kubernetes and CI/CD: Automating your application deployments”

Prateek Kumar
5 min readJan 29, 2023

--

Welcome to the tenth and last blog post in our series “Mastering Kubernetes: A Comprehensive Guide to Container Orchestration”. Continuous integration and continuous deployment (CI/CD) are crucial practices in software development that enable teams to build, test, and deploy their applications more quickly and efficiently. Kubernetes provides the infrastructure to automate the deployment of applications to a cluster of machines, making it an ideal platform for CI/CD. In this blog, we’ll explore how to set up a CI/CD pipeline for your applications on Kubernetes.

What is CI/CD?

CI/CD refers to a set of practices that enable developers to continuously integrate their code changes into a single codebase, build and test the code, and then automatically deploy the code to production environments. This approach helps to ensure that the code is always in a releasable state, and that bugs and security vulnerabilities are caught early in the development cycle.

Why use Kubernetes for CI/CD?

Kubernetes provides a number of features that make it an ideal platform for CI/CD:

  1. Scalability: Kubernetes allows you to scale your application deployments as needed, so you can easily accommodate increases in traffic or load.
  2. Portability: Kubernetes is designed to be cloud-agnostic, so you can run your applications on any cloud provider or on-premises infrastructure.
  3. Automation: Kubernetes automates the deployment, scaling, and management of your applications, making it easier to manage large-scale deployments.
  4. Resilience: Kubernetes is designed to be highly resilient, so your applications are always available, even in the event of machine failures.

How to set up CI/CD for your applications on Kubernetes

Here’s a high-level overview of the steps involved in setting up CI/CD for your applications on Kubernetes:

  1. Create a Git repository for your code: You’ll need to store your code in a Git repository, such as GitHub, GitLab, or Bitbucket.
  2. Set up a build system: You’ll need a build system that can compile your code and create a container image. Jenkins, TravisCI, CircleCI, and GitLab CI/CD are all popular build systems that can be used for this purpose.
  3. Push the container image to a container registry: You’ll need to push the container image to a container registry, such as Docker Hub, Google Container Registry, or Amazon Elastic Container Registry.
  4. Create a Kubernetes cluster: You’ll need a Kubernetes cluster to deploy your applications to. You can create a cluster on a cloud provider or on-premises infrastructure.
  5. Create a deployment in Kubernetes: You’ll need to create a deployment in Kubernetes that specifies how your application should be deployed, including the number of replicas, resource requirements, and update policy.
  6. Create a CI/CD pipeline: You’ll need to set up a CI/CD pipeline that automates the build, test, and deployment of your applications. This can be done using a tool such as Jenkins, TravisCI, CircleCI, or GitLab CI/CD.
  7. Deploy your application: Finally, you can deploy your application by triggering the CI/CD pipeline. The pipeline will compile your code, create a container image, push the image to the container registry, and deploy the image to the Kubernetes cluster.

Example CI/CD pipeline using Jenkins and Kubernetes

Setting up a CI/CD pipeline requires the integration of multiple tools, including a version control system, a build system, and a deployment system. In this blog, we will focus on integrating Kubernetes with the popular CI/CD tool, Jenkins.

Step 1: Create a Jenkins pipeline

The first step is to create a Jenkins pipeline that will automate our deployment process. A Jenkins pipeline is a set of steps that define the process of building, testing and deploying our application.

In Jenkins, click on “New Item” and select “Pipeline”. Give your pipeline a name, and then click on “OK”.

In the pipeline configuration, we can specify the source code repository, build triggers, and the build steps.

For this example, let’s assume that our sample application is stored in a Git repository. We can configure Jenkins to automatically build our application whenever changes are pushed to the Git repository.

Step 2: Build the application

The next step is to build our application. In the Jenkins pipeline, we can use the following code to build our application and generate a Docker image:

pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'docker build -t my-app:$BUILD_NUMBER .'
}
}
}
}

This code uses the “docker build” command to build our application and generate a Docker image. The image is tagged with the name “my-app” and the build number.

Step 3: Push the Docker image to a container registry

Once our application has been built, the next step is to push the Docker image to a container registry. A container registry is a place where Docker images are stored and distributed to other machines.

For this example, let’s assume that we are using the Docker Hub registry. We can use the following code to push our Docker image to the Docker Hub registry:

pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'docker build -t my-app:$BUILD_NUMBER .'
}
}
stage('Push to Docker Hub') {
steps {
sh 'docker login -u $DOCKER_HUB_USERNAME -p $DOCKER_HUB_PASSWORD'
sh 'docker push my-app:$BUILD_NUMBER'
}
}
}
}

This code uses the “docker login” command to log into the Docker Hub registry, and the “docker push” command to push our Docker image to the registry.

Step 4: Deploy the application to Kubernetes

Finally, we can deploy our application to Kubernetes. We can use the following code to deploy our application to a Kubernetes cluster:

apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myregistry/myapp:latest
ports:
- containerPort: 8080

This deployment specification uses the apps/v1 API version and creates a deployment named myapp-deployment. The deployment has a desired replica count of 3, which means that Kubernetes will ensure that there are 3 replicas of the application running at all times. The deployment is targeted at pods with the label app: myapp.

The template section of the specification defines the structure of the pods that will be created by the deployment. The template has the label app: myapp, which matches the selector defined in the deployment specification. The template has a single container named myapp, which is based on the myregistry/myapp:latest image. The container listens on port 8080.

After applying this deployment specification to a Kubernetes cluster, Kubernetes will create the deployment and ensure that the desired number of replicas are running. The deployment can be updated, scaled, and rolled back as needed to manage the application.

This concludes the blog on Kubernetes and CI/CD: Automating your application deployments. In this blog, we have seen how to use Jenkins and Kubernetes to automate the build, test, and deployment of an application. By following these steps, we can reduce the manual effort required to deploy applications and increase the reliability and speed of the deployment process.

Thanks for reading and I hope you enjoyed it.

--

--

Prateek Kumar

Software Engineer | NYU Alumnus | Open Source | CNCF | K8s