Deployment Of Webserver On Kubernetes Using Groovy In Jenkins
In this post, I will share some useful groovy code that can be used when creating a Jenkins job using a groovy script, Pipelines are Jenkins jobs enabled by the Pipeline (formerly called “workflow”) plugin and built with simple text scripts that use a Pipeline DSL (domain-specific language) based on the Groovy programming language. So this groovy script can be used within a declarative pipeline as well if you wish to create Jobs dynamically from within another job. The simple code is as follows, I have added a few inline comments so the code snippet will be easy to understand.
Problem Statement
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 start the Jenkins service in the container.
3. Create a job chain of job1, job2, job3 using build pipeline plugin in Jenkins
4. Job2 ( Seed Job ): Pull the Github repo automatically when some developers push the repo to Github.
5. Further on jobs should be pipeline using written code using Groovy language by the developer
6. Job1 :
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
7. Job3: Test your app if it is working or not. the app is not working, then send an email to the developer with error messages and redeploys the application after code is being edited by the developer.
Tools required to complete this task -
- Git / GitHub -> Source Code Management & Version Controlling
- Jenkins -> Automate software development related to deploying
- Kubernetes -> container-orchestration system for automating application deployment, scaling, and management
- Docker -> Container Engine Kubernetes will manage
- RHEL8
- Minikube on MacOS
We will start by setting up a GitHub repository. A Webhook is configured as well so that whenever a push is done, Job 1 build starts. When Developer changed something into GitHub then Jenkins Job-1 should be triggered automatically. For this, we have to create a webhook trigger on Github so that it automatically can trigger. But we have to remember one thing, Github belongs to the public world and my Jenkins is running into my RHEL8 which has private IP. So the Public world can’t connect to the private world. But we have one way. We can create a tunnel something like this so that the public world connect to my private world. ngrok is a software that I used to expose my private IP on the internet.
My entire setup is done on the top of RHEL8. First, we have to make change some changes in the docker. service file.
systemctl daemon-reload
systemctl restart docker.service
The following plugins to be downloaded in Jenkins
- job DSL
- Cloudbees docker build and publish plugin
- script-security plugin
Go to Manage Jenkins->Configure Global Security and disable script security
Now we will be doing a seed job.
You can find the groovy script in my GitHub repo
After you build it, you get three new jobs and a new view in Build Pipeline.
You have to manually build the job1, job2, and job3 respectively. After the successful build of these jobs, you’ll get the following outputs.