Building a Complete CI/CD Pipeline with Jenkins and Kubernetes
In the fast-paced world of software development, Continuous Integration/Continuous Deployment (CI/CD) has become an essential practice to streamline workflows and accelerate delivery cycles. When paired with powerful tools like Jenkins and Kubernetes, creating a robust CI/CD pipeline becomes not just feasible but efficient. In this article, we’ll explore how to set up a complete CI/CD pipeline using Jenkins and Kubernetes, step by step.
What is CI/CD?
Continuous Integration (CI) refers to the practice of automatically integrating code changes from multiple contributors into a single software project. Continuous Deployment (CD), on the other hand, is the practice of automatically deploying all code changes to a production environment after the build stage. Together, they form a pivotal workflow that enhances collaboration and reduces the time it takes to deliver software.
The Role of Jenkins in CI/CD
Jenkins is an open-source automation server that facilitates the CI/CD pipeline. With its wide array of plugins and integrations, Jenkins allows you to automate the testing and deployment of applications, thus speeding up development cycles. Let’s cover the steps to set up Jenkins to work with our Kubernetes-based infrastructure.
Setting Up Jenkins
To get started, we need to have Jenkins up and running. You can choose to install Jenkins locally or deploy it within a Kubernetes cluster. For our example, we will deploy Jenkins on Kubernetes.
Step 1: Deploy Jenkins on Kubernetes
To deploy Jenkins on Kubernetes, we will leverage Helm, a package manager for Kubernetes.
#!/bin/bash
# Add the Jenkins Helm repository
helm repo add jenkins https://charts.jenkins.io
# Update Helm repositories
helm repo update
# Create a namespace for Jenkins
kubectl create namespace jenkins
# Install Jenkins using Helm
helm install jenkins jenkins/jenkins --namespace jenkins --set serviceType=NodePort
This command will install Jenkins in a namespace called `jenkins`. You can access Jenkins through the NodePort exposed by Kubernetes.
Step 2: Accessing the Jenkins UI
After running the command, you can get the Jenkins UI URL and the initial admin password.
# Get Jenkins service
kubectl get services --namespace jenkins
# Get the administrator password
kubectl get secret --namespace jenkins jenkins -o jsonpath="{.data.jenkins-admin-password}" | base64 --decode; echo
Visit the URL you retrieved under SERVICES, enter the admin username (`admin`) and the password you just decoded, and you will be greeted by Jenkins’ setup wizard.
Configuring Jenkins for Our CI/CD Pipeline
Jenkins supports pipelines as code using the Jenkinsfile. This allows you to define your CI/CD process within your repository. Let’s create a Jenkinsfile for a sample application.
Step 3: Create a Sample Application
For our example, let’s consider a basic Node.js application.
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3000;
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Next, we will set up our Jenkinsfile for CI/CD.
Step 4: Creating the Jenkinsfile
Create a file named `Jenkinsfile` in the root of your Node.js application.
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
docker.build("my-node-app:${env.BUILD_ID}")
}
}
}
stage('Test') {
steps {
sh 'npm install'
sh 'npm test'
}
}
stage('Deploy') {
steps {
sh 'kubectl apply -f k8s/deployment.yaml'
}
}
}
}
Creating Kubernetes Deployment and Service
To deploy the application on Kubernetes, you will need a `deployment.yaml` file. Create a directory named `k8s` in your project root, and add the following:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-node-app
spec:
replicas: 2
selector:
matchLabels:
app: my-node-app
template:
metadata:
labels:
app: my-node-app
spec:
containers:
- name: my-node-app
image: my-node-app:${BUILD_ID}
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: my-node-app-service
spec:
type: NodePort
ports:
- port: 3000
selector:
app: my-node-app
Integrating Jenkins with Kubernetes
To enable Jenkins to communicate with Kubernetes, you need to set up a Kubernetes plugin in Jenkins.
Step 5: Install Kubernetes Plugin
1. Navigate to “Manage Jenkins” > “Manage Plugins”.
2. Search for “Kubernetes” in the Available tab and install it.
3. Configure the Kubernetes credentials by going to “Manage Jenkins” > “Configure System” and adding your Kubernetes API URL and credentials.
Triggering the Pipeline
To automate our CI/CD pipeline, we can set up triggers. Go to your Jenkins dashboard and configure the pipeline in your job settings to build based on SCM changes (e.g., GitHub webhooks).
Step 6: Set Up Webhooks in GitHub
1. Go to your GitHub repository, and click on “Settings”.
2. Under “Webhooks”, click “Add webhook”.
3. Enter your Jenkins server’s address followed by “/github-webhook/”.
4. Select “Just the push event,” and click “Add webhook”.
Conclusion
Congratulations! You have successfully set up a complete CI/CD pipeline using Jenkins and Kubernetes. In this, we utilized Jenkins for CI/CD automation and Kubernetes for deploying containerized applications with ease.
This setup allows development teams to work more efficiently, minimize the chances of errors, and focus more on building features rather than managing deployment complexities. As CI/CD becomes more integral to software development, tools like Jenkins and Kubernetes will remain pivotal.
Feel free to explore more about Jenkins’ extensive plugins, advanced configurations, and Kubernetes’ orchestration features to enhance your CI/CD pipelines further.
Additional Resources
– Jenkins Documentation
– Kubernetes Documentation
– Docker Overview
– GitHub Documentation
Happy coding!
