CI/CD   Kubernetes

CI/CD Kubernetes

This article is a simple integration of GitHub, Jenkins, Kubernetes and Docker to automate the process of deploying a website by creating custom Docker images using Dockerfile.


Problem Statement.....

Perform second 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.


Now let's start from scratch


1.Creating Dockerfile and building it.

No alt text provided for this image

The docker file includes some software installation and to run some commands.

We have copy some certificates from windows like ca.crt, client.crt, client.key from windows which is to connect to minikube from the docker container.

We also have to create a file called config and copy that to folder /root/.kube/

apiVersion: v1
kind: Config


clusters:
- cluster:
    server: https://192.168.99.105:8443
    certificate-authority: /root/ca.crt
  name: api


contexts:
- context:
    cluster: api
    user: yash


users:
- name: yash
  user:
    client-key: /root/client.key
    client-certificate: /root/client.crt

This file accesses the key and certificates to connect to minikube.

CMD ["java", "-jar", "/usr/lib/jenkins/jenkins.war"]


The above command starts Jenkins service when you launch the container.

docker build -t jen-kube . 


Above command is used to build the docker image using the above created Dockerfile.

Now run the docker container and expose it

docker run -dit -p 8081:8080 -v /:/host/repo --name kube_jen jen-kube

Initially, I have created a the folder "/repo" in the host and attached it to the container.

After launching the container you can access jenkins dashboard by using the web address.

IPAddress:Port


This is the Jenkins dashboard.

No alt text provided for this image

2.Creating Job1.

We have to create a git repository and add required files.

Now in Jenkins we have configure the git repository.

No alt text provided for this image

In Repository URL give your git repository url.

For build trigger we can use "GitHub hook trigger for GITScm polling"

No alt text provided for this image

Before this we have to add webhook in our repository

First we have to use ngrok for the site to be accessible anywhere in the world.

Run the command

./ngrok http 8081
No alt text provided for this image

add the forwarding address to the payload url.

No alt text provided for this image

Now we have to copy the files to container from github.

No alt text provided for this image

Job1 is finished.

3.Creating Job2.

This job has to run after Job1.

No alt text provided for this image

Now we have to deploy our file in kubernetes pod using respective image.

No alt text provided for this image


The kubernetes files are available in the GitHub, link below.

No alt text provided for this image

This finishes Job2.

4.Creating Job3.

This runs after Job2.

Here we have to test our site

No alt text provided for this image

If the status code is 200 then it means that the site was opened successfully.

5.Email configuration.

First we have to install Email Extention Plugin.

No alt text provided for this image

We have to search it in Available section if not installed.

Next we have to go to Manage Jenkins >> Configure System

Scroll to last, and configure in E-mail notification

No alt text provided for this image

In Test e-mail recipient put the email address to which you want the email to be delivered.

We can get email of Job3 logs. For that we have to add email notification in post-build-actions.

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

6.Creating Job4.

This Job is created for monitoring of the container and to launch another if the existing fails.

This runs after Job3.

No alt text provided for this image

Here I have scheduled for every 1 minute.

No alt text provided for this image

7.Build Pipeline

Install build pipeline plugin

No alt text provided for this image

give a name in View name.

No alt text provided for this image

click on apply.

No alt text provided for this image

Finally we get the pipeline view of our integration.

Web page.

No alt text provided for this image


Thank You

To view or add a comment, sign in

More articles by Yashwanth M B

  • Hosting Website with AWS CloudFront

    This article is about creating an high availability infrastructure using AWS CLI The AWS Command Line Interface (CLI)…

    1 Comment
  • HADOOP HDFS

    This article is about setting up hadoop cluster and contributing specific amount of storage from data node HADOOP…

    2 Comments
  • AIRBNB and AI

    This article is about how the billion dollar company Airbnb uses AI and machine Learning to make world a better place…

  • AWS CLI

    This article is about AWS CLI for using different aws services. The AWS Command Line Interface (CLI) is a unified tool…

  • AIRBNB and AWS

    This article is about 3 roommates who who built a billion dollar company after renting their home. Airbnb is an…

  • BIG DATA

    This article is about the growing problem of data in the world that is Big Data What is Data ? Computer data is…

  • Configuring LoadBalancer Using Ansible

    This article is about creating a loadbalancer using ec2 instances from Ansible Have you ever wondered that, even…

  • Dynamic Inventory In Ansible

    This article is about provisioning ec2 instance and configuring webserver on it using Ansible Ansible Ansible is an…

  • Integration Of WordPress And RDS

    This article is about deploying WordPress on kubernetes and connecting it with database from AWS RDS using Terraform…

  • Multi-Tier Project In GCP

    This article is about a creating a multi tier project on public cloud GCP (Google Cloud Platform) Cloud computing…

Insights from the community

Others also viewed

Explore topics