How to Install Jenkins on Kubernetes?

Photo of author

By admin

If we look from a developer’s point of view, it is hard to ignore the significance of Continuous Integration/Continuous Deployment (CI/CD) pipelines, which are considered one of the core components to automate your software delivery process. This component plays a significant role in streamlining the entire workflow for different teams and thus further helps increase their productivity.

The pipeline helps create code, runs tests (CI), and enables the deployment of a new version of the application. Regular involvement of Continuous Integration/Continuous Deployment (CI/CD) is crucial to integrate the code with a shared repository numerous times.

Jenkins is a widely-used open source automation server that assists in setting up CI/CD pipelines. However, it is proved that to provide a new automation layer to Jenkins, the Kubernetes cluster is very important. With the help of Kubernetes, it becomes possible to use resources and servers effectively, and also the underlying infrastructure can be managed without putting any extra load.

The need to build the CD (continuous delivery) pipeline and setting up Jenkins via the Kubernetes engine offers the foremost benefits as compared to standard VM-based deployment that is as follows:

  • You get the feature of using one virtual host that allows you to run jobs on different operating systems.
  • The Engine also provides ephemeral build executors, and so every build can also run in a clean environment.
  • Kubernetes Engine also provides you with the Google global load balancer, and thus it becomes easy to handle SSL termination. A global IP address will be provided to you so that you can share it with other users and connect with them Google’s backbone network.

In this write-up, you will get familiar with the entire procedure of installing Jenkins on Kubernetes.

Steps to Install Jenkins on Kubernetes

Before discussing the steps, you need to take note of the prerequisites for installing Jenkins on Kubernetes:

Prerequisites

Prior to beginning the installation process of Jenkins on Kubernetes, some essentials that must be taken care of initially are to set up the Kubernetes cluster and kubectl on your machine. If you don’t have a running Kubernetes cluster, all you require to do is follow the steps described in your Kubernetes Quickstart to set up a Kubernetes cluster on DigitalOcean.

Step 1: Installing Jenkins on Kubernetes

To set up Jenkins, you would require to create:

  1. A namespace that permits you to set apart Jenkins objects within the Kubernetes cluster.
  2. A PersistentVolume that allows storage of your Jenkins data as it helps in preserving data across restarts.

Now to set up an environment for Jenkins, Kubernetes has an API, and you can get across the desired state by using either a YAML or JSON file. However, in such a case, it is wise to use the YAML file to launch Jenkins. And, be sure about the kubectl commands before you use them to manage the clusters.

First, use the following kubectl command to create the Jenkins namespace:

kubectl create namespace Jenkins

Further, to deploy Jenkins, you must build a new YAML file.

Create a new file named jenkins.yaml and open it with the help of nano or your preferred editor.

Now, the next thing that you need to do is to add the following code and define the Jenkins image, in addition to its port, and several more 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: {}

NOTE: As you see in this step, we have created a deployment by making use of the Jenkins LTS image and have opened ports 8080 and 50000. These ports give access to Jenkins.

Step 2: Creation of next deployment in the Jenkins namespace

Ensure you provide adequate time for the cluster to pull the Jenkins image and get the Jenkins pod running. The command you need to provide is kubectl, as it helps in verifying the pod’s state.

kubectl create -f jenkins.yaml –namespace jenkins

The output appears similar to this:

NAME READY STATUS RESTARTS AGE

jenkins-6fb994cfc5-twnvn 1/1 Running 0 95s

There will be a difference in the pod name, and in the process of its running; just ensure to expose it using a Service. In this command, the NodePort Service type will be used. Also, there is a need to create a ClusterIP type service to connect to Jenkins.

Launch the new file after creating it as Jenkins-service.yaml:

nano jenkins-service.yaml

Now, the most vital step is to add the following code and then try to define the NodePort Service:

NOTE: In the following YAML file, we have defined NodePort Service and then exposed port 8080 of the Jenkins pod right to port 30000.

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, finally, we are going to create the Service in a similar namespace:

kubectl create -f jenkins-service.yaml –namespace jenkins

Here is the command to verify if the Service is running or not:

kubectl get services –namespace jenkins

The output will appear like this:

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE

jenkins NodePort your_cluster_ip <none> 8080:30000/TCP 15d

As Jenkins and NodePort are operational, now you can access the Jenkins UI. With NodePort and Jenkins functional, you are ready to access the Jenkins UI.

Step 3: Accessing the Jenkins UI

In this step, let us try to get familiar with the procedure for exploring the Jenkins UI. You must learn about the Nodeport service and its availability in getting the TCP port 30000 with the cluster nodes. However, it is a difficult task to get a node IP for gaining exact access to the Jenkins UI. Using kubectl commands to retrieve node IPs is the fastest and accurate way, here is how to-

kubectl get nodes -o wide

kubectl will produce an output with your external IPs:

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

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

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

NOTE: Copy any one of external_ip values, then open a web browser, and navigate to http://your_external_ip:30000. As you do so, a page will appear asking for an administrator password.

You require to use kubectl and pull the password from those logs.

First, return to your terminal and retrieve your Pod name with the following command:

kubectl get pods -n jenkins

The output will appear as shown below:

NAME READY STATUS RESTARTS AGE

jenkins-6fb994cfc5-twnvn 1/1 Running 0 9m54s

As you can see here, you must examine the pod’s logs for the admin password. Now, all that you require to do is just substitute the highlighted section just with your pod name by using the following command:

kubectl logs jenkins-6fb994cfc5-twnvn -n jenkins

As you scroll up or down, you will find the password:

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

This may also be found at: /var/jenkins_home/secrets/initialAdminPassword

. . .

Finally, you need to copy your_jenkins_password and then go to your browser and paste it into the Jenkins UI. As you enter the password, Jenkins will quickly direct you to install the plugins.

Once the installation process gets completed, Jenkins will load a new page, and then you would be required to create an admin user. While creating the Admin user, just ensure to enter all essential details in the given fields, or you may also skip this step by pressing the skip and continue as admin link. However, by default, your username will be taken as admin and your password as your_jenkins_password.

Moving ahead, the following screen will ask for instance configuration, and then you are required to click the Not now link and continue.

Jenkins successfully print Jenkins is ready!

Click on start using Jenkins, and in a matter of a few seconds, the Jenkins home page will appear.

Step 4: Running a Sample Pipeline

Jenkins finally created pipelines. In this step, we will create one of Jenkins’ sample pipelines.

As you see the Jenkins home page, click on the New item link displayed on the left-hand menu.

As a new page opens up, select pipeline and then press OK.

Jenkins will take you to the pipeline’s configuration, you need to press the Ok button. Look for the Pipeline section and then search for Hello World from the try sample pipeline dropdown menu, which is on the right-hand side.

After selecting the Hello World, click the Save button.

Jenkins will lead you to the main page of the pipeline where you have to enter the Build Now option that could be on the left-hand menu. The moment you click on Build Now, the pipeline begins to run.

Also, make sure you do not forget to examine the console output and look at what happened when the pipeline was running.

Conclusion

We’ve reached the final section of our guide. You already have learned to install and configure Jenkins on a Kubernetes cluster. Let us not forget that Jenkins has got a repository of plugins; using those plugins makes it possible to carry out complex operations. And the kubectl command-line tool will help you to perform these tasks easily. Always remember to double ensure the commands before you deploy it. If you have any more queries regarding the same, drop a comment in the below box. Stay connected for more guides.

Leave a Comment