DevOps AL- Task 3: Managing and Deploying Webserver/App using Jenkins & Docker on top of Kubernetes
PROBLEM STATEMENT: Perform this 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 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) Job1: Pull the Github repo automatically when some developers push the repo to Github.
5) Job2: a) 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 ).
b) Expose your pod so that the testing team could perform the testing on the pod. c) 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 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
NOTE: This task was done over Windows 10 and REHL 8 VM using the VirtualBox hypervisor. Both the OS must have internet connectivity. Kubernetes preinstalled on Windows and Jenkins and Docker on REHL.
STEPS:
STEP 1: Clone the GitHub repository in which the code is present.
Check if all the files are properly imported using the ls command in the folder where files were to be imported.
STEP 3: Create a post-commit trigger for git to push the code to GitHub as soon as the commit command is done executing.
STEP 2: Launch the RHEL8 node, which will be the host for Docker. Create a folder here for Dockerfile.
This folder must also contain the ca.crt,client.crt,client.key, and config file for Kubernetes. These can be imported from Windows to REHL using WinSCP.
STEP 4: Given below is the code of the Dockerfile that will setup the Jenkins server and also the Kubernetes in our container and also copy all the config and certificate files to the image. The expose command will expose the container to the base OS to the desired port number.
FROM centos Installing Jenkins, Java, Kubectl and some other required packages RUN yum install wget -y RUN wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo RUN rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key RUN yum install java-11-openjdk.x86 64 -y RUN yum install jenkins -y RUN yum install git -y RUN yum install initscripts y RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/ curl s https:// storage.googleapis.com/kubernetes-release/release/stable.txt/bin/linux/amd64/kubectl RUN chmod +x ./kubectl RUN mv /kubectl /usr/local/bin/kubectl RUN export PATH=/usr/local/bin/kubectl:$PATH RUN mkdir /root/.kube #Creating dir and Adding kube dir contents i.e. kubectl config file COPY ca.crt /root/.kube/ COPY client.crt /root/.kube/ COPY client, key /root/.kube/ COPY config /root/.kube/ Exposing Jenkins 808e Port
EXPOSE 8080
This is the config file that will be imported to the image with the certificates.
apiVersion: v1 clusters: - cluster: certificate-authority: ca.crt server: https:// 19216899104 :8443 name: minikube contexts: - context: cluster: minikube user: minikube name: minikube current-context: minikube kind: Config preferences: (} users: - name: minikube user: client-certificate: client.crt client-key: client.key
STEP 3: Check if Docker is running and then build the image using the following command.
docker build -t jenkins:v1 /ws3/
Make sure the image is but with no errors. Now launch a container using the newly formed image and expose it to any free port. Also, mount a volume from your local machine to the container so all the data is saved even if the container collapses.
Check if the container is created successfully using the docker ps command.
STEP 5: Now connect to the Jenkins UI using the IP of VM and the port number you allotted to its Jenkins. Log in using your own account.
Now run the command given below to see if Kubernetes is set up properly.
kubectl get all
STEP 6: In Jenkins create a new job easy "Job1" and add your GitHub URL for SCM. Now chain this job to another job say "Job2" using the build trigger feature. This will automatically run Job2 if Job1 is executed successfully.
?Now create a service.yml file that will help in planning the architecture for our Kubernetes.
Now create a pvc.yml file that creates a persistent volume claim and can be mounted to and container.
STEP 7: Create job2 and write the following code. This code will launch a service if the service isn't launched already. Launched a PVC. It will also launch a pod, based on our code. If our code is .php then a PHP pod will be launched using a php.yml file. If the code is .html then HTTP pose will be launched using httpd.yml file, as provided.
This is the code to be executed in the shell using Jenkins:
if ! kubectl get pvc | grep web-pvc then kubectl create -f ../Devtask3job1/pvc.yml fi if ! kubectl get svc | grep web-svc then kubectl create -f ../Devtask3job1/service.yml fi if ls /root/-jenkins/workspace/Devtaska Job1/*.* | grep html then if ! kubectl get pods | grep httpd | grep Running then kubectl create -f ../Devtask3job1/httpd.yml while kubectl get pods | grep httpd | grep ContainerCreating do sleep 1 done kubectl cp ../Devtask3 job1/.html S(kubectl get pods- 0=jsonpath='{.items[@].metadata.na else kubectl cp ../Devtask3jobi/.html $(kubectl get pods -o-jsonpath='{.items[@].metadata.na fi elif ls /root/.jenkins/workspace/Devtask3 job1/". | grep php then if ! kubectl get pods | grep php | grep Running then kubectl create -f ../eytask3job1/php.yml while kubectl get pods | grep php | grep ContainerCreating do sleep 1 done kubectl cp ../Devtask3job1/".php $(kubectl get pods -o=jsonpath='(.items[0].metadata.nan else kubectl cp ../Devtask3job1/".php $(kubectl get pods -0-jsonpath- '(.items[0].metadata.nan fi fi
Now this job will trigger the next job say "Job3" that will test if the job runs successfully or fails.
STEP 8: Now we create our third job that will check the validity of our code using the status of its build. If the page returns code 200 this means that the page is successfully running else it is said to be taken as failed.
Execute the following code in the shell of Job3.
cp-r ../Devtask3job1/* . code=tanya. code file=$(echo $code) nodeport=$(kubectl get svc-0 jsonpath={.items [*].spec.ports[].nodePorts) status=$(curl -s -w "%{http_code} -0 /dev/null https://192.168.99.104:$nodeport/$code_file) if [[ $status == 200 ]] then exit 0 else exit 1 fi
STEP 9: Now our last job say "Job4" will send an email notification to the user when our application or the webserver is not working.
For this, we need to download the email plugin and email extension plugin which will help us in creating system-generated emails that can be customized. We have to provide the recipient's email address and the body of the email that we want to send.
CONCLUSION 1: Since we provided .html file that is tanya.html file, so on successful built of job1 and job2 httpd pod will be launched with service and PVC setup. On running kubectl get all command we can see out httpd pod deployed with our HTML code and exposed as well. Which can be checked using IP: Port number.
Given below is the pipeline for a successfully done job.
CONCLUSION 2: Here I provided a PHP file as well. The job1,job2,job3 will run successfully if the code is right. Job4 won't be triggered until our task is unsuccessful, returning status 200 a PHP pod will be launched for this.
If we make some error in our code and push it on GitHub it will trigger our job1 and job2 successfully but our job3 will fail. This will be because the shell will receive an error code (ie, something other than 200).
Due to this job4 will be triggered and the user will receive a mail for an unsuccessful job.
This is how our build pipeline will look after an unsuccessful job.
This will be all with the setup for our Deployment and Management of our Webserver using Jenkins and Docker on top of Kubernetes.
Thank You!
Adobe | 3x Adobe Certified
4 年Good work!