Deploying Containerized Web Application on Kubernetes Cluster:

Ghazanfar Ali
7 min readAug 13, 2023

--

Scenario:

We have a web application stack which is already containerized now its time to host it for production we need high availability so our containers do not go down, if something happens to our containers like they are not responding in that case they should auto heal. Our containers should be platform independent.

We will use kubernetes as container orchestration tool. As kubernetes is most famous in container orchestration as 77 percent containers runs on kubernetes, if we include redshift and rencher that are also build on kubernetes then it becomes 89 percent that is huge.

We will use KOPS to launch our kubernetes cluster on AWS. We will create EBS volume to run our DB pod. Then we will write definitions file in K8s to create our objects in kubernetes cluster.

Lets make it happen!

First we will create EC2 instance with name KOPS:

We also need s3 bucket to maintain kops state:

Now we will create IAM user with admin access and access keys for AWSCLI:

Create a hosted zone in route 53:

Now we will add ns servers entries in our purchased domain registrar like godaddy:

Now login to ec2 instance and setup everything:

Generate ssh keys on instance also install awscli on it.

Once install awscli then configure it with iam usr access keys:

Now install kubectl and kops on that.

https://kubernetes.io/docs/setup/production-environment/tools/kops/

https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/

Once kops and kubectl installed check the domain:

Now we can run kops command to create kubernetes cluster:

⇒ kops create cluster — name=kubepro.mydevopsstar.com — state=s3://vprofile0-kops-state — zones=us-east-1a,us-east-1b — node-count=2 — node-size=t3.small — master-size=t3.medium — dns-zone=kubepro.mydevopsstar.com — node-volume-size=8 — master-volume-size=8

This command will create conf file for kubernetes cluster and store in s3 bucket.

This command will launch our K8s cluster.

After few minutes around 15 minutes run command to validate the cluster:

This will check the health of our cluster.

You may face issue in validation if kops version 1.27 is installed then downgrade it to 1.26:

You can also see new instances on master node and 2 worker node and scaling group that created them:

Now we will create EBS volume for our DB pod data that store in /var/lib/mysql into EBS volume:

To make sure that our DB pod runs in the same zone where our node is, we can make that through node selector option in definition file, node selector works with labels wo we will create our own labels.

As our ebs volume is in us-east-1a so we will add same label for both worker nodes:

Now add labels for both nodes:

Note:

Labels in Kubernetes are key-value pairs that you can attach to resources like nodes, pods, services, and more. They are used to add metadata and information to these resources, allowing you to organize, identify, and manage them more effectively.

Now we will start writing the definition files.

We will write our first definition file “secret” which will have encoded user and passwords that our application needed like database user, password, memcached service user password and ports etc:

First encode the passwords then use that in secret definition file:

Database password:

Rabbitmq password:

Now use any ide to write first definition file and link it to the your github repo then push first file that contain secrets:

These all definition files are available on github:

https://github.com/devops-CloudComputing/Kubernetes-app-deployment

Then clone that repo to your KOPS instance so that you will have that definition file:

Lets create object from that definition file:

Now we will write our db definition file:

But first we will add tag in EBS volume that created earlier else our db pod will not be able to attach that volume will get permission deny error:

Now clone this file on kops instance in same directory where we cloned the app-secret.yaml file then create the object from this definition file:

It will create new pod for us check the pod and its detail using describe command:

We have to create service (cluster ip) for our db so that our application can access database its for internal access not for outside world:

After this we will write deployment definition file for memcached service:

lets create a service definition file for this this service will accept the request and forward it to the upper deployment file of memcached:

Now we will write definition file and service of rebbitmq:

Same definition files will be written for tomcat application:

Note: In this definition file we will create multiple containers the main and the other is init container whenever we have any dependencies to sort our before main container creation we craete init container and give commands there so until init container successfully executes that command the main container will not create.

Tomcat service file:

In this service def file we ill use service type load balancer instead of clusterIP because this tomcat app will get the outside traffic.

Fetch this all files in KOPS instance using git pull:

Now create objects from these all files:

Now copy the load balancer endpoint and paste in the browser:

Our application is working on kubernetes cluster:

Also login with user admin_vp and password admin_vp:

In the end do not forget to delete the cluster elae you have to pay heavy amount to aws:

Thats all in this project.

--

--

Ghazanfar Ali
Ghazanfar Ali

Written by Ghazanfar Ali

I write technical blogs on DevOps and AWS, Azure and GCP

No responses yet