Integration of GitHub, Jenkins, k8s

To see stunning results……

This article is extended part of my previous article you can see here for your reference , because the same task , we are performing here but this time with kubernetes.

In this article we will perform the following 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 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.

Let’s Begin …

  • Here I am not launching Jenkins using docker image(means Jenkins is not running inside docker) , I am using RHEL8 jenkins server, If you want to know how to create Jenkins image using Dockerfile , for launching Jenkins inside docker ,have a look in my previous article.
  • Here we have to create a job-chain of Four jobs, but I will created only Three ,because 3 jobs are enough to complete this task.

Git & GitHub — JOB-1

  • The whole pipeline will starts from developer’s end , developer write the code & push it into GitHub for management of source code ,version-control & of course for Jenkins jobs.
  • Here we will use the concept of post-commit hooks for automatically pushing & to Remote trigger Jenkins job , for that we have to write URL of Jenkins job (which we have to trigger) with token & credentials in post-commit file so that as soon as developer commit in local repository it will automatically push & trigger Jenkins’ s job.
created a post-commit file for auto pushing in GitHub & for automatically trigger Jenkins job, notice here I have given the URL of Jenkins job with token & my Jenkins server credentials using curl command
commit in local Git repository & trigger Jenkins job-1
  • After commit in local Git repository & triggering Job-1 in Jenkins, Jenkins automatically pull the application code from GitHub.
  • Have a look in Jenkins JOB-1 ..
Given GitHub URL & also token which we used in Post-Commit file in Git for Remote Trigger.

Create Deployment , PV ,PVC for launching application Inside k8s Pods using YAML file — JOB-2 …

First Understand all the major concepts !!

Deployment : —

  • A Deployment provides declarative updates for Pods and ReplicaSets. You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate.
  • Deployment Internally created -> ReplicaSets -> It create Pods -> Inside Pods we have containers -> In containers we have application .
  • Many things like replicas , roll-out , monitoring & if pod fails then automatically create a new pod to meet our desired.. we can get using Deployment , see here for more!!

PV & PVC : —

  • A PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes.
  • A PersistentVolumeClaim (PVC) is a request for storage by a user. It is similar to a Pod. Pods consume node resources and PVCs consume PV resources.

Note : we can create Deployment by 2 ways: either command or file. Here I am creating it using YAML file, so that we are also able to create PV & PVC for Permanent Storage.

Add build trigger option , here select the job-1 which will trigger job-2 after successful built
.yml file for crating deployment & PVC for applications file having .html extension
PV (for Html application )
File for crating deployment & PVC for applications file having .php extension
For PV (for PHP application )
Add these commands in Excute Shell option in jenkins job which will create deployment.
  • The following shell scripts first check the extension of files & after checking extension it will create the deployment for these files having corresponding interpreters.
  • In above code firstly It will copy all the files in a pre-created folder(which is the mount point for the document directory for all the applications ) in minikube vm using scp.

Note : Here I am taking the examples of having .html & .php extensions only.

JOB-2 Output
all services, pv,pvc ,deployment is created in k8s

Testing of Code & Email Sending — JOB-3…

  • The JOB-3 will trigger by JOB-2, It will test the code (Generally It will test by testing team using many efficient testing tools , but here I am just using simple commands for testing that web-application is running fine or not).
  • If There is error in code then sent Email to developer.
For testing , check the status code in case of web-application ,which is generally 200 if it is running fine.
for email-sending add post-build actions
  • For sending email using Jenkins we have to configure email notification in Jenkins, for that take reference from this blog & you may be face the same issue which I faced related to protocol, to solve that take reference from here.
  • If this job will be fail then it wail send email to developer .
email sending to developer for editing the code if there is any bug or error in code.
  • After see this email , developer debug the code , again commit it , then again It will be pushed to GitHub & again there will be redeployment of application inside k8s pods , for that we can also use different deployment strategies in k8s like rolling-updates for roll-out , roll-in concept.
complete build pipeline for these 3 JOBS in job-chaining concept
The final web-application running inside k8s pods(html file)
This php file running inside k8s pods

hurray !! done …

Here I would like to show my gratitude to vimal Daga sir for giving this task. This is the task-3 which was given by sir to all of the trainees in DevOps Assembly Line Training , & Here I done my task..