Kubernetes and Jenkins: A Brief Introduction
The IT industry is now quickly moving towards using containers in software development. The reason behind is that the containers are a very cost-effective solution, and also they decrease the time needed for the development. To manage these containers, orchestration tools like Kubernetes come into play and business needs to set up Kubernetes with Jenkins. However, before taking you further, let’s get a quick idea about Jenkins and Kubernetes:
Kubernetes is an open-source tool created by Google; it manages containers and also takes care of their failure. It supports the majority of CI/CD tools and Kubernetes Security tools which allow developers in running tests, deploying builds and updating the application without any downtime.
Jenkins is one of the most popular CI/CD tools; it is so popular because it monitors tasks repeatedly. During the development phase and shows errors in the early stages of the development. This blog will focus on how to set up Jenkins on Kubernetes.
Prerequisites to Configure Jenkins with Kubernetes
- A Linux system (in my case I’m using Linux).
- Kubernetes (minikube) and Docker should be installed in the system.
The process to setup Jenkins:
1. Create a YAML File:
– name: jenks
– containerPort: 8080
– name: jenk-vol
– name: vol
Save this file as jenkins.yaml
In this deployment, I am using emptyDir volumes, but we can also use other volumes like persistent volume and persistent volume claim.
Create a YAML file for Jenkins service and save it as jenk-svc.yml
– name: jenkines-svc
Start the minikube in your system: sudo minikube start –vm-driver=none
Start the minikube using the command: sudo minikube start –vm-driver=none
Create Jenkins deployment: kubectl create -f jenkins.yaml
This command will pull Jenkins images from the Docker repositories like Docker Hub and create a pod with one replica.
2. Create Jenkins Service
Jenkins deployment is created in the last step, but it is not accessible to the outside world, to make it accessible. This is why users stuck in queries like how to set up Jenkins on Kubernetes. For this, we use services, I am using NodePort service. We use kubectl create -f jenk-svc.yml to create the service on Kubernetes, here jenk-svc.yml is the YAML file we created earlier. If you don’t want to create the service YAML file you can expose the deployment using the command: kubectl expose deployment –port= –type=.
The NodePort will expose it on all nodes IP’s, and also we are using port 30002 so we can access the Jenkins on port 300002 to setup Jenkins on Kubernetes.
We can check our Deployment and service details using commands:and: Sudo kubectl get pods.
Kubectl get deployment
Kubectl get svc
We can also manage our Deployment using dashboard:
Command: sudo minikube dashboard
This command will generate a URL that leads us to the minikube dashboard. In that dashboard, we will get all the details of deployment, pods, and services. We can also manage these using the dashboard.
After Deployment is done on Kubernetes we will find our pod name using the command: Sudo kubectl get pods.
Here the name of my Jenkins pod is Jenkins-84f47b765c-tpxjq, after getting the name of the pod now we will find the password of Jenkins, without that password we can not access the Jenkins. To find that we use command kubectl logs Jenkins-84f47b765c-tpxjq. The password will be at the end of the logs.
We can also find this password at location:
/var/jenkins_home/secrets/initialAdminPassword inside the pod.
3. Access Jenkins Using Browser
After finding the password now, we will access the Jenkins using a browser. For this, first of all, we will find the IP using the command sudo minikube ip. We will use that IP with NodePort to access Jenkins in a browser (IP:port), in my case I used port 30002 in the NodePort service so I will use 18.104.22.168:30002 to access the Jenkins.
After browsing the IP on port 30002 we will get into Jenkins. At first, Jenkin will ask the password that we found earlier without that password we can not unlock the Jenkins.
After entering the password we will get into the sign-up page of Jenkins, there we can create a new admin user to access the Jenkins Dashboard, or we can use the default admin user to get into the dashboard of Jenkins. This will proceed to set up Jenkins on Kubernetes.
After signing up there, we will get into the Dashboard of the Jenkins where we can perform our CI/CD tasks.
How to set up Jenkins on Kubernetes issue resolved!