Integrating Jenkins with Kubernetes(DevOps Task-3)
Naitik Shah
Data Scientist & Machine Learning Engineer | Python, SQL, PyTorch, TensorFlow | NLP, Computer Vision, Predictive Analytics | Turning Data into $1.3M+ Business Impact
Alright, so let's first see the Problem Statement:
- Create container image that has Jenkins installed using Docker File or you can use the Jenkins Server RHEL 8/7.
- When we launch this image, it should automatically starts Jenkins service in the container.
- Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins.
- Job1 : Pull the GitHub repo automatically when some developers push repo to GitHub.
5. Job2 :
1. By looking at the code or program file, Jenkins should automatically start the respective language interpreter installed image container to deploy code on top of Kubernetes ( eg. If code is of PHP, then Jenkins should start the container that has PHP already installed )
2. Expose your pod so that testing team could perform the testing on the pod
3. 3. Make the data to remain persistent ( If server collects some data like logs, other user information )
6. Job3 : Test your app if it is working or not.
7. Job4 : if app is not working , then send email to developer with error messages and redeploy the application after code is being edited by the developer.
Alright, so let's start with the fun part:
So, in the first part, I will be using a Dockerfile, and in order to create your own Dockerfile, head over to your RedHat and remember to save your file name as Dockerfile and file extension as all files, in this case, the file should look like this:
We will be exposing our port 8080, because Jenkins uses port 8080.
So before jumping into using Jenkins, let me show you how to use Jenkins on Windows when it is installed on RedHat 8 Virtual Machine. First of all you need to have Jenkins installed on RedHat 8(Obviously!)
So, run these below commands on your RedHat 8 terminal:
And then, to check the IP address of your Virtual Machine, run ifconfig command, and on the first in the section named enp0s3, copy the numbers after inet, yours will be different:
Now go to your preferred browser in Windows, I will be using Edge, and paste the numbers you have copied(That is your IP address), and;
Your Jenkins should have loaded:
Step 1: To build the Docker image, build it using docker build -t NAME:TAG /location of Dockerfile/
Run a container from your newly built image and expose it to any available port using PAT, for example docker run -it -p 2301:8080 --name jenpro jentest:v5
Step 2: Now, let's move on to the tasks on Jenkins:
For Step 2, in Task 1, the code will be auto downloaded from GitHub, and for the same I will be using trigger method, it might sound complicated, but let me take you through the process step by step and you will realize that it is damn simple.
I have provided the link along with the authentication token to my local git, as soon as code is pushed, Git hook would work and run this task automatically. In the execute shell section, the webpages are being downloaded to this folder inside the container.
Task 2: Transfer In this task, the web pages downloaded inside the container are being transferred to the base Redhat using scp. I have already authorized a ssh key from my container to Redhat.
Task 3: Deployment In this task, the file type is checked by checking the extension, for example if the pages are built in html, then the extension of the file will be .html(obviously!), so then the code would detect that it is a html file and so launch an httpd server to deploy the pages, and to understand it further, if the pages are built in php, then the extension will be .php, and so a php supporting container would be deployed.
In order to run kubectl through your RedHat, make sure you have minikube running, and all of the necessary keys and certificates are transferred to RedHat and the config file is successfully built. I am giving an example config file below:
apiVersion: v1 kind: Config clusters: - cluster: server: https://192.168.0.192:8443 certificate-authority: /root/ca.crt name: my_cluster contexts: - context: cluster: my_cluster user: naitik users: - name: naitik user: client-key: /root/client.key client-certificate: /root/client.crt
Remember, that the above file is just an example, and your file will be different.
So, after creating the above file(config file), save it in .kube folder, and so now you can access the kubernetes via RedHat.
Task 4: So, this is the last task of this problem statement. This is the Testing task, and the deployed code will be tested, and in case it finds any error, an email to the desired email address will be sent, and for the same, I will be using status method, so in that method, whenever we access any web page using curl and Linux, the status 200 states that the page is working or it is not working. So, I will be using the same concept.
And done, your problem statement is done, and now, if your email service isn't working, make sure you have enabled less secure app access and turn off two step authentication.
After you have completed all of the above steps, run sudo service Jenkins restart in your Jenkins container.
So, now open your IP address in your web browser and see your website.
So, as we have used Kubernetes, if the pods crash, sit back and relax(because that is what DevOps is all about), the replica set working behind the deployment will relaunch the pods.
So, that's all from my side guys, see you later! and suggestions are welcome!