Deploying a Web Application to Kubernetes – Basics

Introduction

I have previously written few posts on Kubernetes basics. There we covered some building blocks and how to get started with it.

Here is the high level information about the core Kubernetes resources:

  • Pod: Manages container and container environment.
  • Deployment: Manages pods and rolling updates.
  • Service: Manages network routing and DNS names.

you can read more details about these from the following earlier posts:

We have done some demos in those posts. However, now I want to kind of combine all those learning by deploying a web application to kubernetes. This will help us to have a base line for those Kubernetes basics.

Setting the Scene

I have a very simple web application. The application is built around Accounting domain and it has a frontend written in Angular, a backend API built using .NET Core and a Postgres database for storage. You can use any other application, written in different programming langauges and deployed to Kubernets principles will be same.

For this application, we have one docker image for web part (angular frontend and .NET Core Web API) and the second image for Postgres database and we also have a docker-compose file for the application. Its a very simple design and will help us to focus more on kubernetes part.

We will not be discussing application code or docker part and I am assuming that you are already familiar with docker and docker-compose.

For those of you who want to get more details about how to build and containerize such application, can check out the following two books:

I took the application code and docker files for Accounting Web Application as a starting point and copied it for folder for this post. Here is how the new folder looks like (this is git repository from earlier post, you can clone it on your machine). I also created a new folder k8s which will hold our YML files for kubernetes:

As you can see we have a docker-compose file for the application, which you can use to run the application locally using docker desktop. I also build and pushed the images on docker-hub public repository, so they can be used when we are referencing those in kubernetes YML files:

Here is how the application look when accessing it from browser:

Docker-Compose Limitations

Before we continue with kubernetes part, I want to briefly talk about the question that why we can not use Docker-Compose to manage our containerized application, why we have to learn and use Kubernetes or any other container orchestration platform?

That’s a valid question. Docker-compose is pretty powerful. You can define whole structure of your application in this YML file, store it along your source-code repo and you can bring everything up/down very easily with a single command.

However, you need to be aware that docker-compose is a client-side tool. It is not a fully fledged container platform because its only work with a single server. There are limitations limiting scalability and reliability using docker-compose. To cope with these limitations you can consider using a container orchestration e.g. docker-swarm or kubernetes etc.

In this post, we will take first step and will see one simple approach to take our existing docker-compose file and model it using kubernetes resources and run it on a kubernetes cluster.

Application Deployment on Kubernetes

Our web application has two images/containers, one for web-api + angular part and the second for database.

Here is the high-level arrangement of kubernetes resources we will model:

We can split it in following two steps:

  • Model the Database to kubernetes
  • Model the Web part (Angular and .NET Core API) to kubernetes

Model the Database to Kubernetes

For database part, the model is split across two YML files:

  • accounting-db.service.yml – The Network Service
  • accounting-db.deploymet.yml – The Deployment

accouting-db.service

Here is the YML file for the service:

I hope, the file is self explanatory as we covered these basics in previous posts. We are describing a service, its a ClusterIP type, so will be accessible only from within the cluster and then we also specified ports for postgresSQL as well.

we can now submit this file to kubernetes using following command:

kubectl apply -f accounting-db.service.yml

this will create our accounting-db kubernetes service.

Once we have created the service, we can check service endpoints using using following command:

kubectl get endpoints accounting-db

Here is the output of these commands:

But remember, the service is just an abstraction around the actual pod and we haven’t yet created the pod (that’s why endpoints is none in picture above). Lets see that part next.

accounting-db.deployment

Here is the deployment file for our database pod:

Again we have covered deployment basics in previous posts, as you can see in template section we defined the label app: accounting-db and this is what our earlier created service is expecting.

We can now create the deployment using following command:

kubectl apply -f accounting-db.deploymet.yml

This will create the deployment and the pod for the database.

Now, if we check the accounting-db service’s endpoints again and this time, we can see that value is populated:

Here are the commands output:

So, this covers the database part from our model diagram. Next lets start with Web Part.

Deploy the Web Part

In database setup part, we used two YML files. One for service and the other one for Deployment.

However, The YML file doesn’t have to have single object inside them, we can have multiple kubernetes resources in a single a file. So for this part, we will have a single YML file for both pod and service for web part (by using — as separator).

  • accounting-web.yml – Network Service + Deployment

accounting-web

Here is the file which contains description for both the service and deployment for web part:

Again, this follows the similar structure. However, this time, for service we are using is LoadBalancer type, as we want to expose this to outside of the cluster. We also have some ports mapping setup and if you have read previous posts, these all are covered there.

We can now use kubectl to deploy web part as shown below:

kubectl apply -f accounting-web.yml

Now, if I run kubectl get all command, we can see that both the db and web parts are running:

Note, we apply these YAML files one by one, however, we can apply the whole folder as well:

and now if we visit the web application URL localhost:8080, we will see that our application is working as expected:

Summary

In this post we covered kubernetes basics and also saw a very simple step by step approach about how to take an existing application and deploy it to kubernetes.

We also learned few limitations of docker-compose and saw a simple way to model application components using kubernetes resources.

You can download the code from this git repository.

Let me know if you have some comments or questions. Till next time, Happy Coding.

3 thoughts on “Deploying a Web Application to Kubernetes – Basics”

  1. Thank you for the great work and for sharing it.

    Can we automate this using git lab pipelines ?

    If yes can you advice on this.

    Thanks in advance.

    • Thank you for liking the article. I myself, however not tried git lab pipelines, so can’t say about that. However, as these are simple commands so can be easily automated with any command favored system. However, i think, these can be easily automated using bash.

Comments are closed.