CONTAINER MANAGEMENT WITH KUBERNETES

USING KUBERNETES SERVICE, PERSISTENT VOLUME CLAIM, DEPLOYMENT AND JENKINS TO LAUNCH A WEBSITE

Yobah Bertrand Yonkou
6 min readJun 25, 2020

--

What is Kubernetes?

In a nutshell, Kubernetes is an open-source automation tool for deployment, scaling, and management of containerized applications. Kubernetes used container engines to launch containers and manages them in forms of pods (groups of containers). Kubernetes provides a wide range of resources that can be used for providing persistent storage, fault-tolerant environment, no downtime, and so on.

What is a Kubernetes service?

A Kubernetes service is a resource that is used to provide a single IP address that will represent a group of pods. There are three types of services in Kubernetes namely; ClusterIP, NodePort, and Load balancer. Here, we are concerned with the NodePort.

A NodePort can be seen as an open port through which the outside world can connect to pods in a node. These ports typically range from 30000 to 32767.

What is Persistent Volume Claim (PVC)?

In the nutshell, PVC is a request for volume made by a client. This request is sent to a service called Persistent Volume which later sends it to a storage class. The storage class then allocates volume and sends it back to PV and sends it to PVC. This volume can be mounted to a folder (mostly data directory) of a container to store its contents permanently.

What is Deployment?

In Kubernetes, a deployment is a resource that is used to manage pods based on a user’s desire. Deployments use replica sets in the background to monitor pods. Deployments are capable of updating content in a pod and many more. Click the link below to see one interesting use case of deployments.

In this blog, we will see how to use a Deployment, PVC, SVC, and Jenkins to deploy a website. To demonstrate this, we will try to solve the following problem statements.

PROBLEM STATEMENTS

  1. Create a 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 start the Jenkins service in the container.
  3. Create a job chain of job1, job2, job3, and job4 using the build pipeline plugin in Jenkins.
  4. Job1: Pull the Github repo automatically when some developers push a repo to Github.
  5. 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 ( eg. If code is of PHP, then Jenkins should start the container that has PHP already installed )
    — Expose your pod so that the testing team could perform the testing on the pod.
    — Make the data to remain persistent ( If the server collects some data like logs, other user information ).
  6. Job3: Test your app if it is working or not.
  7. Job4: if the app is not working, then send an email to the developer with error messages and redeploy the application after code is being edited by the developer.

SOLUTION WALKTHROUGH

In this walkthrough, we will use the Jenkins server on RedHat 8. Thus no need for problems 1 and two. Also, problem three will be the last problem to look at because we will use it to visualize how our Jenkins jobs are connected.

1. Job 1 (repoPuller): This job listens to a webhook from a GitHub that triggers job 1 when a developer pushes his code to GitHub.

→Job Configuration:

Create new item (job)
Enter the desired job name
Setting custom workspace
Specifying GitHub repository where code will be pulled
Makes job to be triggered bu GitHub webhook

2. Job 2 (contLauncher): This job is one of the main jobs in this task. This job basically examines the code pulled from GitHub and on the basis of the language used, it launchers a pod (container) that has the respective compiler already installed.

This job determines the language used by searching for a particular index file. The index file is considered as the main code file that will invoke other files. So, if the index file is index.html, then it means that we are dealing with HTML code thus an HTML server is launched.

On the other hand, if we have an index.php file, then it is obvious that we are looking at PHP code thus a PHP server is launched.

In this same job, before launching a container, we launch a NodePort service and a persistent volume claim. The NodePort exposes our deployment to the outside world for connectivity.

The PVC claims 2Gi from persistent volume. This volume is later mounted to the data directory of our web server. This is so that we can retain our webpages even if the pod/web server fails. It is important to note that the PVC has to be launched before our deployment.

To save some time, this job first of all checks if our deployment is already running. If it is running, then there is no need to try launching it again. If it is not running then, it checks whether our PVC is running and if it is running then it goes forth and examines our index file as stated above. If the required PVC is not running, then it is launched alongside our SVC then goes forth to examine our index file.

NB: If a deployment is already running and new code comes, we can update it by rolling out the old code. Click below to see more on rolling out.

To launcher the deployments and other resources like PVC, SVC, etc. we used a DSL language called YAML. This language is used to transfer data from a Kubernetes user to the Kubernetes API server for processing. The link to all the codes used will be at the bottom of this blog.

→Job Configuration:

→Job Execute Shell Code:

3. Job 3 & Job 4 (siteTester): This job connects to the webserver after it has been launched and retrieves a status code. This status code is checked with the SUCCESS code 200. If the status code retrieved from the server is different from 200, then, there's a problem with the server thus a mail is sent to the developer.

→Job Configuration:

Create a new job and enter the following code in the Build: Execute Shell section;

→Job Execute Shell Code:

Pipeline Visualization of Jobs

To see how to add the aforementioned jobs, visit the link below and scroll to the bottom of the page.

→Visualization:

TASK 3 DEMONSTRATION VIDEO

Acknowledgments:

I would like to thank Mr. Vimal Daga and LinuxWorld Informatics Pvt Ltd for taking such an initiative to help and train students during this COVID-19 period. Thank you, Sir.

--

--