Managing And Deploying Webserver On The Top Of Kubernetes Using Jenkins..................

Managing And Deploying Webserver On The Top Of Kubernetes Using Jenkins..................

No alt text provided for this image


About the Task

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 starts 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 repo to Github.

5. Job2 : 

  1. 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 )

  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 )

6. Job3 : Test your app if it is working or not.

7. Job4 : if app is not working , then send email to developer with error messages and redeploy the application after code is being edited by the developer

JOB1

Creating the docker image in which Jenkins installed using Dockerfile.

Lets start doing the task..

First of all i will create a container image that has jenkins installed.

Here is the code that will create container image..


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

Now i will run this code by using "docker build -t [name] ." command..


No alt text provided for this image

Image has been created successfully..

Now i will launch a container and expose the container to the outer world while launching the container i will also mount one of the directory of the container to my base redhat system because i am going to run some of my job in base redhat system using remote host..


No alt text provided for this image

Now i will setup the jenkins..

I will download some plugin like ssh , GitHub , Email Extension , buildPipeline..


No alt text provided for this image

I will get the password from the above marked file..


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

Now jenkins is ready to use..

Setting up the ssh site..


No alt text provided for this image

Keywords Used in This Dockerfile.

FROM: the image to be used for container

RUN: commands to be executed while building the modified container

We will be installing wget for downloading the files from URL, and before moving further we will set up the yum repository for Jenkins from URLs using wget command. Then we will first install java as it is a prerequisite for the functioning of Jenkins. We will make a Jenkins entry in the sudoers file to give it root powers without the requirement for any password. 

COPY: command will be used to copy files from host to image while building it.

CMD: the cmd used here will keep the Jenkins live till the container is on and will start on container boot.

I have Combined the job1 and job2 , first jenkins download the code from github and then copy the code to host os , I launched jenkins on docker and do the remaining thing in kubernetes on redhat OS
No alt text provided for this image

When jenkins run this job , our pod get launched on kubernetes and automatically exposed to outside world , because we used service in the file , also we attach PVC.

Program Files Used in this Task.

FILENAME => jenins_kube_html.yml

This file launch the code for apache webserver that conatines html code fetches from github.

apiVersion: v1
kind: Service
metadata:
  name: apache-service
  labels:
    app: apache
spec:
  ports:
    - nodePort: 31000
      port: 80
      targetPort: 80
  selector:
    app: apache
  type: LoadBalancer


---


apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: apache-pv-claim
  labels:
    app: apache
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi


---


apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: apache-pod
  labels:
    app: apache
spec:
  selector:
    matchLabels:
      app: apache
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: apache
    spec:
      containers:
      - image: vimal13/apache-webserver-php
        name: apache
        ports:
        - containerPort: 31000
          name: apache
        volumeMounts:
        - name: apache-persistent-storage
          mountPath: /var/www/html
      volumes:
      - name: apache-persistent-storage 
        persistentVolumeClaim:
        

FILENAME => jenins_kube_php.yml

This below file launch for php interpreter

apiVersion: v1
kind: Service
metadata:
  name: php-service
  labels:
    app: php
spec:
  ports:
    - nodePort: 32000
      port: 80
      targetPort: 80
  selector:
    app: php
  type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: php-pv-claim
  labels:
    app: php
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: php-pod
  labels:
    app: php
spec:
  selector:
    matchLabels:
      app: php
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: php
    spec:
      containers:
      - image: vimal13/apache-webserver-php
        name: php
        ports:
        - containerPort: 32000
          name: php
        volumeMounts:
        - name: php-persistent-storage
          mountPath: /var/www/html
      volumes:
      - name: php-persistent-storage 
        persistentVolumeClaim:
          claimName: php-pv-claim

when jenkins run their job you can access your website that is running in kubernetes inside the pod and exposed to outside world , yhan we can access the website.

IP of minikube :port

example => 192.168.99.101:31000 , 192.168.99.101:32000

After accessing website will look like:

No alt text provided for this image

For sending mail i have to do some setting in the jenkins configure system.

No alt text provided for this image

Now i will set the Post build action


No alt text provided for this image

Here is the code that will create deployment..

apiVersion: v1
kind: Service
metadata:
  name: webaj
  labels:
    app: webaj
spec:
  ports:
    - port: 80
      nodePort: 30000
  selector:
    app: webaj
    tier: frontend
  type: NodePort
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: webaj-pv-claim
  labels:
    app: webaj
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: webaj
  labels:
    app: webaj
spec:
  selector:
    matchLabels:
      app: webaj
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: webaj
        tier: frontend
    spec:
      containers:
      - image: httpd
        name: webaj
        
        ports:
        - containerPort: 80
          name: webaj
        volumeMounts:
        - name: webaj-persistent-storage
          mountPath: /usr/local/apache2/htdocs/
      volumes:
      - name: webaj-persistent-storage
        persistentVolumeClaim:
          claimName: webaj-pv-claim

Now lets see the console output of all three job..

No alt text provided for this image

Console output of job 1

No alt text provided for this image

Console output of job 2..

No alt text provided for this image

Console output of job 3..

No alt text provided for this image

If there is any error in application email will send to developer by jenkins and developer will review the application and solve the error , and redeploy the application.

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

No alt text provided for this image





To view or add a comment, sign in

More articles by Pankaj Agarwal

Insights from the community

Others also viewed

Explore topics