Continuous Integration and Continuous Delivery have become important in modern software development, ensuring faster and more reliable delivery of applications. Jenkins and Kubernetes, when combined, offer a powerful solution for automating the building, testing, and deployment of containerized applications. This article will explore building robust CI/CD pipelines using Jenkins and Kubernetes, creating a seamless and efficient software development lifecycle.
Table of contents
- What is Kubernetes?
- Advantages of running Jenkins on Kubernetes
- How to use Jenkins with Kubernetes
- Difference between Kubernetes and Docker
- Conclusion
What is Kubernetes?
Kubernetes, often called K8s, is a free and open-source platform for orchestrating containers. Its primary function is automating the deployment, scaling, and management of containerized applications. Initially developed by Google, Kubernetes was later donated to the Cloud Native Computing Foundation (CNCF). It offers a dependable and versatile framework for executing containerized workloads on a cluster of machines.
Kubernetes offers a unified API and powerful tools for declaratively deploying, managing, and scaling applications. With its emphasis on automation, Kubernetes streamlines complex operational tasks, allowing development teams to focus on building and improving applications. Its extensibility and vast ecosystem contribute to its general adoption, making it a cornerstone technology for modern containerized infrastructure.
Key features of Kubernetes
Container Orchestration
Kubernetes excels in automating the deployment and management of containerized applications. By encapsulating the applications and their dependencies into containers, Kubernetes ensures consistency across different environments, making it more effortless for developers to deploy and manage complex workloads.
Automated Scaling
One of Kubernetes’ key features is its ability to scale applications automatically based on demand. With features like Horizontal Pod Autoscaling (HPA), Kubernetes dynamically adjusts the number of running instances to handle varying workloads, optimizing resource utilization and ensuring responsiveness to changing traffic patterns.
Service Discovery and Load Balancing
Kubernetes simplifies the challenges of service discovery and load balancing in a containerized environment. It provides built-in mechanisms for seamless communication between containers within a cluster and distributes network traffic across multiple instances of a service, enhancing the availability and reliability of applications.
Self-healing
Kubernetes incorporates self-healing capabilities to enhance system resilience. If a container or node fails, Kubernetes automatically detects and mitigates the issue by rescheduling workloads to healthy nodes. This ensures continuous availability and minimizes downtime caused by unexpected failures.
Declarative Configuration
Kubernetes adopts a declarative approach to configuration, allowing users to specify the desired state of their applications and infrastructure in declarative files. This simplifies management and automation, as Kubernetes continuously works to reconcile the actual state with the declared state, ensuring consistency and reducing the risk of configuration drift.
Rolling Updates and Rollbacks
Facilitating seamless updates, Kubernetes supports rolling updates, enabling a smooth shift from one version of an application to another without service interruptions. In case of issues or errors during an update, Kubernetes can roll back to the previous version, ensuring a quick recovery and minimizing the impact on end-users.
What are the advantages of running Jenkins on Kubernetes?
Running Jenkins on Kubernetes offers numerous benefits that enhance the scalability, flexibility, and manageability of continuous integration and continuous delivery (CI/CD) workflows.
Dynamic scalability
Kubernetes enables dynamic scaling of Jenkins instances based on workload demands. With the ability to auto-scale resources, Jenkins can efficiently handle varying workloads, ensuring optimal resource utilization during periods of high demand and scaling down during periods of lower activity. This elasticity improves the overall efficiency of the CI/CD pipeline.
Resource efficiency
Leveraging Kubernetes allows Jenkins to run in lightweight containers that can be quickly instantiated or terminated. This approach optimizes resource utilization, as containers share the underlying host OS kernel, reducing overhead and enabling more efficient use of computing resources. Jenkins workloads can be isolated within containers, preventing interference and enhancing overall system stability.
Declarative configuration
Kubernetes allows for the declarative configuration of Jenkins instances through YAML files, enabling infrastructure as code practices. This simplifies the setup and maintenance of Jenkins environments, as configuration changes can be version-controlled and applied consistently across different clusters or environments. The declarative approach ensures consistency and reduces the risk of configuration drift.
High availability and reliability
Kubernetes’ architecture promotes high availability by distributing Jenkins master and agent instances across multiple nodes in the cluster. In the failure events, Kubernetes automatically reschedules Jenkins workloads to healthy nodes, ensuring uninterrupted CI/CD processes. This resilience contributes to the overall reliability of Jenkins-based workflows.
Easy updates and rollbacks
Kubernetes facilitates seamless updates and rollbacks of Jenkins instances. With features like rolling deployments, new Jenkins versions can be gradually introduced without downtime. In case of issues, rollbacks can be performed swiftly, minimizing the impact on development pipelines. This flexibility enhances the agility of Jenkins-based CI/CD workflows.
Uniform environment
Kubernetes provides a consistent environment for running Jenkins, regardless of the underlying infrastructure. This consistency simplifies the development and testing processes, as Jenkins jobs can be executed uniformly across various clusters, cloud providers, or on-premises data centers. This uniformity reduces potential compatibility issues and streamlines the overall workflow.
How to use Jenkins with Kubernetes
Prerequisites
Make sure to check the prerequisites before starting the deployment process:
Note
While it is possible to integrate Jenkins with Kubernetes without Docker, using Docker as the containerization technology offers several advantages in terms of consistency, isolation, dependency management, and deployment automation, making the overall integration more seamless and efficient.
- Docker is installed and properly configured on your machine.
- Jenkins is installed and running on your system.
Set up the Kubernetes Cluster
The very first step to creating a Kubernetes cluster is setting it up. The steps you must follow may differ slightly depending on your chosen environment. For this example, we will create a local Kubernetes cluster using Minikube.
Note
Minikube is a software tool that can be used to set up a Kubernetes environment on a personal computer or laptop. It is considered a Kubernetes distribution. However, it is more commonly referred to as a tool because it caters to a specific use case different from other Kubernetes distributions such as Rancher, OpenShift, and EKS.
To install the latest minikube release on x86-64 Windows, click the following link to download the installer file. Then, complete your installation process with the default choices.
Alternatively, you may use the following PowerShell command:
New-Item -Path 'c:\' -Name 'minikube' -ItemType Directory -Force
Invoke-WebRequest -OutFile 'c:\minikube\minikube.exe' -Uri 'https://github.com/kubernetes/minikube/releases/latest/download/minikube-windows-amd64.exe' -UseBasicParsing
Add the minikube.exe
binary to your respective PATH. To ensure proper functioning, please run PowerShell as an admin by selecting the Run as administrator option.
$oldPath = [Environment]::GetEnvironmentVariable('Path', [EnvironmentVariableTarget]::Machine)
if ($oldPath.Split(';') -inotcontains 'C:\minikube'){
[Environment]::SetEnvironmentVariable('Path', $('{0};C:\minikube' -f $oldPath), [EnvironmentVariableTarget]::Machine)
}
Note
If you used a Powershell terminal for the installation, please close it and reopen it before running Minikube.
To start your cluster from a terminal, run the following command. And it may take a few minutes to complete.
minikube start
Minikube can be used to download the appropriate version of Kubectl, and you will be able to utilize it by following the command:
minikube kubectl -- get po -A
Note
Kubectl, the Kubernetes command-line tool, enables you to execute commands on Kubernetes clusters. You can deploy applications, manage cluster resources, and inspect and view logs using Kubectl.
Minikube includes the Kubernetes dashboard to provide additional insight into the cluster state, which allows you to familiarize yourself with a new environment quickly.
minikube dashboard
Please create a sample deployment and ensure it is accessible on port 8080:
kubectl create deployment hello-minikube --image=kicbase/echo-server:1.0
kubectl expose deployment hello-minikube --type=NodePort --port=8080
You can also use the kubectl command to forward the port:
kubectl port-forward service/hello-minikube 7080:8080
Congratulations! Your application is now accessible at http://localhost:7080/
.
Finally, you can verify that your cluster is up and running by the following command:
kubectl cluster-info
Install Kubernetes Jenkins Plugin
Open the Jenkins dashboard in the respective web browser.
Click on Manage Jenkins and navigate to the Plugins tab.
Then, go to the Available tab and search for the Kubernetes plugin.
Select the plugin by checking the box and then click the Install button. Wait for the installation process to finish.
Alternatively, you can download the plugin directly from here.
How to configure Jenkins for Kubernetes integration
Start by navigating to the Jenkins dashboard. Once you’re there, look for the Manage Jenkins option in the left-hand menu.
Scroll down to locate the Cloud section within system configuration tab.
Then, you’ll find an option as New cloud. Click on this to initiate the Kubernetes integration process.
Choose Kubernetes and provide a name for the Kubernetes cloud configuration. This selection and this name will help you quickly identify and manage the integration settings within Jenkins. And lastly, click the Create button.
Set the Kubernetes URL for your Kubernetes cluster. Then, enable the Use Jenkins Proxy option.
Select the Kubernetes Namespace where you want your Jenkins agents deployed. This ensures a clean and organized environment for running Jenkins workloads.
Then, enable the Web socket option. If this is not done, connecting Kubernetes agents will not be possible without using WebSocket mode.
Customize the remaining options based on your specific requirements. This includes setting parameters such as the Connection Timeout, Concurrency Limit, and other configurations relevant to your workflow.
Once you’ve verified the connection, save the configuration settings. This action commits your changes and enables Jenkins to integrate seamlessly with your Kubernetes cluster.
How to create a Jenkins pipeline for Kubernetes
Head to the Jenkins dashboard and initiate the creation of a new Jenkins pipeline job by clicking on New Item.
Provide a meaningful name for your job and select Pipeline as the job type. This establishes the foundation for your continuous deployment process.
In the job configuration page, navigate to the Pipeline section. Opt for Pipeline script from the Definition dropdown.
In the pipeline script block, integrate the following code snippet tailored for deploying a Node.js application:
pipeline {
agent {
kubernetes {
label 'my-kubernetes-agent'
}
}
stages {
stage('Clone repository') {
steps {
git 'https://github.com/your-repo.git'
}
}
stage('Deploy to Kubernetes') {
steps {
kubernetesDeploy(
configs: 'kubernetes/deployment.yaml',
kubeconfigId: 'my-kubeconfig'
)
}
}
}
}
Note
Replace the https://github.com/your-repo.git
with the URL of your respective Git repository and the Kubernetes/deployment.yaml
with the path to your Kubernetes deployment configuration file.
Scroll to the bottom of the page and click the Save button to create the Jenkins pipeline job successfully.
Run and deploy Jenkins Pipeline
After that, click on the Build Now button to start the pipeline execution.
Ensure successful completion of all pipeline stages in Jenkins Console output. Verify application deployment and running on Kubernetes cluster with kubectl.
How to implement CI/CD using Jenkins and Kubernetes
To establish a robust CI/CD workflow with Jenkins and Kubernetes, begin by integrating your Jenkins pipeline with your version control system, such as Git. Configuring automated triggers enables seamless pipeline execution, automating key stages like building, testing, and deploying applications each time new code changes are pushed or commits are made to the repository. This integration fosters a continuous feedback loop, promoting early detection of issues and ensuring that your applications adhere to quality standards throughout development.
Utilizing Kubernetes for orchestration, your CI/CD pipeline gains the flexibility to deploy containerized applications consistently across various environments, enhancing scalability and minimizing deployment complexities. Embrace the power of this integrated CI/CD approach to accelerate development cycles, reduce manual interventions, and deliver high-quality software efficiently and confidently.
What is the difference between Kubernetes and Docker?
Features | Kubernetes | Docker |
---|---|---|
Definition | An open-source container orchestration platform designed to automate containerized applications’ deployment, scaling, and management across a cluster of machines. | A platform for developing, shipping, and running applications in containers. It provides a standardized unit (container) for packaging and distributing applications. |
Scope | Manages container orchestration at scale, handling containerized applications’ deployment, scaling, and management across multiple nodes. | Primarily focuses on packaging applications into containers, providing a lightweight, portable, and consistent runtime environment. |
Clustering and Scaling | Offers robust clustering support, allowing the distribution of containerized workloads across multiple nodes for better resource utilization and high availability. | Limited native support for clustering. Docker Swarm can be used for clustering, but it is less feature-rich than Kubernetes. |
Networking | Provides a rich set of networking features, allowing containers to communicate with each other within the same pod and across different pods. | Provides networking capabilities, allowing containers to communicate with each other using bridge networks, overlay networks, and user-defined networks. |
Configuration Management | Uses declarative YAML files to define and manage the desired state of applications, making it easy to do version control and reproduce configurations. | Configuration is often managed through Dockerfiles, which define the steps to build an image. However, Docker Compose also allows the definition of multi-container applications using a YAML file. |
Orchestration | Provides advanced orchestration features, including automated scaling, load balancing, rolling updates, and self-healing of containerized applications. | Limited orchestration capabilities. Docker Compose helps define and run multi-container Docker applications, but it’s more suitable for development environments. |
Final thoughts on building CI/CD pipelines with Jenkins and Kubernetes
In conclusion, building CI/CD pipelines with Jenkins and Kubernetes represents a crucial step toward achieving streamlined, automated, and scalable software development workflows. The powerful combination of Jenkins for continuous integration and Kubernetes for container orchestration provides a robust foundation for efficiently deploying, scaling, and managing applications.
Following the integration steps mentioned in this article, development teams can harness the benefits of a seamless CI/CD pipeline, ensuring rapid and reliable delivery of high-quality software. Adopting these technologies enhances the speed of software development and promotes a culture of automation, collaboration, and continuous improvement within modern DevOps practices.
Related articles
- How to build CI/CD pipelines with Jenkins and Docker
- How to create a Jenkins pipeline
- Declarative vs scripted Jenkins pipelines
- Using Jenkins in test automation
- The Jenkins architecture
Follow our blog
Be the first to know when we publish new content.
How to use Jenkins with Kubernetes
- Top 10 API Testing Tools - April 6, 2024
- The ABCs of UAT Testing: Understanding User Acceptance Testing - March 21, 2024
- Agile Testing: Key Principles and Practices - March 15, 2024