DevOps AL- Task 2: Managing and Deploying Webserver/App using Jenkins & Docker
Problem Statement :
- Create a container image that’s has Jenkins installed using Dockerfile. When we launch this image, it should automatically start the Jenkins service in the container.
- Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins
- Job1: Pull the Github repo automatically when some developers push the repo to Github.
- Job2: By looking at the code or program file, Jenkins should automatically start the respective language interpreter install image container to deploy code ( eg. If code is of PHP, then Jenkins should start the container that has PHP already installed ).
- Job3: Test your webpage whether it is working or not, If its working fine else notifies the developer.
- Job4: Create One extra job not belonging to the pipeline to monitor: If the container where the webpage is running fails due to any reason then this job should automatically start the container again.
Steps to achievement:
Step 1: We create a Dockerfile on the centos:latest version as mentioned below.
From centos:latest run yum install wget -y run yum install net-tools -y run wget - /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 upgrade -y run yum install java -y run yum install jenkins -y run yum install git - run echo "jenkins ALL=(ALL) NOPASSWD:ALL">>/etc/sudoers run yum install python3 -y copy sendmail.py/ cmd java -jar /usr/lib/jenkins/jenkins.war
Installed java and git in this image as java is a prerequisite for running Jenkins and Git we need to clone the repo.
FROM is used for the image to be used.
RUN is used for executing command while building the new image so that features will be pre-installed.
CMD here is used to start Jenkins and keep the container live even after executing the command.
The below-mentioned command is used to run the Dockerfile made above.
docker build -t jenkins:v1
You can give name and version according to you instead of Jenkins:v1
To see if the image was built successfully run the following command and search for the name and version.
docker images
Now to run the container use this command.
docker run -it --privileged -p 9900:8080 -v /:/host --name jenkins jenkins:v1
PS: "--privileged" is youed to gain special access for our later use when using the container.
Step 2: Creating jobs in Jenkins.
We will have 4 jobs for this task as demonstrated below.
Job1: This job will pull our code from GitHub automatically when a developer pushes some change in it.
Now in the Execute shell section give the following set of commands.
rm -rf /host/task mkdir /host/task cp -rvf . /host/task
This will copy the code from GitHub to your task folder.
Job 2: By looking at the code or program file, Jenkins should automatically start the respective language interpreter install image container to deploy code ( ie: If code is of PHP, then Jenkins should start the container that has PHP already installed ).
This will only automatically start this job if the previous job is triggered and executed successfully.
Now in execute shell section write the following code.
chroot /host /bin/bash <<"EOT" if sudo cat /task/index.html | grep html then if docker ps grep task2 then docker rm -f task2 docker run -dit -p 8081:80 - v /task:/usr/local/apache2/htdocs --name task2 httpd:latest docker run -dit -p 8081:80 -v /task:/usr/local/apache2/htdocs --name task2 httpd:latest fi else echo "not html" fi
PS: This is an example using HTML but you can add PHP or any interpreter you want using if-else statements.
You can either set a post-build action now or do it later for job 4. It will only be triggered if this job fails to be successful. Skip to job 4 to know more.
Job 3: This job will test if the webpage is working or not. If for some reason the page fails to load it notifies the developer.
This will now start automatically after the completion of job 2.
Now in the execute shell section write the following code.
chroot /host /bin/bash <<"EOT" export status=$(curl -o /dev/null -sw "%(http_code)" -o /dev/null IP:Port-munber) if [ $status -eq 200 ] then exit 0 else python3 /root/Desktop/task2/sendmail.py exit 1 fi
this is for testing our webpage if it's working properly then exit 0, else will send a mail to develop and the job fails with exit 1.
Job 4: This job is to be executed if the webpage deployment fails due to any reason and should start the container again.
This will check every minute if the container is running or not.
Now in the Execute Shell column write the following set of commands.
chroot /host/bin/bash <<"EOT" if docker ps | grep task2 then exit 0 else docker run -dit -p 8081:80 -v /task/usr/local/apache2/htdocs --name task2 httpd:latest exit 1 fi
"exit 0" and "exit 1" are the outputs that will be shown as in both the cases the console will have a positive result.
Post-build will start the job 3 again for successful deployment.
Thank You!