Deploy App On Kubernetes With Jenkins Groovy Script
Varun Bhutani
Senior Salesforce Developer || 4x Salesforce Certified || 1X Ranger || 4X Superbadge || LWC || Trigger || Integration || Apex
As we know jenkins is CI/CD tool by writing groovy script we can achieve automation in term for creating job and etc, In this task we run our application on kubernetes .
Groovy is a DSL for Jenkins .??
: 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 )
: Expose your pod so that testing team could perform the testing on the pod
: Make the data to remain persistent using PVC ( If server collects some data like logs, other user information )
: Test your app if it is working or not.
: 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
First we will create jenkins seed job that auto create and build another jobs
task6-seedjob
This above jenkins code contains this below script code that will create 3 new job in jenkins
jenkins seed job file = > task6-seedjob
job("J1_groovy"){ description("this job will copy the file in folder ") scm { github('varunbhutani98/task-6' , 'master') } triggers { scm("* * * * *") } steps { shell(' sudo cp -vrf * /root/task6/ ') } } job("J2_groovy"){ description("this Job will create deployment for website and expose deployment") triggers { upstream('J1_groovy', 'SUCCESS') } steps { shell(''' if sudo ls /root/task6/ | grep php then echo " Going to Start deployment for php code " sudo cd /root/task6/ sudo ls sudo kubectl apply -f /root/task6/jen_kube_php.yml else echo "There is no php file" fi if sudo ls /root/task6/ | grep html echo " Going to Start deployment for html code " sudo cd /root/task6/ sudo kubectl apply -f /root/task6/jen_kube_html.yml else echo "There is no html file" fi ''') } } job("J3_groovy") { steps{ shell(''' status=$(curl -o /dev/null -s -w "%{http_code}" https://192.168.99.101:31000) if [[ $status == 200 ]] then echo "Running HTML Website" exit 0 else exit 1 fi status=$(curl -o /dev/null -s -w "%{http_code}" https://192.168.99.101:32000) if [[ $status == 200 ]] then echo "Running Php Website" exit 0 else exit 1 fi ''') } triggers { upstream('J2_groovy', 'SUCCESS') } publishers { extendedEmail { recipientList('[email protected]') defaultSubject('Job status') attachBuildLog(attachBuildLog = true) defaultContent('Status Report') contentType('text/html') triggers { always { subject('build Status') content('Body') sendTo { developers() recipientList() } } } } } }
JOB-1 = J1_groovy
ABOVE BIG FILE WILL CREATE J1_GROOVY JOB THAT WILL DOWNLOAD THE CODE FROM GITHUB AND COPY IN REDHAT /TASK6 FOLDER
job("J1_groovy"){ description("this job will copy the file in folder ") scm { github('varunbhutani98/task-6' , 'master') } triggers { scm("* * * * *") } steps { shell(' sudo cp -vrf * /root/task6/ ') }
}
JOB-2 = J2_groovy
THIS JOB2 WILL CHECK THE TYPE OF CODE IN WORKSPACE IF CODE IS FOR PYTHON THAN THIS JOB WILL LAUNCH PYTHON INTERPRETER ,, IF CODE IS OF TYPE HTML THAN THIS SCRIPT WILL LAUNCH HTML APPLICATION
WE HERE DEPLOYING OUR APPLICATION ON TOP OF KUBERNETES WITH PVC
JOB-3 = J3_groovy
THIS THIRD JOB WILL DO TESTING , IF OUR APP IS WORKING FINE THAN STATUS CODE WILL BE 200 MEANS NO ERROR , IF OUR APP IS NOT RUNNING IT WILL GIVE ERROR CODE 404 AND MEANS OUR APP IS NOT RUNNING AND THERE IS SOME ISSUE IN THIS , THAN DEVELOPER WILL CHECK AND CORRECT ,
ALSO THESE JOB WILL SEND EMAIL NOTIFICATIONS TO DEVELOPERS FOR EVERY BUILD
job("J3_groovy") { steps{ shell(''' status=$(curl -o /dev/null -s -w "%{http_code}" https://192.168.99.101:31000) if [[ $status == 200 ]] then echo "Running HTML Website" exit 0 else exit 1 fi status=$(curl -o /dev/null -s -w "%{http_code}" https://192.168.99.101:32000) if [[ $status == 200 ]] then echo "Running Php Website" exit 0 else exit 1 fi ''') } triggers { upstream('J2_groovy', 'SUCCESS') } publishers { extendedEmail { recipientList('[email protected]') defaultSubject('Job status') attachBuildLog(attachBuildLog = true) defaultContent('Status Report') contentType('text/html') triggers { always { subject('build Status') content('Body') sendTo { developers() recipientList() } } } } }
}
jen_kube_html.yml
THIS IS THE FILE THAT LAUNCH HTML APP POD ON TOP OF KUBERNETES WITH SERVICE PVC & DEPLOYMENT
apiVersion: v1 kind: Service metadata: name: apache-service labels: app: apache spec: ports: - nodePort: 31000 port: 80 targetPort: 80 selector: app: apache type: LoadBalancer --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: apache-pv-claim labels: app: apache spec: accessModes: - ReadWriteOnce resources: requests: storage: 20Gi --- apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 kind: Deployment metadata: name: apache-pod labels: app: apache spec: selector: matchLabels: app: apache strategy: type: Recreate template: metadata: labels: app: apache spec: containers: - image: vimal13/apache-webserver-php name: apache ports: - containerPort: 31000 name: apache volumeMounts: - name: apache-persistent-storage mountPath: /var/www/html volumes: - name: apache-persistent-storage persistentVolumeClaim:
claimName: apache-pv-claim
jen_kube_php.yml
THIS IS THE FILE THAT LAUNCH PHP APP POD ON TOP OF KUBERNETES WITH SERVICE PVC & DEPLOYMENT
apiVersion: v1 kind: Service metadata: name: php-service labels: app: php spec: ports: - nodePort: 32000 port: 80 targetPort: 80 selector: app: php type: LoadBalancer --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: php-pv-claim labels: app: php spec: accessModes: - ReadWriteOnce resources: requests: storage: 20Gi --- apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 kind: Deployment metadata: name: php-pod labels: app: php spec: selector: matchLabels: app: php strategy: type: Recreate template: metadata: labels: app: php spec: containers: - image: vimal13/apache-webserver-php name: php ports: - containerPort: 32000 name: php volumeMounts: - name: php-persistent-storage mountPath: /var/www/html volumes: - name: php-persistent-storage persistentVolumeClaim:
claimName: php-pv-claim
JENKINS JOBS ::
TASK6-SEEDJOB
JOB-1
JOB-2
JOB-3
OUR APPLICATION WILL LOOK LIKE THIS IP OF MINIKUBE:PORT
KUBERNETES GET ALL SCREENSHOT
BUILD PIPELINE SCREENSHOT
I HAVE UPLOADED ALL THESE FILES ON GITHUB ::)
MAY BE YOU WILL LIKE THIS ARTICLE , THANKS FOR READING ????????