MlOps Task_2
In this task we learn about how to create a docker Jenkins image and solve the perform various operation use Jenkins image.
Description of task………….
Use following steps to perform the task…
step1. Create container image that’s has Jenkins installed using dockerfile
step2. When we launch this image, it should automatically starts Jenkins service in the container.
step3. Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins
step4. Job1 : Pull the GitHub repo automatically when some developers push repo to GitHub.
step5. 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 ).
step6. Job3 : Test your app if it is working or not.
step7. Job4 : if app is not working , then send email to developer with error messages.
step8. Create One extra job job5 for monitor : If container where app is running. fails due to any reason then this job should automatically start the container again.
Perform or solve the task step by step………….
step1. Create container image that’s has Jenkins installed using Dockerfile
In this step we use concept of docker to create container image.
Dockerfile:-
Docker can build images automatically by reading the instructions from a Dockerfile
. A Dockerfile
is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build
users can create an automated build that executes several command-line instructions in succession.
FROM:-
The FROM
instruction initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile
must start with a FROM
instruction. The image can be any valid image – it is especially easy to start by pulling an image from the Public Repositories.
RUN:-
The RUN
instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile
.
CMD:-
The CMD
instruction has three forms:
CMD ["executable","param1","param2"]
(exec form, this is the preferred form)CMD ["param1","param2"]
(as default parameters to ENTRYPOINT)CMD command param1 param2
(shell form)
The main purpose of a CMD
is to provide defaults for an executing container.
Use of above concept create Jenkins image…
FROM centos :-
Use this command or code to launch prebuild operating system Centos.
RUN yum install sudo -y :-
Use this code to give the user root permission.
RUN yum install curl -y :-
Curl is a tool to transfer data from or to a server, using one of the supported protocols (FTP, FTPS, HTTP). The command is designed to work without user interaction.
RUN yum install wget -y :-
Wget is a free software package for retrieving files using HTTP, HTTPS, FTP and FTPS the most widely-used Internet protocols.
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
Use above code to install the repository or download the Jenkins or java.
RUN yum install java-11-openjdk.x86_64 -y :-
Use this code to install the java software. Jenkins work on the java platform.
RUN yum install Jenkins -y :-
This line of code use to download the Jenkins software.
RUN echo -e “jenkins ALL=(ALL) NOPASSWD:ALL”>>/etc/sudoers :-
Give the all permission to sudo command.
CMD /etc/rc.d/init.d/jenkins start :-
This line of code use to start the Jenkins service.
EXPOSE 8080 :-
This line use to expose the Jenkins port number 8080 and run Jenkins dashboard.
CMD java -jar usr/lib/jenkins/jenkins.war :-
Use this line of code run the Jenkins.
Use above all the line of code create docker container image.
Build container image…..
docker build -t jenkins:v1 .
use this line of code create docker image.
Run the Jenkins container :-
Show the docker image.
docker image
step2. When we launch this image, it should automatically starts Jenkins service in the container.
Run the docker container and expose the port number 8081
docker run -it -p 8081:8080 — name jenkin jenkins:v1
This line of code run the jenkins container and give the interactive terminal and also expose on the port number 8081.
Open the web dashboard use of port number 8081:-
All the above command use to step by step install the Jenkins
step3. Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins
Goto Jenkins dashboard click Manage Jenkins->Manage Plugins ->Available ->Build Pipeline Plugin
step4. Job1 : Pull the GitHub repo automatically when some developers push repo to GitHub.
Write a code upload on the github.Fallow below link learn about How to integrate with Git and GitHub?.
https://medium.com/@neeteeshyadav98/integration-with-git-github-cec65fb4ec3f
Use of Jenkins copy the code on github and store the directory.
step5. 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 ).
Deploy the code show the output.
Job3 : Test your app if it is working or not.
In this job when job 2 complete then third job run this job test the webpage working or not use condition to check the code status.
200:- Web page working properly or show the status success.
500:- Web page not working properly or show the status failed.
Use some command to find the status of webpage failed or success.
command1:- curl -i 192.*.*.*:8888/index.html
use above command show the status some information of web page and also show the status 200 show web page run properly.
command2:- curl -w “%{http_code}” 192.*.*.*:8888/index.html
command3:- curl -o /dev/null -w “%{http_code}” 192.*.*.*:8888/index.html
command4:- status=$(curl -o /dev/null -s -w “%{http_code}” 192.*.*.*:8888/index.html)
echo $status
use of above command find the status of the web page and create third job to check the status of web app.
Job4 : if app is not working , then send email to developer with error messages.
In this job create a job to send the mail developer when the web app not working or status failed of the web app.
Above code send the mail developer when web app failed.
above figure show the successfully job run and sent mail developer.
Send the mail developer.
job5:- Create job5 for monitor : If container where app is running. fails due to any reason then this job should automatically start the container again.
This job monitor the container job, if container job stop then run container job and deploy the code.
Thank for reading my blog.