Kubernetes Deployment: Connect Your Front End to Your Back End With Nginx
By: Vidhyanshu Jain
Tags: DevOps, Docker, Programming, Containers, Kubernetes

Back End

For this tutorial, we will keep our back end very simple. We have a simple Node server with Express and Mongoose.

We are using Mongoose to connect to our MongoDB. And I have chosen the port 3000 for Express.

Note: We have used mongo-service as the hostname for database URL. That’s because Kubernetes provides you with abstractions called services. We will create a mongo-service service in the latter part of this tutorial. And you can use the service name as the hostname to connect to your application ( mongo-service , in this case).

TLDR; If you have a Kubernetes service named mongo-service , you can use mongo-service as the DNS name of your application. It’s the responsibility of Kubernetes to forward the request to the corresponding pod. Don’t worry, we will revisit this.

We should also add a route /api to our back-end application.

That’s it. We are now done with our front-end application. All we have to do now is to containerize the application and write Kubernetes Objects.

Containerizing Node app

To deploy an application to Kubernetes, we need a Docker image of that application. So in this step, we will containerize our back-end app. Below is a fairly simple Dockerfile for our Node app.

I have used node-alpine as my base image. To create the Docker image, run the following command:

docker build -t backend .

Now we have an image called backend . Next, we will write Kubernetes objects.

Kubernetes objects for Node app

For any Kubernetes deployment, we need Kubernetes objects. These Kubernetes objects represent the state of your container.

We will create a Kubernetes deployment object. Deployment objects contain information such as the Docker image used, the number of replica sets, resource allocation, etc.

We will create a deployment called node-deployment . We will use our back-end image and create one replica of the container. Inside the node-deployment.yaml file, write:

With this deployment object, we are telling Kubernetes to create a deployment with name node-deployment which has one replica set of pod named node-pod . node-pod is a container of backend image with the concerned container port 3000 .

To create this deployment, just run the following command:

kubectl apply -f node-deployment.yaml

We have created a deployment, but we have no way of communicating with it. To solve this problem, we will have to create a service. Service is an abstraction in Kubernetes that assigns IP addresses to pods and a single DNS to a set of pods. As we have seen before while connecting to MongoDB, we used mongo-service as the hostname. We will be writing a service called node-service so that we can connect to our back-end app from the front end.

Our node-service.yaml file looks like this:

We have now created a service of type LoadBalancer . This service is bound to all the replicas of the pod with the name node-pod . And you can now talk to the node server using the hostname node-pod .

Apply the service using the following command:

kubectl apply -f node-service.yaml

This concludes our back-end step. We can now start working on our front-end app.