Deploy Jenkins on the Kubernetes cluster with AWS EFS

 

 

In this tutorial, we will deploy real-world applications on a Kubernetes cluster. We will continue to use AWS as our cloud provider and continue to integrate AWS services with the Kubernetes cluster.

In this tutorial, we will use the following AWS services EC2 instances and EFS.

In our previous blogs (Kubernetes on CentOS-7 and Kubernetes on Ubuntu), we created Kubernetes clusters on CentOS-7 and Ubuntu 18.04 operating systems. It doesn’t matter which underlying OS you use, the Kubernetes commands and deployment are the same. You can choose CentOS-7 or Ubuntu 18.04, whichever is convenient for you.

For this tutorial, we will use the Kubernetes cluster created on CentOS-7.

 

 

What are real-world applications?

 

Any application that is containerized can be deployed on a Kubernetes cluster. In this tutorial, we will deploy a very popular automation tool called Jenkins.

Jenkins is an open-source continuous integration/continuous delivery and deployment (CI/CD) automation software DevOps tool written in the Java programming language. It is used to implement CI/CD workflows, called pipelines. Jenkins is one of the most popular tools used for DevOps.

Jenkins is a very popular software in the DevOps world. 

 

What is Jenkins?

 

Jenkins is an open-source automation tool written in Java with plugins built for continuous integration. Jenkins is used to build and test your software projects continuously making it easier for developers to integrate changes to the project, and making it easier for users to obtain a fresh build. It also allows you to continuously deliver your software by integrating with a large number of testing and deployment technologies.

With Jenkins, organizations can accelerate the software development process through automation. Jenkins integrates development life-cycle processes of all kinds, including build, document, test, package, stage, deploy, static analysis, and much more.

Pipelines automate testing and reporting on isolated changes in a larger code base in real-time and facilitate the integration of disparate branches of the code into a main branch. They also rapidly detect defects in a code base, build the software, automate testing of their builds, prepare the code base for deployment (delivery), and ultimately deploy code to containers and virtual machines, as well as bare metal and cloud servers.

Jenkins automates the software builds in a continuous manner and lets the developers know about the errors at an early stage. A strong Jenkins community is one of the prime reasons for its popularity.

 

 

How does Kubernetes deploy a Jenkins application?

 

 

Any docker container, called a “pod” in the Kubernetes world, can be deployed on a Kubernetes cluster using various Kubernetes tools like “kubectl”, “kops”, “helm”, etc. A “pod” can be deployed on a Kubernetes cluster using the command line. However, deploying a single pod is good for basic testing and validating a Kubernetes cluster, but not very useful when deploying multiple pods.

To deploy Kubernetes pods we create “yaml” files. YAML is a human-friendly language. YAML files are mainly used for the configuration of pods, services and deployments. YAML is a manifest file, which performs the above-mentioned functions. They define how a “pod” should be deployed, and interact with other pods and/or services.

“Pods” communication is managed by Kubernetes services. Service is an abstraction to help you expose groups of Pods over a network. Each Service object defines a logical set of endpoints (usually these endpoints are Pods) along with a policy about how to make those pods accessible. For more details on services, please refer to the below link:

https://kubernetes.io/docs/concepts/services-networking/service/

In our Kubernetes Jenkins deployment when we use the Persistent Volumes (PV), the WordPress data is stored in the AWS EFS, which is mounted on a directory in the EC2 instance. Kubernetes pods do not directly access the PV, but it is done through Persistent Volume Claim (PVC). You can read more about PV vs PVC in the below link.

https://kubernetes.io/docs/concepts/storage/persistent-volumes/#:~:text=PVs%20are%20volume%20plugins%20like,for%20storage%20by%20a%20user.

When we deploy a Jenkins application, it deploys a Jenkins pod. The Jenkins pod is the front end in which we can log in, install plugins and create Jenkins jobs or CI/Cd pipelines.

We can control the number of Jenkins pods that will be deployed. If we use multiple worker nodes then it is a good practice to deploy a Jenkins pod on each of the nodes, so that there is High Availability in case of failure of an AWS availability zone.

We will be using a separate deployment file for Jenkins. You can create a single deployment file for both, but it is easier to separate them.

In Kubernetes, pods communicate with each other using services. So each pod must have its own unique service defined. In our WordPress deployment, we will be using the Kubernetes service of the type: “NodePort

To make things easier, we have created 4 “yaml” files for the Jenkins deployment. So there are 4 “yaml” files for Jenkins (pv, pvc, deployment and service file

Purpose of the “yaml” files:

  • PV file –> For creating the Persistent Volume.

  • PVC file –> For providing the storage to the pods. 

  • Deployment file –> Specifies details about the number of pods, container image, volume mount points (PVC), etc.

  • Service file –> Specifies the port number of the service and type of service. 

 

 

How to deploy a Jenkins application on a Kubernetes Cluster?

 

 

 

Introduction and Pre-requisites

 

 

There are 2 ways to deploy the Jenkins container.

1).   In an earlier blog (WordPress on Kubernetes) we deployed the WordPress application on a Kubernetes cluster. You can deploy a Jenkins pod on the same infrastructure. You can deploy multiple applications on a single Kubernetes cluster, but ensure that you monitor the service ports that you are allocating to each application and above all monitor the free CPU and RAM resources, before deploying any new pods. If you are doing that then, please skip to Step 2

2).   If you have not deployed WordPress as mentioned in our previous blog and are ONLY deploying the Jenkins pod on the Kubernetes cluster, please follow the below Steps.

 

 

Step 1: Pre-requisites and Infrastructure setup

 

 

Use the CentOS-7 Master and Worker nodes AMIs created in this article.

Launch the 2 AMIs using EC2 instance =  “t3a.small” 2 vCPUs and 2 GB RAM for each EC2 instance.

Create 2 EFS storages. The first one is for storing the Kubernetes configuration files and the second one is for storing the WordPress data files.

 

Give appropriate names to the EFS storages (Ex: “config-files” for Kubernetes config files and “data-files” for the Kubernetes deployment data)

Creating AWS EFS and mounting it on an EC2 instance has been defined in this article.

Note: You can use even 1 storage and then create 2 separate mount points (1 for storing the WordPress configuration files and 1 for storing the WordPress data files). However, it’s always good practice to create 2 separate EFS mount points and then mount them on different directories.

 

 

Kubernetes “pods” are ephemeral, which means when the pod is stopped or killed all the data is lost. This is OK in a development environment but is not useful for production systems. In all our Kubernetes deployments, we will use Persistent Volumes.

Once the instances are launched, you have log in to the instances (Master and Worker) as the “jenkins” user. This user was created in the blogs mentioned here pre-requisites, and centos-7 installation. It is important to follow the steps mentioned in these blogs on creating the correct user and setting up the systems before proceeding.

All the Kubernetes deployments are done from the Master node, but the pods are actually deployed on the worker nodes. For any Kubernetes deployment, you should take care to ensure that no pods should be deployed on the master node. Only the pods that are required to run the Kubernetes cluster should run on the Master node. All other application deployments should be on the Worker nodes ONLY.

 

 

Step 2: On the Master node

 

 

Login as the “jenkins” user on the Master Node. 

    • $ cd ~

    • $ mkdir jenkins-cfg                  (you can give any directory name you like)

Next mount the EFS created for the config files on this directory. The command to mount the directory is mentioned in the AWS EFS console and has been explained in the blog.

    • $ sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport fs-011a40c8f24e5b94d.efs.ap-south-1.amazonaws.com:/ /home/jenkins/jenkins-cfg

“/home/jenkins/jenkins-cfg” is the complete path of the directory we have created for the Kubernetes config files.

    • $ df -k                    (should show the “jenkins-cfg” mounted on the EFS)

    • $ cd jenkins-cfg

    • $ sudo yum install -y git

Download the Jenkins “yaml” files from github location.

As we are running the commands in the “jenkins-cfg” directory, this will download the required  “yaml” files for deploying the Jenkins application on the Kubernetes cluster in the current “jenkins-cfg” directory

Now let’s set up the Worker node for the Jenkins data files.

 

 

Step 3: On the Worker node

 

 

Login to the Worker Node as “jenkins” user. 

    • $ cd ~

    • $ mkdir jenkins-data           (you can give any directory name you like)

Now mount the EFS created for the jenkins data files on this directory. The command to mount the directory is mentioned in the AWS EFS console and has been explained in this article.

    • $ sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport fs-09b70377fe373b9f6.efs.ap-south-1.amazonaws.com:/ /home/jenkins/jenkins-data   

/home/jenkins/jenkins-data” is the complete path of the directory we have created for the jenkins data files.

    • $ df -k                    (should show the jenkins-data mounted on the EFS)

 
The directory setup on both the Master node and Worker node is complete.

 

 

 

Step 4: On the Master node

 

 

Login to the Master Node as “jenkins” user for the next steps. 

NOTE: In our “yaml” files, we have mentioned the container image as “jenkins/jenkins”. This image is downloaded to the worker node from hub.docker.com if the image does not exist on your nodes. Once downloaded, it will re-use this image every time the pod is stopped or started. 

    • $ cd ~/jenkins-cfg

    • $ cd ~

    • $ kubectl create -f jenkins-cfg/

This command will deploy the “jenkins” pod and create the necessary services 

If everything is OK, you should see an output like below:

This means “jenkins” is successfully deployed on the Kubernetes cluster.

 

 

Step 5: How to access the Jenkins Application using a Web browser

 

 

Get the public IP of the Worker node from the EC2 console.

In the service “yaml” file, we have used 32000 as the NodePort for the “jenkins” service. 

Open any web browser and paste the “public IP address of the Worker Node” EC2 instance with port no.

http://41.102.32.219:32000/

Syntax for accessing WordPress: HTTP://<public-ip-address-worker-node>:32000 

Port”32000”  is defined as NodePort of “Jenkins”. It is on this port that we will access “Jenkins” from the external world or from the internet.

You should see the below “jenkins” login screen.

 

 

 

 

Step 6: Logging into Jenkins and installing the plugins

 

 

On the Master node, login as “jenkins” user. 

    • $ kubectl get all

From the above command you can the deployed “jenkins” pod name. 

Use the below command to view the “jenkins” pod logs. The administrator password can be found in the logs. 

    • $ kubectl logs -f jenkins-7967df4c6c-n4q6c

*************************************************************

*************************************************************

*************************************************************

Jenkins initial setup is required. An admin user has been created and a password generated.

Please use the following password to proceed to installation:

27f4ca7473c54407a30954b58b47d57b

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

*************************************************************

*************************************************************

*************************************************************

 

Enter the password in the below login screen. (NOTE: When you check your logs, you will find a different password)

 

Once the password is entered, press “Continue” to go to the next screen.

 

Press the “Install suggested plugins” which will install all the default plugins. 

You should see the below screen while the plugin installation is going on:

 

Once the plugins are installed, it will automatically go to the “Create First Admin User” screen for setting the Administrator details. You can enter the below details or put your admin username and password. 

Username = admin

Password = admin

Confirm password = admin

Full name = Admin

Email = abc@abc.com

 

 

 

Once you enter all the details, “Save and Continue” to the next screen.

 

 

This is the same URL and port number which we have entered in the web browser. 

Click “Save and Finish“. You should see the below screen. 

 

Click on “Start using Jenkins”. It will bring you the Jenkins Dashboard. 

 

Congratulations !!! You have successfully installed a Jenkins container on a Kubernetes cluster. You can now start using Jenkins for your automation tasks. 

 
 

 

This completes the installation of Jenkins on a Kubernetes cluster.