CI/CD pipeline using an integration of Jenkins and Kubernetes.

CI/CD pipeline using an integration of Jenkins and Kubernetes.

TASK DESCRIPTION:

Perform this task on top of Kubernetes where we use Kubernetes resources like Pods, ReplicaSet, Deployment, PVC, and Service.

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 start the 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 the 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 the app is not working, then send email to the developer with error messages and redeploy the application after code is being edited by the developer

Technology that I used are:

  1. git and GitHub.
  2. Kubernetes
  3. docker
  4. jenkins

NOW let perform the task

first, we need to create a Docker file that will create an image that will launch the Jenkins service for us and also create a config file for Kubernetes so that Jenkins container can able to contact to Kubernetes cluster.

No alt text provided for this image

this file will create a docker image in which Jenkins is installed and Kubernetes is also configured.

this is a config file for the Kubernetes cluster which is running in my local server.

No alt text provided for this image

NOW let run the docker build cmd for creating an image using Dockerfile

docker build -t mukul30/jenkinskube:v1 . [this cmd will create an image for us and "." is used for referencing the current directory]

if you want to use my created image then you can use this cmd

[docker push mukul30/jenkinskube:tagname]

NOW our image is ready to use, let run a container using our image for starting Jenkins service.

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

after running a docker container with our created image we will get an initial password for login into a Jenkins web app.

now let open our Jenkins startup page. copy the password and paste it in that dial-up box

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

NOW we are ready to go.

Let create jobs in Jenkins:

Job1: Pull the Github repo automatically when some developers push the repo to Github.

now we have created a local git repository where developer manage the code as soon a developer commits the file the code will automatically push to GitHub repository and Jenkins will download that code into there workspace and we have also created a post-commit hook to run the job

No alt text provided for this image

post-commit hooks, where we have mentioned the remotely trigger build so that as soon as the developer commits it will automatically run the job.

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

NOW let see how we have created a JOB 1.its a freestyle job that will help us in pulling the code from git.

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

we have also written some script to copy the code in a local container repository or folder. now, job1 is complete.

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 )

now its time to involve Kubernetes for deploying our application for testing and for production and for that I have created a YAML file for using the resources of Kubernetes like service, PVC, and deployment.

apiVersion: v1
kind: Service
metadata:
  name: htmlexpose
  labels:
    app: htmlwebsite
spec:
  type: NodePort
  ports:
  -  port: 8081
     targetPort: 80
  selector:
    app: htmlwebsite
---


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


---
apiVersion: apps/v1 
kind: Deployment
metadata:
  name: html-deploy
  labels:
    app: htmlwebsite
spec:
  selector:
    matchLabels:
      app: htmlwebsite
  template:
    metadata:
      labels:
        app: htmlwebsite
    spec:
      containers:
      - image: httpd
        name: html-con
        ports:
        - containerPort: 80
          
        volumeMounts:
        - name: html-storage
          mountPath: /usr/local/apache2/htdocs/
      volumes:
      - name: html-storage
        persistentVolumeClaim:
          claimName: html-pv-claim

this is for HTML code if code is written in HTML then Jenkins will use this file for deployment and if the code is written in PHP the Jenkins will use the YAML file for deployment

apiVersion: v1
kind: Service
metadata:
  name: phpexpose
  labels:
    app: phpwebsite
spec:
  type: NodePort
  ports:
  -  port: 8082
     targetPort: 80
  selector:
    app: phpwebsite
---


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


---
apiVersion: apps/v1 
kind: Deployment
metadata:
  name: php-deploy
  labels:
    app: phpwebsite
spec:
  selector:
    matchLabels:
      app: phpwebsite
  template:
    metadata:
      labels:
        app: phpwebsite
    spec:
      containers:
      - image: vimal13/apache-webserver-php
        name: php-con
        ports:
        - containerPort: 80
          
        volumeMounts:
        - name: php-storage
          mountPath: /var/www/html/
      volumes:
      - name: php-storage
        persistentVolumeClaim:
          claimName: php-pv-claim

let create a job 2 in Jenkins and this is also a freestyle job.

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

This script will check whether the code is written in HTML or in PHP and according to that it will create a deployment and expose the pod to the public world.

NOW JOB2 is working fine let create a another job for monitoring the pod or for testing the code.

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

let create this job but for sending an email notification we need to install a plugin in Jenkins which is "EMAIL EXTENSION" to enable the email notification and we have to enable some checkbox for sending an Email.

No alt text provided for this image

after configuring the email notification part let create a job that will check the code is working fine or not.

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 everything is set up and ready to run let see if it is working and check is it doing what it supposed to do let see.

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

and they have also informed about the job whether it is successfully built or not with the attached log file.

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

THE WEBPAGE IS ALSO WORKING FINE. we have not created a monitoring job because Kubernetes has the capability to launch a pod again because we have used deployment and over there we have mentioned our desire of pod.

so that it thanks for taking a look at it.

git hub url : https://github.com/vmukul41/task3devops.git

THANKS !!!!







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

Mukul Kumar的更多文章

社区洞察

其他会员也浏览了