?? DevOps Task-3??
Tushar Dighe
DevOps/Cloud Engineer | Kubernetes | GCP | AWS | Azure | Wiz | Databricks| Cycode | Security
Integrate Jenkins with Kubernetes
This will be a quick task NOT a detailed one we will just have to perform previous TASK i.e task2. In last task we used DOCKER in this we have to use Kubernates.
Link To Previous Task - 2 : https://www.dhirubhai.net/pulse/devops-task-2-tushar-dighe
Now we get to our this task....
Pre-requisites:
- Install minikube VM and Kubectl in the base OS (Windows) in order to use the Kubernetes Cluster
Objective :
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
?Solution :
We are gonna use our Jenkins on RHEL8 for this job to work we need some configuration of Kubernates we need the kubectl command to be configured in the configuration of kubectl command.
- Start minikube server from the Windows Command Prompt.
minikube start
Copy these three files from windows to workstation for configuration.
C:\Users\Admin\.minikube\ca.crt C:\Users\Admin\.minikube\profiles\minikube\client.crt C:\Users\Admin\.minikube\profiles\minikube\client.key
Now we need a config file that will be used by kubectl to connect to our K8S cluster we will make a folder from name ".kube" and we will paste this file in that folder.
Create a Jenkins image with kubectl installed on it.
Build Command :
docker build -t tushardighe/jenkube:v1 .
Now our kubectl command is configured and we can use it in RHEL8 to access our cluster.
After successfully created this image push it on Docker hub. But the first login through command line in docker hub
docker login docker push tushardighe/jenkube:v1
So your docker image is upload the dockerhub.com so you can use this image anywhere...using docker
Then you to use of the kubernetes so the create the PVC, Deployements and services of the kubernetes so write the .yml code to create this resources.
PVC (Persistent Volume Claim)
Create a YML file and write that code
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: jenkins-data labels: app: jenkins spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi
After that run
kubectl create -f file_name.yml
So it creates a PVC. After creating that you need to mount that PVC and also download that image. Better management, we create a Deployment.
apiVersion: apps/v1 kind: Deployment metadata: name: myweb labels: app: jenkins spec: selector: matchLabels: app: jenkins tier: frontend strategy: type: Recreate template: metadata: labels: app: jenkins tier: frontend spec: containers: - image: tushardighe/jenkube:v1 name: jenkins volumeMounts: - name: jenkins-persistent-storage mountPath: /root/.jenkins volumes: - name: jenkins-persistent-storage persistentVolumeClaim: claimName: jenkins-data
After that, we need to expose the Jenkins port 8080. For that
kubectl expose deployment myweb --type=NodePort --port=8080
Finally, you get Jenkins services start. If you want to see all things
kubectl get all
you get output look like this
After getting minikube IP and use that port 32122. You see your Jenkins service starts at that port in minikube IP. After setup Jenkins and download important plugins like Git, Github then start the things.
Now our task begins as we have done the similar task in task2 so we will not discuss it so deeply. So now we will just start jenkins and start building jobs.
Job1 will have simplest work to get code from the git hub it will download the code and store in a folder.
Now job2 we will launch pods we will launch them and we have to launch pod in respective pod so that they have perfect interpreter.
That's all we can deploy our code like this in this the reader.py file will give output as html then we deploy a pod from httpd image and if the reader.py gives php then we will use a image which has php installed.I have created 3 replicas you can create according to your requirement.
Job3 will have a remote which will be given to QAT team so they can test our code and redeploy of there is any problem i have written some simple code just to make sure pod is running.
we can write one more if statement there for php-pod but in my case i had html code so i have written that only.
Thanks for reading............