INTEGRATION OF GIT,GITHUB,JENKINS AND KUBERNETES
Udit Agarwal
Software Engineer | Python | GCP Cloud | Devops | Kubernetes | Grafana | AWS cloud | JAVA enthusiast | web developer | Docker | Rhel 8
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 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\
Solution :
First of all we have to create the docker image which jenkins as well as kubectl configured.
Dockerfile
Dockerfile for html code
Now Job1:-
- In this job we pull data from our github repository
- To create a job click on new item give anyname select freestyle project
JOB2:
In this job we check the code file extension and according to the code type Jenkins should automatically start the respective language interpreter installed image container to deploy code on top of Kubernetes. Expose your pod so that testing team could perform the testing on the pod. Also make the data to remain persistent ( If server collects some data like apache logs).
Deployment for code by using yaml file:
apiVersion: apps/v1 kind: Deployment metadata: name: myweb-deploy labels: app: webapp spec: selector: matchLabels: app: webapp env: production template: metadata: name: myweb-pod labels: env: production app: webapp spec: containers: - name: myweb-con image: hemant123/myweb:v1 ports: - containerPort: 80 volumeMounts: - mountPath: "/usr/local/apache2/htdocs" name: jenkins-pvc volumes: - name: jenkins-pvc persistentVolumeClaim: claimName: pv-claim-jenkins
Exposing the service by using yaml file:
apiVersion: v1 kind: Service metadata: name: myweblb spec: selector: app: webapp type: NodePort ports: - port: 80 targetPort: 80 nodePort: 31000
Create PVC for storing the logs of apache web server by yaml file:
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: pv-claim-jenkins labels: app: webapp spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi
Now Job3:-
- In this job we check our server is working or not
- In buiild trigger select build after other project build and also if build stable
JOB4:
If the code is not running properly then this job send the mail to the developer and remove all the pods , services as well as persistent volume.
#code:- sudo python3 /root/dvt3/sendmail.py
python code for sending mail :-
import smtplib # module to send mail sender ="[email protected]" #gives your or sender mail receiver="[email protected]" #gives receiver or authority mail password= "senderpass" #gives password of sender mail message= "some error is found..." #message you want to send server=smtplib.SMTP("smtp.gmail.com",587) #use to congiure mail server server.starttls() # to start server server.login(sender,password) #login as sender server.sendmail(sender,receiver,message) #send mail from sender to receiver print("message sent successfully")
then .....
OutPut of Job4:-
Final Webserver OutPut Is:-
because of some reasons my php page does not working but html interpreter is installed and index.html file gives proper output and shows proper page.
And Final Build Pipeline is :-
Thanks for reading,,,,,,,,,,,,,................. Any suggestions then always welcome.....
thank you Mr.vimal daga sir for giving me this knowledge..........