DevOps Task4

DevOps Task4

Create A dynamic Jenkins cluster and perform task-3 using the dynamic Jenkins cluster.

Steps to proceed as:


1. Create container image that’s has Linux and other basic configuration required to run Slave for Jenkins. ( example here we require kubectl to be configured )

2. When we launch the job it should automatically starts job on slave based on the label provided for dynamic approach.

3. Create a job chain of job1 & job2 using build pipeline plugin in Jenkins 

4. Job1 : Pull the Github repo automatically when some developers push repo to Github and perform the following operations as:

  1. Create the new image dynamically for the application and copy the application code into that corresponding docker image

  2. Push that image to the docker hub (Public repository) 

 ( Github code contain the application code and Dockerfile to create a new image )

5. Job2 ( Should be run on the dynamic slave of Jenkins configured with Kubernetes kubectl command): Launch the application on the top of Kubernetes cluster performing following operations:

  1. If launching first time then create a deployment of the pod using the image created in the previous job. Else if deployment already exists then do rollout of the existing pod making zero downtime for the user.

  2. If Application created first time, then Expose the application. Else don’t expose it.


Now let's start building Job1:

Step-1: Here I am using AWS ec2-instance for Node for Job1

  • Goto AWS console > EC2> Launch EC2-instance.
No alt text provided for this image
  • For AMI , Here i am using my own AMI in which all the necessary setup is configured. In this AMI , I have install docker,docker login it also , java version 1.8.0.
  • But in your case > Select AMI(Amazon Linux 2 AMI ) > select it.


No alt text provided for this image
  • Select Instance type t2.micro > Next Configure Instance Details
No alt text provided for this image
  • Select Subnet> ap-south-1a
No alt text provided for this image
  • Next Add Storage.
No alt text provided for this image
  • Next Add Tags > Give name to os.
No alt text provided for this image
  • Next > Configure the Security Group.
No alt text provided for this image
  • Next Review and Launch.
No alt text provided for this image
  • Select your key > Launch Instance.
  • Now Here our Instance is ready
No alt text provided for this image
  • Login in it and install software:
$sudo su -- root
$yum install git -y
#yum remove java-1.7.0-openjdk -y
$yum install java-1.8.0 -y
$yum install docker
  • Configure some of part which is necessary:
$docker login (type username and password)
$docker pull centos.
$docker pull rootritesh/webserver-php


Step-2)Setup of jenkins on docker

  • Now its time to Configure the jenkins .
  • I used Dockerfile for jenkins installation in the docker.
FROM centos
RUN dnf install wget -y
RUN wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins.io/redhat-stable/jenkins.repo
RUN rpm --import http://pkg.jenkins.io/redhat-stable/jenkins.io.key
RUN dnf install java-11-openjdk.x86_64 jenkins -y
RUN dnf install -y openssh-server
RUN dnf install net-tools -y
RUN dnf install git -y
RUN dnf install httpd -y
RUN dnf install which -y
RUN dnf install python36 -y
RUN echo "jenkins ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
RUN echo "java -jar /usr/lib/jenkins/jenkins.war &" >> /root/.bashrc
RUN bash

  • Save this name > Dockerfile.
  • Type in the terminal in the current directory where Dockerfile:
docker build -t rootritesh/devops3 .
  • Explanation of about point.: Here this build command create iso image of docker , and it install and configure all software provided in Dockerfile.
  • Now out Build is complete.
No alt text provided for this image
  • Now run the this docker iso using command:
docker run -it --name jen1 -p 8085:8080 rootritesh/devops3

Explanation : This command run the docker OS, --name for giving name of OS, -p for patting because jenkins works on port 8080 here I expose this using 8085.

  • Now to Copy the IP of your OS > ifconfig
  • Go to your browser and type: IP:8085
No alt text provided for this image
  • Now here copy the password form terminal and paste it in the jenkins for Unlock.
No alt text provided for this image
  • Copied the password and past > Click on Continue.
No alt text provided for this image
  • Here We don't need to install the plugin right Now
  • Click on the X .
No alt text provided for this image
  • After that Click on Start using Jenkins.

Step-3) Setup Plugin for Job1

  • Go to Manage Jenkins>Plugin Manager
  • install these plugin > github, ssh , docker, build pipeline, Publish Over SSH, SSH Agent Plugin.
No alt text provided for this image
No alt text provided for this image
  • Install it > using Install without restart.

Step-4)Configure the Node for AWS for Slave.

  • Goto Manage Jenkins > Manage Node and Cloud > New Node
No alt text provided for this image
No alt text provided for this image
  • Give the name as your choice in my case i am giving aws
  • Configure the Node using following image:
No alt text provided for this image
  • Give the credential > Select kind > SSH Username with private key > give username (ec2-user) > Private key > Add it
No alt text provided for this image
  • here to give private key type if your are using windows type : type KEYNAME.pem, ou in linux type: cat KEYNAME.pem > Copy it and paste it and add it.
  • After this Goto log> of aws.
No alt text provided for this image
  • Now here our aws Node is configured.

Step-5)Configure the job1.

  • Now its time to make job1.
  • Goto jenkins HomePage> New item > give name job1 > create freestyle project, and configure it :
No alt text provided for this image
  • After that save it .

Step-6)Configure the job2.

  • For job2 we need to setup the kubectl using docker.
  • Now copy the ca.cert , client.key , client.crt form .minikube folder .
  • Make on directory keep all the files there
  • Dockerfile.
FROM centos
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 mkdir .kube
COPY ca.crt /root/
COPY client.crt /root/
COPY client.key /root/
COPY config /root/.kube/
RUN dnf install net-tools -y
RUN dnf install openssh-server -y
RUN dnf install which -y
RUN dnf install java-1.8.0 -y
RUN ssh-keygen -A
CMD ["/usr/sbin/sshd", "-D"] && /bin/bash
RUN bash

  • config file for kubectl > just goto in C> user> username> .kube , here u find one file config copy it and change the location of certificates.
  • After that type in that folder :
docker build -t rootritesh/kube
No alt text provided for this image
  • After that We need to configure the Dynamic Slave of this rootritesh/kube iso.
  • U need to add the tcp connection in our docker.service file
  • Goto : cd /usr/lib/systemd/system , here U find one file name docker.service > open it using editor > and add tcp://0.0.0.0:4243.
No alt text provided for this image


  • Now Goto jenkins HomePage > Manage jenkins> Manage Node and Cloud > Configure Cloud > give the name docker
No alt text provided for this image
  • Configure it > gave name > IP_of_your_OS:port (test it also, by clicking on Test Connection) > give the name of docker iso > configure the SSH:
No alt text provided for this image
  • Save it .
  • Now its time to configure the job2 .
  • Goto jenkins HomePage> New item > give name job2 > create freestyle project, and configure it :
No alt text provided for this image


  • Save it .

Step-7)Install the Build pipeline plugin > install it.

  • Now go to jenkins homepage > Click on the + icon > Give Name. > Tick the build pipeline > Click ok.
No alt text provided for this image
  • Configure it using following image:
No alt text provided for this image
  • Save it .

Final Step : Here U see such a powerfull setup.

  • Click on the Run icon.
No alt text provided for this image


Output of job1:

No alt text provided for this image
  • Here it login into aws ec2, copy the github code, build the docker image, and upload it .
  • Now goto Docker hub for the output if image is uploaded or not.
No alt text provided for this image
  • Here rootritesh/webserver-php:v9 has come which is pushed by job1 aws instance.

Explanation of job1: Now here Why i use AWS for job1 because We all know aws use own internet connectivity for download/ upload which is very fast , in this job1 we need to download the git code, make the docker iso, and push it to the dockerhub. You Now Guys, here uploading speed is so fast, within 3-4 seconds your docker iso pushed into dockerhub


Output of job2

No alt text provided for this image
  • Here it first launch the pod and expose it , if you run this job2 again then it will check first status code if it is 200 then it will print message POD is running Good, otherwise it will Rollout.

Expanation of job2: Here I have setup this job2 on Dynamic Slave using docker iso which is created from the dockerfile. In this job2 let me explain the script i have created, first it check the deployment is running or not, if yes , it check the status code, if its is 200 it will do nothing just print a message " Pod Status Good" , otherwise it will Rollout, and if there is not deployments present it create it , scale it and expose it.


Final output of Website.

  • Copy the minikube ip:port number given by the service.
No alt text provided for this image


Final Explanation: Here both the jobs are in Build pipeline , it is very powerful setup job1 is running on aws using Node , job2 is running on docker image using Dynamic Slave, Here I tried as much i can enhances this Task4


Github Link = https://github.com/rootritesh/DevopsTask4.git


Shyam Sulbhewar

Software Engineer At BUSINESSNEXT

5y

great work bro

Chirag Deepak Ratvekar

Mainframe Developer at Wipro | Microsoft Certified Azure Administrator Associate | Cloud & Data Enthusiast.

5y

That's great 👍

To view or add a comment, sign in

Others also viewed

Explore topics