DevOps Automation with Kubernetes
Manmohan .
Sr.Executive DevOps Engineer | Ex-DevOps Engineer at Mediamelon.Inc | DevOps | AWS | Big Data | Hadoop | Kubernetes | Jenkins | Docker | AKS | EKS | Azure DevOps | CI-CD | RHCSA8 | RH294 | GitOps | DevSecOpsk
This the third task of DevOps AL.
The objectives of the task are:
1. Create container image that’s has Jenkins installed using dockerfile Or You can use the Jenkins Server on RHEL 8/7
2. When we launch this image, it should automatically starts Jenkins service in the container.
3. Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins
4. 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. 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
So, let’s start to accomplish the task’s objectives one by one.
On the very first step, I have created my own Jenkins image using Dockerfile. I am going to use this image to launch a Jenkins container.
To create an image from this Dcokerfile, we have to run the above command:
“docker build -t [name] [location_of_Dockerfile]”
After creating the image, I have launched a container from this image and exposed it to the outer world. For convenience, I have also mounted one of the directories of this container to the same on my base RedHat system. You will get to know about it.
I have used the above command to launch the container through the image:
After launching the container, we need to open the URL and complete the basic setup of the Jenkins server. We have to download some plugins that are required in this task such as SSH, Email, and so on. I have done the entire setup on this post: https://www.dhirubhai.net/pulse/end-automation-using-github-jenkins-docker-manmohan-singh
You can go through it for more details.
Now, let us move towards jobs.
The first job is to pull the code from GitHub that will be uploaded by the developer.
Here is the job configuration:
This job will be triggered by a remote URL. This kind of configuration is done by:
This Job will trigger the Job2 after its successful completion.
The console output of the first job is:
Then, we have to configure the Job2. The objective of Job2 is to deploy the code on the respective pod using Kubernetes and expose it afterward. Here is the configuration for the second job:
This job will be triggered by Job1.
And I ran the code in Job2 on the top of base RedHat using SSH.
The console log of the second job:
Here comes the third job. This job will do the testing on the deployed environments.
This job will be triggered by the second job:
Now, we have a requirement to send an email after the testing of the environments. And for this purpose, I have added an option to send the email in the third job only using the Email plugin.
Console log of the third job:
Here is the build pipeline view of all these jobs:
Here are all the resources launched on Kubernetes:
I have used the YAML program files to launch resources such as service, PVC, deployment. Here is the code:
Webserver Service:
Webserver PVC:
Webserver Deployment:
All these codes are written in a single file, names as “webserver.yml”
PHP server service:
PHP server PVC:
PHP server Deployment:
All these codes are written in a single file, names as “phpserver.yml”
This is the code to trigger the Jenkins build remotely:
This code needs to be written inside the “post-commit” file, in the “hooks” folders under the “.git” directory.
These are the environments where the codes were deployed:
Hence, all the objectives of the task have been accomplished successfully.
So, that’s all as of now. I hope you liked the article. Thanks for reading!
Have a good day ;)