Complete automation with Kubernetes
Task :
Perform second task on top of Kubernetes where we use Kubernetes resources like Pods, ReplicaSet, Deployment, PVC and Service.
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
Description :
First we have to create container image that’s has Jenkins installed using dockerfile Or You can use the Jenkins Server on RHEL 8/7. Here i also installed kubectl so as a admin we can connect to Kubernetes cluster.
So this is Dockerfile to create image if you want to see in detail how to build Dockerfile, i explain that in my first article
Here we have to provide config file so that kubectl we know where is Kubernetes is running. Also we have to give all the certificate file so that it can connect to Kubernetes.
This is how config file look's like :
apiVersion: v1 kind: Config clusters: - cluster: server: https://192.168.99.107:8443 certificate-authority: /root/ca.crt name: lwcluster contexts: - context: cluster: lwcluster user: omprakash users: - name: omprakash user: client-key: /root/client.key client-certificate: /root/client.crt
Now build Dockerfile using build command
After Jenkins launch set the password and username, and install all the required plugins
Second part of this task create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins
We have to deploy the website for this we have to first create code in one of the folder this time we are creating repository directly into Github. Here the code file name is "first.html".
We can also run Jenkins on the public Ip using ngrok
Now let's create a job chain of all the jobs using build pipeline plugin in Jenkins
Job1 : Pull the Github repo automatically when some developers push repo to Github.
This Job is to download the code from Github here we also enabled one trigger of Github this will trigger the Job when webhook from Github come to it.
Here in above image we added one webhook which will trigger Job1. While creating webhook we have to give public Ip which we had created using ngrok.
Once this job triggers, the downloaded code will be store into one folder name "kuberada". After this job2 will be trigger automatically.
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 )
This is yaml file for php code
apiVersion: v1 kind: Service metadata: name: php-service labels: app: php spec: ports: - port: 80 nodePort: 30000 selector: app: main tier: cicd type: NodePort --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: pod-claim labels: app: main spec: accessModes: - ReadWriteOnce resources: requests: storage: 2Gi --- apiVersion: apps/v1 kind: Deployment metadata: name: php-container labels: app: main spec: selector: matchLabels: app: main tier: cicd strategy: type: Recreate template: metadata: labels: app: main tier: cicd spec: containers: - image: omprakash1234/php name: image-name ports: - containerPort: 80 name: port-name volumeMounts: - name: web-vol mountPath: /var/www/html volumes: - name: web-vol persistentVolumeClaim: claimName: pod-claim
This is yaml file for html code
apiVersion: v1 kind: Service metadata: name: html-service labels: app: html spec: ports: - port: 80 nodePort: 30000 selector: app: main tier: cicd type: NodePort --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: pod-claim labels: app: main spec: accessModes: - ReadWriteOnce resources: requests: storage: 2Gi --- apiVersion: apps/v1 kind: Deployment metadata: name: html-container labels: app: main spec: selector: matchLabels: app: main tier: cicd strategy: type: Recreate template: metadata: labels: app: main tier: cicd spec: containers: - image: omprakash1234/html name: image-name ports: - containerPort: 80 name: port-name volumeMounts: - name: web-vol mountPath: /var/www/html volumes: - name: web-vol persistentVolumeClaim: claimName: pod-claim
This yaml file will create pods which will be under Deployment controller and also PVC will be created which we had mounted to the folder which is very important for us. Also in this yaml which exposed the pod at port 30000. So node url will be https://<minikube ip>:30000. But we dont't know which yaml file to run this our job will predict and it will run yaml file.
Both images of php and html environment required.
Here this job should automatically identify the type of code that developer commited. For this i used grep command which will identify the extension of the code file and based on that this job will run respective yaml file. Also we have to copy the code to that pod which has been launched but we don't the name of pod that Deployment launched for us, so to retrieve that name i used this type of commands
output=$(sudo kubectl get pods | grep html-container | cut -d ' ' -f 1)
and to copy use this variable
sudo cp /kuberada/first.html $output:/var/www/html/
After this job3 will be trigger which will check the web server.
Job3 : Test your app if it is working or not.
We already deployed the code on respective container as we have to test the code working or not. So this Job will do it for us. This curl command will check website running or not and give the status code which would be store it into status variable and if value in status is 200 then we will pass this job by giving exit 0 and if value in status is not 200 means website is not working . And if website is not working we have to notify the developer for this there is job4 will be trigger only if website is not working.
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
import smtplib as s op=s.SMTP("smtp.gmail.com",587) op.starttls() op.login("[email protected]","**********") subject="Job failed" boby="Your website is not accessible to client" message="Subject:{}\n\n{}".format(subject,boby) listOfAddress=["[email protected]"] op.sendmail("choudharyomprakash",listOfAddress,message) op.quit()
For this you should have python file which will help to send mail with body of mail. To send mail through python please have a look to above code.
Our website has been properly deployed and running on port 30000.
Build Pipeline
This is how we can automate the complete deployment process using Kubernetes.
Thank you to read this article. Any query or suggestion's are welcomed : )