CI/CD Pipeline Using Kubernetes

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

No alt text provided for this image

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

No alt text provided for this image
  • 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.

No alt text provided for this image
No alt text provided for this image

Console Output

No alt text provided for this 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.
No alt text provided for this image

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
No alt text provided for this image

If Deployment is not present:

No alt text provided for this image

If Deployment is present (Rollout):

No alt text provided for this image
No alt text provided for this image

You can checkout the complete code at my Github Repo:


Atul Yadav

Android Developer | 4+ Years | Kotlin, Java, Jetpack, MVVM, Firebase, REST APIs | Open to Work

4 年

Great work!

Suraj Ghodke ? ??

Lead DevSecOps | Cloud Architect | DevOps and Cloud Enthusiast ? | Kubernetes ?? | Terraform | Docker | Amazon Web Services | Azure Cloud | Python | 1.4K+ @Linkedin ??

4 年

Useful ??

要查看或添加评论,请登录

Gaurav Yadav的更多文章

  • Hosting Wordpress on AWS with maximum Security

    Hosting Wordpress on AWS with maximum Security

    Task Write a Infrastructure as code using terraform, which automatically create a VPC. In that VPC we have to create 2…

    2 条评论
  • Jenkins Automation Using Groovy

    Jenkins Automation Using Groovy

    In this tutorial I tried to Show how to use Groovy script to build a Jenkins pipeline. Groovy is suitable for beginners…

    1 条评论
  • Deploying Prometheus and Grafana over Kubernetes

    Deploying Prometheus and Grafana over Kubernetes

    Task: Creating Docker images for Prometheus & Grafana. Deploying Prometheus & Grafana as pods on top of Kubernetes by…

  • Setting Up WordPress On AWS Using Amazon EKS

    Setting Up WordPress On AWS Using Amazon EKS

    Tasks: Kubernetes Cluster using AWS EKS. Integrate EKS with EC2,EBS,LB,EFS.

  • Deploying Openstack on AWS

    Deploying Openstack on AWS

    Probably everyone with OpenStack hands-on experience would agree that sometimes it could be hard and frustrating to…

    3 条评论
  • Kubernetes deployment and Monitoring using Jenkins

    Kubernetes deployment and Monitoring using Jenkins

    Task: Using Jenkins Server on Rhel, Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in…

    1 条评论
  • Number Plate Detection With Supervise.ly

    Number Plate Detection With Supervise.ly

    What is Supervisely? There are many open-sourced implementations of state of the art neural network architectures. But…

    8 条评论
  • Infrastructure as Code with AWS and Terraform

    Infrastructure as Code with AWS and Terraform

    For This Task, I first created an Amazon Machine Image(AMI) from an instance in which I configured Jenkins and Apache…

    20 条评论
  • Hyperparameter Tuning using MLOps

    Hyperparameter Tuning using MLOps

    The percentage of AI models created but never put into production in large enterprises has been estimated to be as much…

    2 条评论
  • Facial Recognition using Transfer Learning

    Facial Recognition using Transfer Learning

    Transfer learning is a machine learning method where a model developed for a task is reused as the starting point for a…

    1 条评论

社区洞察

其他会员也浏览了