DevOps task3 - Configure the Jenkins and deploy the app on Kubernetes
The motive of the task
Perform the DevOps task #2 on top of Kubernetes where we use Kubernetes resources like Pods, ReplicaSet, Deployment, PVC and Service.
- Create container image that has Jenkins installed using dockerfile Or you can use the Jenkins Server on RHEL 7/8
- When we launch this image, it should automatically start Jenkins service in the container
- Create a job chain of Job1, Job2, Job3, and Job4 using build pipeline plugin in Jenkins
- Job1: Pull the GitHub repository automatically when some developer pushes the repo to Github.
- 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 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: Test your app if it is working or not.
- 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
Creation of the dockerfile
FROM centos:latest RUN yum install java -y --nogpgcheck RUN yum install wget -y --nogpgcheck RUN wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo RUN rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key RUN yum install jenkins -y RUN yum install sudo -y --nogpgcheck RUN echo "jenkins ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers RUN yum install git RUN yum install python3 -y CMD java -jar /usr/lib/jenkins/jenkins.war EXPOSE 8080
Above is the dockerfile used for creating the container image that has jenkins already installed. And you just need to run the container using this image.
Note: in case, if you are using the kubernetes through minikube in other VM, so, copy the required files to this image at the time of creating the image. or if you are using the Kubernetes as a service from cloud provider, then, do accordingly. E.g. For the GCP, refer to the link: https://cloud.google.com/sdk/docs/downloads-yum
Configuring the kubectl
For using and controlling the resources in the kubernetes, you can use kubectl. But there's possibilitity that the OS in which you are using the Jenkins and OS in which K8s is running might be different. But for controlling the kubernetes from the jenkins, you need to have the kubectl configured in the the same OS. And for configuring the kubectl, you have a number of ways depending upon where your kubernetes host is configured.
For example, if your kubernetes is configured on minikube and you are running the jenkins in the VM in linux, then, you just need to follow the following steps:
- Install the kubectl program in Linux VM
- Copy the required files to the same OS
- Configure the ~./kube/config file in the kubectl to be able to control the minikube
There can also be the case when you are using the kubernetes from Cloud Providers such as GCP or AWS or may be Azure. For example, if you are using the GKE (Google Kubernetes Engine) for accessing the kubernetes, then, you just need to follow these steps:
- Install the gcloud command
- Install the kubectl
- configure the kubectl to be able to contact the GKE using the command:
gcloud container clusters get-credentials cluster-name
There can also be the case when you might be using the serverless architecture for accessing the kubernetes. In that case also, just configure the same OS with kubectl in which jenkins is running. And you are all done with this part.
So, coming to Jenkins part of creating the jobs
CONFIGURING JOBS IN JENKINS
Job1 - for Pulling the code from the GitHub
Work of the Job1 is to PULL the GitHub repository as soon as the developer push the code into the repository.
Now, for that, we'll be using the GitHub hook trigger for GitSCM Polling in the Jenkins build trigger. But in the GitHub Webhooks, the Payload URL that we need to give does not work for the local system. Hence, we'll be using ngrok to create a tunnel that will give us a random public URL for accessing the IP provided.
Use the following command for starting the ngrok in the host system
./ngrok http 8080
Job2
- 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
- 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
The role of this job is to check whether the app is working correctly or not, if it is not working fine, then, send the email to the developer/admin for the same.
Future Scope
The project can be taken forward to integrate with other technologies such as Kubernetes as a service (not talked much above) provided by various Cloud Providers such as Google Cloud provides GKE (Google Kubernetes Engine), AWS provides EKS (Elastic Kubernetes Service) and Azure Kubernetes Services (AKS). Similarly, ngrok can be replaced when you use the instance of the of the Cloud providers, in that case, public IP can be used and hence, no ngrok needed. Further, the things in the jenkins can also be done by creating the jenkinsfile.
Outstanding piece of work ..