Deploying web-server using Jenkins and Kubernetes

Deploying web-server using Jenkins and Kubernetes

Task Overview:

  • 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 : 

    1. By looking at the code or program file, Jenkins should automatically start the respective language interpreter installed image pod 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 )

  • Job3: Test your app if it is working or not.
  • Job4: if the app is not working, then send an email to the developer with error messages and redeploy the application after code is being edited by the developer.

Tools used:

  • Jenkins
  • Kubernetes
  • Docker

Project Description:

1.Create Jenkins installed Dockerfile:

First I need to create an image which has Jenkins installed also running Kubernetes commands. This is the Dockerfile for this...

FROM centos:latest


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 python3 net-tools -y
RUN echo -e "jenkins ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers


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
COPY .kube /root/.kube


RUN mkdir -p /home/jenkins


EXPOSE 8080


CMD [ "java", "-jar", "/usr/lib/jenkins/jenkins.war" ]


Save this as Dockerfile and run this command...

Only thing you need to change is files of .kube folder.

docker build -t dockerhub_username/img_name:tag

Now your image is created and now push it to in docker hub using command... Before pushing to Docker hub, you need to login to your account- docker login docker push img_name:tag

Now my image is created.

Running Jenkins server on top of Kubernetes with persistent storage.

When you work on Kubernetes, creating a YAML file for this is always better than doing it manually using the command. So here I creating one Dockerfile.

apiVersion: v1
kind: Service
metadata:
  name: jenkins
  labels:
    app: jenkins
spec:
  ports:
    - port: 8080
  selector:
    app: jenkins
  type: NodePort
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jenkins-claim
  labels:
    app: jenkins
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: jenkins
  labels:
    app: jenkins
spec:
  selector:
    matchLabels:
      app: jenkins
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      containers:
      - image: 9897829817/jenkins:v2
        name: jenkins
        ports:
        - containerPort: 8080
          name: jenkins
        volumeMounts:
        - name: jenkins-storage
          mountPath: /root/.jenkins/
      volumes:
      - name: jenkins-storage
        persistentVolumeClaim:
        
          claimName: jenkins-claim

To run this, we using the command...

kubectl apply -f file_name

Now your jenkins_server is running.

No alt text provided for this image

For getting the password, Run kubectl logs pod_name

No alt text provided for this image

2. Jobs in Jenkins:

  • Job1(Copy Github Code):

Whenever the developer pushes any code in Github, this job automatically detects and copy in host OS.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

This job creates a directory mycode and copy the GitHub code in that directory.

Job2(Deploy deployment for website):

If my job1 is successfully built, it triggers job2 and launches the container. Here I have taken the example of webpages so I have used the apache webserver.

No alt text provided for this image
No alt text provided for this image

The YAML code is launching for the website is below...

    apiVersion: v1
	kind: Service
	metadata:
	  name: httpd-server
	  labels:
	    app: httpd
	spec:
	  ports:
	    - port: 80
	      protocol: TCP
	      targetPort: 80
	  selector:
	    app: httpd
	  type: NodePort
	---
	apiVersion: v1
	kind: PersistentVolumeClaim
	metadata:
	  name: httpd-claim
	  labels:
	    app: httpd
	spec:
	  accessModes:
	    - ReadWriteOnce
	  resources:
	    requests:
	      storage: 10Gi
	---
	apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
	kind: Deployment
	metadata:
	  name: httpd
	  labels:
	    app: httpd
	spec:
	  selector:
	    matchLabels:
	      app: httpd
	  strategy:
	    type: Recreate
	  template:
	    metadata:
	      labels:
	        app: httpd
	    spec:
	      containers:
	      - image: 9897829817/http:latest
	        name: httpd
	        ports:
	        - containerPort: 80
	          name: httpd
	        volumeMounts:
	        - name: httpd-storage
	          mountPath: /var/www/html/
	      volumes:
	      - name: httpd-storage
	        persistentVolumeClaim:
	        
              claimName: httpd-claim

Job3(Testing that website is running or not) and Job4(Sent a mail to the developer):

After successfully build, Job2 will trigger Job3 and it checks the website that is working or not. If not working, it triggers job4 and sent a notification to the developer.

No alt text provided for this image
No alt text provided for this image

> Developer get the mails:-

No alt text provided for this image
No alt text provided for this image

FINALLY DONE...

Github Link :- https://github.com/anubhav1626/devops3

Thanks for reading...

Abhishek Saini

HTML || CSS || JS || JAVASCRPIT || PYTHON || QA || API TESTING || MANUAL TESTING || SQL

4 年

Great ????

Kashish Arora

Talent Acquisition Specialist-North America (State/Govt)

4 年

Great ??????

要查看或添加评论,请登录

Anubhav Pahwa的更多文章

社区洞察

其他会员也浏览了