Deploying a web application using Kubernetes

This post outlines the steps I followed in deploying a website in Kubernetes.

Kubernetes is a platform for automating deployment, scaling and management of our application across a cluster of hosts. It works with a different range of container tools and the one we will be using here is the popular Docker.

I have divided the process into 3 main sections:

  1. Creating a Docker image for the website
  2. Build the Docker image and upload it to Docker hub
  3. Deploy the Docker image on Kubernetes

I will be using the example of Factorial application – a simple web application created by Qxf2 that can be used as a QA interview tool.

Note: Skip to section 3 if you already have a Docker image of your website.


Creating a Docker image for the website

First, we need a Docker image that will be used while creating the Kubernetes deployment.
You can directly download the Docker image for the Factorial app from our Docker Hub repository using the following command:

docker pull sravantit25/factorial-qxf2:0.1

Below I have listed the steps to create the Docker file from scratch.

I have Windows Home version, so had 2 options to use Docker : Docker Toolbox or create a Linux AWS instance (free one) and install docker on it. I used the later approach. Images that are built using Docker are portable across operating systems.

Building the Docker file includes the following steps:

a. Installing essential tools – We will need python, pip and git

RUN apt-get update && apt-get install -y \
           curl \
           python \
           python-setuptools 
           python-pip git

b. Clone the repository. It is present at https://github.com/qxf2/qa-interview-web-application

RUN git clone https://github.com/qxf2/qa-interview-web-application.git

c. Putting it all together
WORKDIR helps to define the working directory we want to run the command from. Using CMD will start the server after the docker image is completely built.

WORKDIR qa-interview-web-application
pip install -r requirements.txt
CMD python qa_interview.py

The final docker file will look like this:

#Docker file for Factorial application
FROM ubuntu
MAINTAINER Qxf2 Services
 
#Install essential tools
RUN apt-get update && apt-get install -y \
curl \
python \
python-setuptools \
python-pip git
 
#Clone the Factorial application
RUN git clone https://github.com/qxf2/qa-interview-web-application.git
 
WORKDIR qa-interview-web-application
RUN pip install -r requirements.txt
 
#Start the server
CMD python qa_interview.py

Save the above final file as Dockerfile in your working or any other directory.


Build the docker image and upload it to docker hub

a. Build the docker image. Make sure you are in the same directory where the Dockerfile is present.

docker build .

b. Upload the docker image to docker hub
I created a repo in my account and used this repo to tag the docker image.

docker tag 634a7b88de75 sravantit25/factorial-qxf2:0.1

c. push the image to the hub

docker push sravantit25/factorial-qxf2

Deploy the website docker image on Kubernetes

Applications need to be packaged into one of the supported container formats in order to be deployed on Kubernetes. We will now create a kubernetes pod where we will deploy our docker image. A pod is the basic execution unit of a Kubernetes application and is a group of one or more containers tied together for the purposes of administration and networking. A Kubernetes Deployment checks on the health of your Pod and restarts the pod’s Container if it terminates. Deployments are the recommended way to manage the creation and scaling of Pods.

a. Installation and set up on Windows
We will use minikube, which makes it easy to run Kubernetes locally. It runs a single-node Kubernetes cluster in a virtual machine on your computer.
Install Minikube on Windows using Chocolatey (run as an administrator):

choco install minikube

A Hypervisor is also required. I downloaded VirtualBox

Use the following command to start up minikube:

minikube start --driver=VirtualBox

Run the below command to check the status of cluster:

minikube status
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured

b. Verify kubectl is installed
Once the minikube is successfully configured, we can use kubectl, the Kubernetes command-line tool. It allows to run commands against Kubernetes clusters.
Use the following command to verify if kubectl is working

$kubectl version
Client Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.1", GitCommit:"d224476cd0730baca2b6e357d144171ed74192d6", GitTreeState:"clean", BuildDate:"2020-01-14T21:04:32Z", GoVersion:"go1.13.5", Compiler:"gc", Platform:"windows/amd64"}
Server Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.3", GitCommit:"06ad960bfd03b39c8310aaf92d1e7c12ce618213", GitTreeState:"clean", BuildDate:"2020-02-11T18:07:13Z", GoVersion:"go1.13.6", Compiler:"gc", Platform:"linux/amd64"}

c. Create a Kubernetes deployment using the docker image

$ kubectl create deployment qxf2-factorial-app --image=sravantit25/factorial-qxf2:0.1
deployment.apps/qxf2-factorial-app created

d. Check if the pod is running

$ kubectl get pod
NAME READY STATUS RESTARTS AGE
qxf2-factorial-app-58dcdbc97f-5zzt5 1/1 Running 0 3m20s

This means that the website is running inside the pod.

e. To test the running server (pod), port-forward the pod port 6464 to localhost port 8081 (or any other port of your choice)

$ kubectl port-forward qxf2-factorial-app-58dcdbc97f-5zzt5 8081:6464
Forwarding from 127.0.0.1:8081 > 6464
Forwarding from [::1]:8081 > 6464

f. Test by running

http://localhost:8081/


That is it. We can get our server running in the pod. Next steps, look at ingress, load balancing, auto scaling etc.


References:
https://kubernetes.io/docs/setup/learning-environment/minikube/

Leave a Reply

Your email address will not be published. Required fields are marked *