[

ūüéČ

G2 Spring 2024 Report] Hatica does it again! Wins Momentum Leader, Best Est. ROI and Users Love Us badge among many!Read More ->

Tools2023-07-04

How to Build a CI/CD Pipeline with Jenkins and Kubernetes

Learn how to deploy a Kubernetes cluster with Jenkins for streamlined software development with step-by-step instructions, code examples, and documentation.
CI/CD With Kubernetes and Jenkins

Kubernetes is a popular container orchestration platform allowing you to efficiently manage and scale containerized applications. On the other hand, Jenkins is a widely-used automation tool that helps with continuous integration and continuous deployment (CI/CD) workflows.

By combining the power of Kubernetes and Jenkins, you can streamline your application deployment process and ensure smooth operations. We will cover the steps involved in setting up a Kubernetes cluster and integrating it with Jenkins, along with code examples and proper documentation. In this article, we will explore the process of deploying a Kubernetes cluster using Jenkins.

Prerequisites

Before diving into the deployment process, make sure you have the following prerequisites:

  1. Your computer with Docker installed
  2. A Kubernetes  cluster environment (e.g., Minikube, Docker Desktop with Kubernetes enabled, or a cloud-based Kubernetes service like Amazon EKS, Google Kubernetes Engine, etc.)
  3. Jenkins installed and running (You can follow the official Jenkins documentation for installation instructions)

[Read More: GitHub Jenkins Webhook Integration]

Deploying a Kubernetes cluster Using Jenkins

Step 1: Setting Up the Kubernetes Cluster

The first step is to set up your Kubernetes cluster. Depending on your chosen environment, the steps may vary slightly. In this example, we will use Minikube to create a local Kubernetes cluster. Follow the steps below:

  • Install Minikube by following the instructions provided in the official Minikube documentation.
  • Start the Minikube cluster by running the command: minikube start.
  • Verify that your cluster is up and running: kubectl cluster-info.

You have now successfully set up your Kubernetes cluster. Let's move on to the next step of integrating Jenkins with Kubernetes.

Step 2: Installing Jenkins Plugins

To enable Jenkins to interact with your Kubernetes cluster, you need to install the necessary plugins. Follow the steps below:

  • Open your Jenkins dashboard in a web browser.
  • Click on "Manage Jenkins" in the sidebar.
  • Select "Manage Plugins" from the dropdown menu.

Then navigate to the "Available" tab and search for the following plugins:

  • Kubernetes Plugin
  • Docker Pipeline Plugin
  • Kubernetes Continuous Deploy Plugin

Select the checkboxes next to the mentioned plugins and click on the "Install without restart" button. Then wait for the plugins to be installed. Alternatively, you can directly download the plugin here. 

Step 3: Configuring Jenkins for Kubernetes Integration

Now that we have the required plugins installed, let's configure Jenkins to interact with our Kubernetes cluster. Follow the steps below:

  • Go back to the Jenkins dashboard and click on "Manage Jenkins."
  • Select "Configure System" from the dropdown menu.
  • Scroll down to the "Cloud" section and click on "Add a new cloud."
  • Choose "Kubernetes" from the "Add a new cloud" dropdown.
  • Provide a name for the Kubernetes cloud configuration.
  • Set the "Kubernetes URL" to the URL of your Kubernetes cluster (e.g., http://localhost:8001 for Minikube).
  • Select the "Kubernetes Namespace" where your Jenkins agents will be deployed.
  • Configure the remaining options as per your requirements, such as the maximum number of Jenkins agents, usage restrictions, etc.
  • Click on the "Test Connection" button to ensure Jenkins can communicate with your Kubernetes cluster successfully.
  • Save the configuration.

Step 4: Creating a Jenkins Pipeline for Kubernetes Deployment

Now that we have Jenkins integrated with Kubernetes, let's create a Jenkins pipeline to deploy our application. In this example, we will deploy a sample Node.js application.

Follow the steps below:

  • Create a new Jenkins pipeline job by clicking on "New Item" on the Jenkins dashboard.
  • Provide a name for your job and select "Pipeline" as the job type.
  • Scroll down to the "Pipeline" section and select "Pipeline script" from the "Definition" dropdown.

In the pipeline script block, add the following code snippet:

pipeline {

  agent {

    kubernetes {

      label 'my-kubernetes-agent'

    }

  }

  

  stages {

    stage('Clone repository') {

      steps {

        git 'https://github.com/your-repo.git'

      }

    }

    

    stage('Build and push Docker image') {

      steps {

        sh 'docker build -t my-image:latest .'

        sh 'docker push my-image:latest'

      }

    }

    

    stage('Deploy to Kubernetes') {

      steps {

        kubernetesDeploy(

          configs: 'kubernetes/deployment.yaml',

          kubeconfigId: 'my-kubeconfig'

        )

      }

    }

  }

}

Ensure you replace 'https://github.com/your-repo.git' in the script with the URL of your actual Git repository and 'kubernetes/deployment.yaml' with the path to your Kubernetes deployment configuration file.

  • Scroll down and click on the "Save" button to create the Jenkins pipeline job.

Step 5: Run the Jenkins Pipeline and Deploy to Kubernetes

With the pipeline job created, let's run the pipeline and deploy our application to Kubernetes. Follow the steps below:

  • Open the Jenkins dashboard and navigate to the pipeline job you created.
  • Click on the "Build Now" button to start the pipeline execution.

Jenkins will clone your Git repository, build a Docker image, push it to a registry, and deploy the application to your Kubernetes cluster.

Monitor the pipeline execution in the Jenkins console output to ensure all stages are completed successfully. Once the pipeline finishes, verify that your application is deployed and running in the Kubernetes cluster using kubectl.

Step 6: Implementing Continuous Integration and Continuous Deployment (CI/CD)

To implement a robust CI/CD workflow with Jenkins and Kubernetes, you can integrate your Jenkins pipeline with your version control system (e.g., Git) and set up automated triggers for pipeline execution. This allows you to automatically build, test, and deploy your applications whenever new code changes are pushed or commits are made to the repository.

Step 7: Handling Secrets and Configurations

When deploying applications to Kubernetes, it's important to handle sensitive information and configurations securely. Jenkins provides several mechanisms to manage secrets and configurations:

  • Kubernetes Secrets: You can use Kubernetes Secrets to store sensitive information like passwords, API keys, or database credentials. These secrets can be mounted as environment variables or files in your application's containers, ensuring secure access to sensitive data during deployment.
  • Jenkins Credentials Plugin: Jenkins provides a credentials plugin that allows you to securely store and manage sensitive information. You can use this plugin to store credentials like usernames, passwords, SSH keys, or API tokens. These credentials can be accessed within your pipeline script and used during deployment securely.

By utilizing these mechanisms, you can ensure that sensitive information is not exposed in your pipeline scripts or stored in plain text, improving the security of your deployment process.

Step 8: Scaling and High Availability

As your applications grow, it's important to scale your Kubernetes cluster and ensure high availability to handle increased traffic and maintain a resilient infrastructure. Here are some considerations:

  • Horizontal Pod Autoscaling: You can configure Kubernetes to automatically scale the number of replicas for your application pods based on CPU or memory utilization. This ensures that your applications can handle varying levels of traffic and resource demands.
  • Cluster Scaling: Depending on your Kubernetes environment, you may need to scale your cluster itself. This can involve adding or removing worker nodes to meet the resource requirements of your applications.
  • Load Balancing: Implementing a load balancer, such as Kubernetes Ingress or an external load balancer, helps distribute incoming traffic across multiple application instances, ensuring scalability and high availability.

By implementing these strategies, you can handle increased traffic, optimize resource utilization, and maintain a resilient infrastructure.

Step 9: Monitoring and Logging

Monitoring and logging are crucial for gaining insights into the health and performance of your Kubernetes cluster and applications. Here are some suggestions:

Metrics Collection and Visualization: Tools like Prometheus and Grafana can be used to collect, store, and visualize metrics from your Kubernetes cluster and applications. They provide valuable insights into resource utilization, application performance, and scalability.

Logging and Analysis: Implementing the ELK stack (Elasticsearch, Logstash, Kibana) or other log aggregation solutions enables centralized logging and analysis of your application logs. This helps with troubleshooting, debugging, and detecting anomalies within your cluster and applications.

By monitoring and logging your Kubernetes cluster and applications, you can proactively identify issues, optimize performance, and ensure smooth operations.

Step 10: Advanced Deployment Strategies

In addition to basic deployment, Kubernetes offers advanced deployment strategies that ensure zero downtime, smooth rollbacks, and canary testing. Here are a few examples:

  • Canary Deployments: With canary deployments, you roll out a new version of your application to a small subset of users or a specific environment to test its performance and stability before rolling it out to the entire user base.
  • Blue/Green Deployments: Blue/green deployments involve running two identical environments (blue and green) simultaneously. The new version is deployed to the green environment, and once it passes testing and validation, traffic is routed from the blue environment to the green, resulting in zero downtime.
  • Rolling Updates: Kubernetes supports rolling updates, where the new version of an application is incrementally deployed across the replicas, minimizing disruption to user traffic. This allows for smooth updates and easy rollbacks if issues are detected.

Congratulations! You have successfully deployed your application to Kubernetes using Jenkins. By leveraging these advanced deployment strategies, you can ensure seamless updates, minimize user impact, and easily revert to a stable version if necessary.

Looking to reduce build time in Jenkins? Check out our comprehensive guide on How to Optimize and Speed up Jenkins Builds to significantly improve your development workflow!

Conclusion

We have explored the process of deploying a Kubernetes cluster with Jenkins. We also covered the steps to set up a Kubernetes cluster, install the necessary Jenkins plugins, and configure Jenkins to interact with Kubernetes. We also created a Jenkins pipeline to automate the deployment process and deployed a sample application to the Kubernetes cluster. By integrating Jenkins and Kubernetes, you can streamline your application deployment workflow and ensure efficient management of containerized applications.

You can further enhance your deployment pipeline with testing, monitoring, and rollback strategies to ensure a robust and reliable deployment process.

Happy deploying with Jenkins and Kubernetes!

Share this article:
Table of Contents
  • Prerequisites
  • Deploying a Kubernetes cluster Using Jenkins
  • Step 1: Setting Up the Kubernetes Cluster
  • Step 2: Installing Jenkins Plugins
  • Step 3: Configuring Jenkins for Kubernetes Integration
  • Step 4: Creating a Jenkins Pipeline for Kubernetes Deployment
  • Step 5: Run the Jenkins Pipeline and Deploy to Kubernetes
  • Step 6: Implementing Continuous Integration and Continuous Deployment (CI/CD)
  • Step 7: Handling Secrets and Configurations
  • Step 8: Scaling and High Availability
  • Step 9: Monitoring and Logging
  • Step 10: Advanced Deployment Strategies
  • Conclusion

Ready to dive in? Start your free trial today

Overview dashboard from Hatica