Jenkins on Kubernetes: Complete Setup and Configuration
Putting Jenkins on Kubernetes may be a game-changer for any development team looking to maximize their pipeline for continuous integration and deliver
Table of contents
Putting Jenkins on Kubernetes may be a game-changer for any development team looking to maximize their pipeline for continuous integration and delivery. Jenkins can manage several builds and tests in parallel by utilizing Kubernetes' scalability and flexibility, which lowers bottlenecks and speeds up deployment times. You will be guided through every stage of the setup process by this all-inclusive guide, which will guarantee that your Jenkins setup on Kubernetes is reliable and effective.
Introduction
Jenkins, an open-source automation server, enables developers to build, test, and deploy their software. Kubernetes, an open-source container orchestration platform, allows for the automated deployment, scaling, and management of containerized applications. Together, they create a powerful combination for continuous integration and continuous delivery (CI/CD).
Prerequisites
Before diving into the setup, ensure you have the following prerequisites:
A Kubernetes cluster (local or cloud-based).
kubectl
command-line tool configured to communicate with your cluster.Helm package manager installed on your local machine.
Basic knowledge of Kubernetes concepts such as Pods, Services, Deployments, and Persistent Volumes.
Step 1: Setting Up a Kubernetes Cluster
If you do not have a Kubernetes cluster, you can set one up using Minikube for local development or a managed Kubernetes service like Google Kubernetes Engine (GKE), Amazon Elastic Kubernetes Service (EKS), or Azure Kubernetes Service (AKS) for production environments.
Using Minikube
To install Minikube, follow these steps:
- Download and install Minikube from the official Minikube installation guide.
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
chmod +x minikube
sudo mv minikube /usr/local/bin/
- Start Minikube:
minikube start
Using GKE, EKS, or AKS
For cloud-based clusters, follow the respective provider's documentation:
Step 2: Installing Helm
Helm is a package manager for Kubernetes that simplifies the deployment of applications. Install Helm by following these steps:
- Download and install Helm from the official Helm installation guide.
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
- Verify the installation:
helm version
Step 3: Deploying Jenkins Using Helm
Helm makes it easy to deploy Jenkins on Kubernetes. We will use the Jenkins Helm chart to perform the deployment.
- Add the Jenkins Helm repository:
helm repo add jenkins https://charts.jenkins.io
helm repo update
- Create a namespace for Jenkins:
kubectl create namespace jenkins
- Install Jenkins using the Helm chart:
helm install jenkins jenkins/jenkins --namespace jenkins
- Verify the deployment:
kubectl get pods -n jenkins
You should see the Jenkins Pod running.
Step 4: Accessing Jenkins
By default, the Jenkins service is exposed as a ClusterIP service, which is only accessible within the cluster. To access Jenkins from your local machine, you can use kubectl port-forward
:
kubectl port-forward svc/jenkins 8080:8080 -n jenkins
Now, open your browser and navigate to http://localhost:8080
. You will be prompted to enter the Jenkins admin password, which you can retrieve using the following command:
kubectl exec --namespace jenkins -it svc/jenkins -c jenkins -- /bin/cat /run/secrets/chart-admin-password && echo
Step 5: Configuring Jenkins
Installing Plugins
Jenkins has a vast ecosystem of plugins. To install plugins, navigate to Manage Jenkins
-> Manage Plugins
and install the necessary plugins such as Kubernetes, Git, and Pipeline.
Setting Up Nodes (Agents)
Jenkins uses agents to execute build tasks. You can configure Kubernetes-based agents to leverage the scalability of your Kubernetes cluster.
Install the Kubernetes plugin for Jenkins.
Navigate to
Manage Jenkins
->Manage Nodes and Clouds
->Configure Clouds
.Add a new Kubernetes cloud and configure it with the following settings:
Kubernetes URL:
https://kubernetes.default.svc
Kubernetes Namespace:
jenkins
Jenkins URL:
http://<your-jenkins-service>:8080
Credentials: Add Kubernetes credentials (service account token).
- Configure Pod Templates to define the Pods that Jenkins will use as agents. For example, you can define a Pod with a container that has Docker and Maven installed:
apiVersion: v1
kind: Pod
metadata:
name: jenkins-agent
spec:
containers:
- name: jnlp
image: jenkins/inbound-agent
- name: docker
image: docker:latest
- name: maven
image: maven:latest
- Save the configuration and create a new Jenkins pipeline job to test the setup.
Step 6: Creating a Jenkins Pipeline
Create a new Jenkins pipeline job and define a simple pipeline script to verify the configuration:
pipeline {
agent {
kubernetes {
label 'jenkins-agent'
defaultContainer 'jnlp'
yaml """
apiVersion: v1
kind: Pod
metadata:
name: jenkins-agent
spec:
containers:
- name: jnlp
image: jenkins/inbound-agent
- name: docker
image: docker:latest
- name: maven
image: maven:latest
"""
}
}
stages {
stage('Build') {
steps {
container('maven') {
sh 'mvn --version'
}
}
}
stage('Test') {
steps {
container('docker') {
sh 'docker --version'
}
}
}
}
}
Conclusion
A scalable and adaptable CI/CD solution may be achieved by installing Jenkins on Kubernetes. The entire procedure, from installing Jenkins and configuring agents to building up a Kubernetes cluster, has been guided through by this tutorial. With this configuration, you can effectively manage your Jenkins builds and deployments by utilizing Kubernetes' capabilities.
For further reading and resources, refer to the following links:
By following this guide, you now have a robust Jenkins setup on Kubernetes, ready to handle your CI/CD needs.