Webserver deployment on k8s cluster using groovy script

Ankit Prakash
3 min readOct 15, 2020

Task Description

  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 and job4 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 :

(i) 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 )
(ii) Expose your pod so that testing team could perform the testing on the pod

(iii)Make the data to remain persistent using PVC ( If server collects some data like logs, other user information )

7. Job3: Test your app if it is working or not.

8. Job4: if the app is not working, then send email to the developer with error messages and redeploy the application after code is being edited by the developer

Some plugins should be installed to keep up the work:

Job DSL
Script Security Plugin
GitHub plugin
Workspace Cleanup Plugin
CloudBees Docker Build and Publish plugin
Email Extension Plugin
Build Pipeline Plugin

Creating Jenkins Job 1 seed job.

Writing DSLs First for Job2 creation: using job1’s success as trigger for task6job2 creation.

#Creating Job 2 job(“task6job2”) {
triggers {
upstream {
upstreamProjects(“task6job1”)
threshold(“SUCCESS”)
}
}
steps {
if(shell(“sudo ls /root/task3dev | grep html”)) {
if(shell(“sudo kubectl get deployment | grep webserver”)){
shell(“echo \”webserver setup already\””)
}
else{
shell(“sudo kubectl apply -f /root/yamls/httpweb.yaml”)
POD=shell(“sudo kubectl get pods -l ws=httpserver-service -o jsonpath=\”{.items[0].metadata.name}\””)
shell(“sudo kubectl cp /root/task3dev ${POD}:/usr/local/apache2/htdocs/”)
}
}
else {
shell(“echo \”No HTML file \””)
}
if(shell(“sudo ls /root/task3dev | grep php”)) {
if(shell(“sudo kubectl get deployment | grep php”)){
shell(“echo \”php setup already\””)
}
else{
shell(“sudo kubectl apply -f /root/yamls/httpweb.yaml”)
POD=shell(“sudo kubectl get pods -l ws=phpserver-service -o jsonpath=\”{.items[0].metadata.name}\””)
shell(“sudo kubectl cp /root/task3dev ${POD}:/usr/local/apache2/htdocs/”) } }
else{ shell(“echo \”No PHP file\””) } }

Job3:Now here we will be using Job2 as the upstream project. After build, we need to send the Email to the developer, in this case, my email if the job Fails.

job(“task6job3”){
triggers{
upstream{
upstreamProjects(“task6job2”)
threshold(“SUCCESS”) }
} steps{
httpno=shell(“sudo kubectl get pods -l ws=httpserver-service -o jsonpath=\”{.items[0].metadata.name}\””)
phpno=shell(“sudo kubectl get pods -l ws=phpserver-service -o jsonpath=\”{.items[0].metadata.name}\””)
if(${httpno}==200){
shell(“echo HTTPServerRunning”)
shell(“exit 0”)
} else{
shell(“exit 1”)
} if(${phpno}==200){
shell(“echo PHPServerRunning”)
shell(“exit 0”)
} else{
shell(“exit 1”)
} } publishers {
extendedEmail {
recipientList(‘ankit
*****@gmail.com’)
defaultSubject(‘Default Subject’)
defaultContent(‘Default Content’)
contentType(‘text/html’)
triggers {
failure {
subject(‘Something Went Wrong’)
content(‘Something Went Wrong in Job 3’)
sendTo {
recipientList() } } } } }

Output console:-

Built pipeline

--

--