CI/CD Pipeline Using Kubernetes
Task Description
1. Create container image that’s has Linux and other basic configuration required to run Slave for Jenkins. ( example here we require kubectl to be configured )
2. When we launch the job it should automatically starts job on slave based on the label provided for dynamic approach.
3. Create a job chain of job1 & job2 using build pipeline plugin in Jenkins
Job1 : Pull the Github repo automatically when some developers push repo to Github and perform the following operations as:
- Create the new image dynamically for the application and copy the application code into that corresponding docker image
- Push that image to the docker hub (Public repository) ( Github code contain the application code and Dockerfile to create a new image )
Job2 Launch the application on the top of Kubernetes cluster performing following operations:
- If launching first time then create a deployment of the pod using the image created in the previous job. Else if deployment already exists then do rollout of the existing pod making zero downtime for the user.
- If Application created first time, then Expose the application. Else don’t expose it.
Let's Start
As we have to control Docker using Jenkins, We will have to set-up Docker cloud on Jenkins. For this we will need Docker plugin.
Moreover We need to allow docker daemon to be accessed remotely. For this, we need to edit /usr/lib/systemd/system/docker.service.
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:1234 #restart daemon and docker systemctl daemon-reload systemctl restart docker.service
Here 0.0.0.0:1234 denotes that any IP can be given to the host but the port must be 1234. Then, we will have to setup the Docker-cloud on Jenkins:
Manage Jenkins > Manage Nodes and Clouds > Configure Clouds > Add A New Cloud — Docker > Set the required parameters > Test connection
Job 1
- Pulling the Github repo automatically when some developers push repo to Github
For automation I used post-commit git hook. This hook ensures that the code gets pushed to Github as soon as the user commits, and also runs Job1 Using build triggers of Jenkins.
#!/bin/bash git push curl --user "admin:root" https://192.168.99.101:8080/job/Git%20Clone/build?token=job1
- Creating the new image dynamically for the application and copying the application code into that corresponding docker image and Push that image to the docker hub (Public repository)
I included the Dockerfile in the Git repo and used Docker plugin to build and push the Docker image automatically.
Console Output
Job2
- Launch the application on the top of Kubernetes cluster performing following operations:
- If launching first time then create a deployment of the pod using the image created in the previous job. Else if deployment already exists then do rollout of the existing pod making zero downtime for the user.
- If Application created first time, then Expose the application. Else don’t expose it.
I used yml file for deployment and used 3 replicas of pods for easy rollout.
apiVersion: apps/v1 kind: Deployment metadata: name: web-deployment spec: replicas: 3 selector: matchLabels: env: production template: metadata: name: web-pod labels: env: production spec: containers: - name: httpd image: 269mehta/webserver-httpd:latest
If Deployment is not present:
If Deployment is present (Rollout):
You can checkout the complete code at my Github Repo:
Android Developer | 4+ Years | Kotlin, Java, Jetpack, MVVM, Firebase, REST APIs | Open to Work
4 年Great work!
Lead DevSecOps | Cloud Architect | DevOps and Cloud Enthusiast ? | Kubernetes ?? | Terraform | Docker | Amazon Web Services | Azure Cloud | Python | 1.4K+ @Linkedin ??
4 年Useful ??