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.
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 :
- 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.)
- Expose your pod so that the testing team could perform the testing on the pod.
- 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:
- Kubernetes
- Jenkins
- Git
- 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 .
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.
so here we are giving the URL of the github repository.
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
Now write the code for the pods
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.
Hence finally this job will send the mail if our JOB2 is unsuccessful .
the container are managed by the Kubernetes .