Integrating Kubernetes with Jenkins , Github and Docker

Integrating Kubernetes with Jenkins , Github and Docker

Hi friends , This is Milind Verma and today I am going to show you how we can integrate multiple technologies to perform a very great use case in DEVOPS . In this fast growing world learning only single technology doesn't help much. You need to focus on multiple technologies so that you can integrate them to create a great product.
No alt text provided for this image

Our Task is:

  • Create a container image that has Jenkins installed using the Dockerfile or you can use the Jenkins Server on RHEL8
  • 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 the build pipeline plugin in Jenkins.
  • Job1 : Pull the github repo automatically when some developer push the repo to github.
  • Job2 :
  1. By looking at the code 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 the PHP installed.)
  2. Expose your pod so that the 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 : If the app is not working , then send email to developer with the error messages and redeploy the application after the code is being edited by the developer.

TECHNOLOGIES USED:

  1. Kubernetes
  2. Jenkins
  3. Git
  4. Docker

SO NOW LETS START:

First we have to install the minikube VM to make our Kubernetes work.

Below command will download the minikube VM and installs it automatically.

minikube start --vm-driver=virtualbox

After the download and installation of the minikube , we need to start the minikube VM

minikube start

Now we have to copy 4 files to the RHEL 8 in order to install the kubernetes on it , so that we use it for creating our container.

C:\Users\milind verma\.minikube\profiles\minikube>client.crt

C:\Users\milind verma\.minikube\profiles\minikube>client.key

C:\Users\milind verma\.minikube\profiles\minikube>cd ..

C:\Users\milind verma\.minikube\profiles>cd ..

C:\Users\milind verma\.minikube>ca.crt


C:\Users\milind verma>cd .kube/config

Creating the Container image:

This image have the Jenkins and Kubernetes configured.

FROM centos
RUN yum install wget -y
RUN yum install ncurses -y
RUN yum install sudo -y
RUN yum install git -y
RUN yum install net-tools -y
RUN sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
RUN sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
RUN yum install java -y
RUN yum install jenkins -y
RUN yum install initscripts -y
CMD service jenkins start 
RUN echo "jenkins ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers


RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.18.0/bin/linux/amd64/kubectl
RUN chmod +x ./kubectl
RUN sudo mv ./kubectl /usr/local/bin/kubectl
RUN mkdir /root/.kube
COPY /root/.kube/config /root/.kube/
COPY /root/ca.crt /root/
COPY /root/client.crt /root/
COPY /root/client.key /root/


Now we build the Dockerfile.

docker build -t jenkins:v1 .

This Dockerfile will automatically start the jenkins service and k8s also, as we have given the command to automatically start the jenkins service once the jenkins is installed in the docker container.

Now we are going to launch the container for the jenkins

For this type:

docker run -dit -v /root/kube_code:/root/kube_code -p 8082:8080 --name os2 jenkins:v1

This will launch the container which has the jenkins service automatically start and your Kubernetes is also configured in the container and also copy the code for the pods that we are going to launch in the container.

NOW WE WILL CREATE THE JOBS!!

Before creating the jobs we need to install some plugins for the git and the build pipeline .

No alt text provided for this image

JOB1:

In this job we are pulling the github repository with the help of jenkins.

For this first add a webhook to the github ,so that it will automatically pull the repository whenever the deeveloper pushes the new code to the github.

No alt text provided for this image

so here we are giving the URL of the github repository.

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


JOB2:

Now we create the second job. As soon as the Job1 downloads the code, this job will check the language of the code. If it is an html code, it will start the Deployment Service for HTML containers. If it is a PHP code, it will start the Deployment Service for PHP container.

HTTPD yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd-dep


spec:
  replicas: 1
  selector:
    matchLabels:
      - env: production
      - region: IN


  template:
    metadata:
      name: httpd-dep
      labels:
        env: production
        region: IN


    spec:
      containers:
      - name: httpd-dep  
      
        image: vimal13/apache-webserver-php
        
        volumeMounts:
          - name: http-vol1
            mountPath: /var/www/html
      volumes:
      - name: http-vol1
        persistentVolumeClaim:
          claimName: http-pvc1

            

HTTPD pvc:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name:http-pvc1
  labels:
    name: httppvc1
spec:
  accessModes:
   - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

Here is the PHP container code.

PHP yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-dep

spec:
  replicas: 1
  selector:
    matchLabels:
      env: production

  template:
    metadata:
      name: php-dep
      labels:
        env: production

    spec:
      containers:
      - name: php-dep
      
        image: vimal13/apache-webserver-php     
        volumeMounts:
        - name: php-vol1
          mountPath: /var/www/html
      volumes:
      - name: php-vol1
        persistentVolumeClaim:
          claimName: php-pvc1
                 

PHP pvc:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: php-pvc1
  labels:
    name: phppvc1
spec:
  accessModes:
   - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

now we make the job in the jenkins

No alt text provided for this image

Now write the code for the pods

No alt text provided for this image

JOB3:

Now we create the final job that will notify that our job is failed or not .If failed then it will notify by sending the mail.

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

Hence finally this job will send the mail if our JOB2 is unsuccessful .

the container are managed by the Kubernetes .

FINALLY THE BUILD PIPELINE

No alt text provided for this image



























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

Milind Verma的更多文章

  • Red Hat OpenShift Deployment on AWS via ROSA Service

    Red Hat OpenShift Deployment on AWS via ROSA Service

    Hello everyone, welcome to my article. In this article, I will cover: what's Red Hat OpenShift Service on AWS (ROSA)?…

    10 条评论
  • JavaScript - "An Integral Part of Modern Apps"

    JavaScript - "An Integral Part of Modern Apps"

    Let’s see what’s so special about JavaScript, what we can achieve with it, and what other technologies play well with…

  • Cyber Crimes & Confusion Matrix

    Cyber Crimes & Confusion Matrix

    Cybercrime is vastly growing in the world of tech today. Criminals of the World Wide Web exploit internet users’…

  • AWS S3 Multipart Uploading

    AWS S3 Multipart Uploading

    Hello Guys, I am Milind Verma and in this article I am going to show how you can perform multipart upload in the S3…

  • ARTH TASK-6

    ARTH TASK-6

    OUR TASK: Create High Availability Architecture with AWS CLI The architecture includes- Webserver configured on EC2…

  • How Google uses Machine Learning

    How Google uses Machine Learning

    Google is the master of all. It takes advantage of machine learning algorithms and provides customers with a valuable…

  • Kubernetes CASE STUDY: Pearson

    Kubernetes CASE STUDY: Pearson

    Challenge A global education company serving 75 million learners, Pearson set a goal to more than double that number…

  • Launching webserver and python using Docker Conatiner

    Launching webserver and python using Docker Conatiner

    Our task: launching the HTTPD server over the docker container setting up the python interpreter and running python…

    2 条评论
  • Integrating LVM with Hadoop and providing Elasticity to DataNode Storage

    Integrating LVM with Hadoop and providing Elasticity to DataNode Storage

    In this article I am going to show you , how can we integrate the Hadoop with the LVM. First we will launch 2 EC2…

    1 条评论
  • Netflix Case Study on top of AWS

    Netflix Case Study on top of AWS

    NETFLIX Netflix is an American media services provider and production company headquartered in Los Gatos, California…

社区洞察

其他会员也浏览了