Setting up Jenkins on a Kubernetes cluster is an efficient way to automate your CI/CD pipelines and streamline your software development lifecycle.
In this guide, we will walk you through the process of installing Jenkins on Kubernetes, why you should do it, and the steps you need to follow to make it happen.
What Is Kubernetes?
Kubernetes is an orchestration engine and open-source platform for managing containerized applications. which is responsible for container deployment, scaling & descaling of containers v& container load balancing. Kubernetes is not a replacement for Docker, But Kubernetes can be considered as a replacement for Docker Swarm, Kubernetes is significantly more.
What is Jenkins?
Jenkins is an open-source automation tool for continuous integration (CI) and continuous delivery (CD). It allows you to automate all the repetitive tasks in software development, such as building, testing, and deploying code. Integrating Jenkins with Kubernetes offers significant advantages in terms of scalability, performance, and automation.
In this guide, we'll show you how to deploy Jenkins on a Kubernetes cluster using YAML configurations and Kubernetes services.
Benefits of Running Jenkins on Kubernetes
Running Jenkins on a Kubernetes cluster offers several advantages:
- Scalability: Kubernetes allows Jenkins to scale dynamically, handling a large number of build jobs without impacting performance.
- Resource Efficiency: Kubernetes optimizes resource allocation, ensuring efficient use of CPU and memory resources.
- Containerization: Jenkins itself runs inside a container, enabling rapid deployment and simplified management.
- High Availability: Kubernetes can ensure high availability by distributing Jenkins instances across multiple nodes.
Prerequisites
Before following this tutorial, ensure you have the following:
- A working Kubernetes cluster and kubectl set up on your workstation. If you haven't set up Kubernetes yet, you can follow our Kubernetes Quickstart.
Setup Jenkins On K8's Cluster: Step by Step Guide
Follow the below steps to setup Jenkins on Kubernetes cluster:
Step 1: Install Jenkins on Kubernetes
Kubernetes utilizes a declarative API where the desired state is conveyed through YAML or JSON files. In this tutorial, we will deploy Jenkins using a YAML file. Ensure that your kubectl command is properly configured for your Kubernetes cluster.
1.1: Create the Jenkins Namespace
First, create a dedicated namespace for Jenkins:
kubectl create namespace jenkins
1.2: Create the Jenkins YAML Deployment File
Now, create a YAML file to define the Jenkins deployment. Open your preferred editor (e.g., nano) and create a new file named jenkins.yaml:
nano jenkins.yaml
In this file, add the following code to define the Jenkins deployment, its port, and other configurations:
apiVersion: apps/v1
kind: Deployment
metadata:
name: jenkins
spec:
replicas: 1
selector:
matchLabels:
app: jenkins
template:
metadata:
labels:
app: jenkins
spec:
containers:
- name: jenkins
image: jenkins/jenkins:lts
ports:
- name: http-port
containerPort: 8080
- name: jnlp-port
containerPort: 50000
volumeMounts:
- name: jenkins-vol
mountPath: /var/jenkins_vol
volumes:
- name: jenkins-vol
emptyDir: {}
This YAML file configures a Jenkins LTS image and exposes port 8080 (for the Jenkins UI) and port 50000 (for Jenkins worker connections). The emptyDir volume is mounted to persist data.
1.3: Apply the Deployment YAML File
To create the Jenkins deployment in the Kubernetes cluster, use the following command:
kubectl create -f jenkins.yaml --namespace jenkins
Give Kubernetes a few minutes to pull the Jenkins image and launch the Jenkins pod.
1.4: Verify the Pod's Status
Check the status of the Jenkins pod to ensure it's running:
kubectl get pods -n jenkins
You will get the output as shown below:
NAME READY STATUS RESTARTS AGE
jenkins-6fb994cfc5-twnvn 1/1 Running 0 95s
The pod name may differ in your environment.
1.5: Expose Jenkins Using Services
Once the Jenkins pod is running, expose it via a Service. You’ll use a NodePort service for external access and a ClusterIP service for Jenkins workers to connect to Jenkins.
Create and open a new file called jenkins-service.yaml:
nano jenkins-service.yaml
Add the following code to define the two services: a NodePort service for accessing Jenkins from outside the cluster, and a ClusterIP service for worker connections:
apiVersion: v1
kind: Service
metadata:
name: jenkins
spec:
type: NodePort
ports:
- port: 8080
targetPort: 8080
nodePort: 30000
selector:
app: jenkins
---
apiVersion: v1
kind: Service
metadata:
name: jenkins-jnlp
spec:
type: ClusterIP
ports:
- port: 50000
targetPort: 50000
selector:
app: jenkins
1.6: Apply the Service YAML File
To create the services in the Kubernetes cluster, run:
kubectl create -f jenkins-service.yaml --namespace jenkins
1.7: Verify the Service
Check the status of the services to ensure they are running correctly:
kubectl get services --namespace jenkins
You will see the below output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
jenkins NodePort your_cluster_ip <none> 8080:30000/TCP 15d
Now that your NodePort and Jenkins services are running, you're ready to access Jenkins via the external IP.
Step 2: Access the Jenkins UI
2.1: Retrieve Node IP
To access Jenkins, you need the external IP of your Kubernetes node. Run the following command to list the nodes and their external IP addresses:
kubectl get nodes -o wide
You will see the output as shown below:
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
your_node Ready <none> 16d v1.18.8 your_internal_ip your_external_ip Debian GNU/Linux 10 (buster) 4.19.0-10-cloud-amd64 docker://18.9.9
Copy one of the External - IP values.
2.2: Access Jenkins in Your Browser
Open a web browser and navigate to the following URL:
http://your_external_ip:30000
Now Enter the administrator password to unlock Jenkins.
2.3: Retrieve the Administrator Password
To find the password, check the logs of your Jenkins pod. First, get the pod name:
kubectl get pods -n jenkins
Then, retrieve the logs using the following command, replacing jenkins-6fb994cfc5-twnvn with your actual pod name:
kubectl logs jenkins-6fb994cfc5-twnvn -n jenkins
You will see the output as shown below:
Running from: /usr/share/jenkins/jenkins.war
webroot: EnvVars.masterEnvVars.get("JENKINS_HOME")
...
Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:
your_jenkins_password
Copy your Jenkins Password.
2.4: Enter the Password in Jenkins UI
Return to the Jenkins login page and paste the password. Jenkins will then ask you to install suggested plugins. Select Install suggested plugins and proceed.
2.5: Set Up Jenkins Admin User
After plugin installation, Jenkins will prompt you to create an admin user. You can either fill in the details or click Skip and continue as admin to use the default admin username and password.
2.6: Final Setup
Once the setup is complete, Jenkins will show a summary of your configuration. Click Start using Jenkins to access the Jenkins dashboard.
Step 3: Running a Sample Pipeline
Now that Jenkins is up and running, let’s create a sample pipeline.
3.1: Create a New Pipeline
From the Jenkins homepage, click New Item in the left-hand menu. Then:
Choose Pipeline >> Click OK
On the pipeline configuration page:
- Under the Pipeline section, choose Hello World from the Try sample pipeline dropdown.
- Click Save.
3.3: Run the Pipeline
After saving the pipeline, Jenkins will redirect you to the pipeline’s home page. From there:
- Click Build Now to start the pipeline.
- Once the task completes, you’ll see build statistics.
3.4: Check Console Output
To see the details of the pipeline execution, click #1 under Build History and select Console Output to view the log.
Conclusion
Deploying Jenkins on Kubernetes offers a highly scalable and cost-effective CI/CD pipeline solution for modern DevOps teams. By learning Kubernetes orchestration, containerized Jenkins deployment, and infrastructure as code, you can automate builds and releases efficiently. This setup not only improves software delivery speed but also supports seamless continuous integration and delivery across cloud-native environments. Whether you're working with AWS EKS, Google Kubernetes Engine (GKE), or Azure Kubernetes Service (AKS), this approach ensures high availability, faster deployments, and better resource optimization.
Similar Reads
How to Scale a Kubernetes Cluster ? Scaling a Kubernetes cluster is crucial to meet the developing needs of applications and ensure the finest performance. As your workload increases, scaling becomes vital to distribute the weight, enhance useful resource usage, and maintain high availability. This article will guide you through the t
8 min read
How to Careate Static Pod in Kubernetes ? Static pods in Kubernetes make it simple to run pods directly on individual cluster nodes without the Kubernetes API server being involved. Not at all like ordinary pods oversaw by the Kubernetes control plane, static pods are characterized and overseen straight by the Kubelet, the essential node ag
7 min read
How To Use Kind To Deploy Kubernetes Clusters? Kind also referred to as Kubernetes in Docker is a popular open-source tool used for running a Kubernetes cluster locally. Kind uses Docker containers as Cluster Nodes making it substantially faster than its alternatives like Minikube or Docker Desktop which uses a Virtual Machine. Kind is commonly
10 min read
How to Upgrade a Kubernetes Cluster? Kubernetes, the open-source field orchestration platform, is continuously evolving to satisfy the demands of modern applications. Regularly upgrading your Kubernetes cluster is essential to leverage new capabilities, safety patches, and overall performance enhancements. In this article, we can dive
10 min read
How to Add Node to Existing Kubernetes Cluster Kubernetes allows the management and orchestration of containerized deployments. We can use various cluster managing tools, but Kubeadm allows us to create and manage Kubernetes clusters from scratch. Kubernetes cluster can be autoscaled with the use of Cluster management services. In this article,
4 min read