CI/CD (Continuous Integration/ Continuous Deployment) is a core concept of the DevOps world. It helps us to automate the software development processes: building, testing and deploying the source codes.
Among many CI/CD tools, Jenkins is one of the most well-known open-source tool that help setting up the pipelines for continuous integration and continuous deployment.
This tutorial will show you the way to install Jenkins on a Kubernetes cluster.
Preparing a Kubernetes cluster
There are many ways to setup a Kubernetes cluster. You can use the public cloud services to provision your own cluster on AWS, GCP, Linode, etc. Besides, the simplest way is installing minikube on your local machine.
Here, I have a Kubernetes cluster with 5 nodes provisioned by using kubespray.
$ kubectl get node
Installing Jenkins
Firstly, we will create a new namespace to provide the isolation mechanism for controlling the CI environment:
$ kubectl create namespace jenkins namespace/jenkins created
Verifying that the namespace jenkins was successfully created by running the following command:
$ kubectl get namespaces
Then, let’s create Jenkins deployment. You can use the following manifest file to create a single instance deployment of Jenkins.
Creating file `jenkins-deployment.yaml`:
apiVersion: apps/v1 kind: Deployment metadata: name: jenkins-deployment 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: {}
The deployment using the Docker image `jenkins/jenkins:lts` and expose ports `8080` and `50000`.
Apply the above manifest:
$ kubectl create -f jenkins-deployment.yaml --namespace jenkins
It takes a few minutes to pull the image jenkins/jenkins:lts and make the pod running. Verifying that the Jenkins deployment was successfully deployed:
$ kubectl get pod -n jenkins NAME READY STATUS RESTARTS AGE jenkins-deployment-794699f9bc-rln7x 1/1 Running 0 66s
Deploying Jenkins service
After creating the Jenkins deployment and the Jenkins pod is running, you have to expose the connection to the running pod using a service. In this tutorial, we will use NodePort and ClusterIP services.
Let’s create file `jenkins-svc.yaml` as follows:
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
Now, apply the above manifest and create the service in namespace `jenkins`:
$ kubectl create -f jenkins-svc.yaml --namespace jenkins
Verify that the service is running:
$ kubectl get svc --namespace jenkins NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE jenkins NodePort 10.233.2.81 8080:30000/TCP 30s jenkins-jnlp ClusterIP 10.233.53.44 50000/TCP 30s
From now on, we can access the Jenkins dashboard using the NodePort service.
Accessing the Jenkins dashboard
The NodePort service is accessible from port `30000` on any worker node of the Kubernetes cluster:
`http://worker_node_ip_address:30000`
You can get the IP address of worker nodes by the following command:
$ kubectl get node -o wide NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME node1 Ready master 149m v1.19.2 192.168.0.111 Ubuntu 18.04.5 LTS 4.15.0-121-generic docker://19.3.13 node2 Ready master 148m v1.19.2 192.168.0.112 Ubuntu 18.04.5 LTS 4.15.0-121-generic docker://19.3.13 node3 Ready master 148m v1.19.2 192.168.0.113 Ubuntu 18.04.5 LTS 4.15.0-121-generic docker://19.3.13 node4 Ready 147m v1.19.2 192.168.0.114 Ubuntu 18.04.5 LTS 4.15.0-121-generic docker://19.3.13 node5 Ready 147m v1.19.2 192.168.0.115 Ubuntu 18.04.5 LTS 4.15.0-121-generic docker://19.3.13
For example, let’s open the web browser and go to: `http://192.168.0.111:30000`
In order to retrieve the Administrator password, you can run the following command:
$ kubectl logs jenkins-deployment-794699f9bc-rln7x --namespace jenkins
Enter the Administrator password then follow the next setup steps and you will successfully access to the Jenkins dashboard.
Conclusion
Jenkins is a really powerful CI/CD tool for any DevOps engineer and also software developer. This tutorial has gone through all steps of deploying Jenkins on a Kubernetes cluster. Thanks to Jenkins, we will be more productive and it helps us reduce time to build, test and deploy codes.
Thanks for reading and please leave your suggestion in the below comment section.