Jenkins on Kubernetes: Complete Setup and Configuration
Amr Saafan
Founder | CTO | Software Architect & Consultant | Engineering Manager | Project Manager | Product Owner | +27K Followers | Now Hiring!
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:
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:
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
chmod +x minikube
sudo mv minikube /usr/local/bin/
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:
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
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.
helm repo add jenkins https://charts.jenkins.io
helm repo update
领英推荐
kubectl create namespace jenkins
helm install jenkins jenkins/jenkins --namespace jenkins
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 https://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.
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
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.