Deploying a Complete Node.js Application in Kubernetes

Complete Node.js application deployment using Kubernetes components.

Kalhara Tennakoon
Level Up Coding

--

Photo by Juanjo Jaramillo on Unsplash

Introduction

First, you need to have Docker installed on your computer so that it will make this process easy for you. Docker and Kubernetes are two different things but you need Docker as a container runtime for your containers inside Kubernetes pods.

Download Docker Desktop to your computer using the link below. Then install it and make sure you get the output by running the docker version on your terminal.

Create a Node.js Application

You can create your Node.js app according to your preferences, and if you’ve already created a node app, you can use it for this tutorial. I’ll put my node app down below, so you can get an idea about the node application.

const express = require('express');
const app = express();
const port = 8080;
app.listen(port, () => {
console.log('listening for request on port 8080');
});
app.get('/', (req, res) => {
console.log('request made');
res.sendFile("./docs/index.html", { root: __dirname })
});
app.get('/about', (req, res) => {
res.sendFile('./docs/about.html', { root: __dirname });
});
app.get('/about-us', (req, res) => {
res.redirect('/about');
});
app.use((req, res) => {
res.status(404).sendFile('./docs/404.html', { root: __dirname });
});

Here, you can see a simple node app created using express and I’ve set port 8080 for this application. Also, I’ve created a docs folder inside the project and placed all my HTML files inside it. I’ve set basic routing to access different pages of my node app and I’ve added a 404 route at the bottom of the code to capture other requests.

Screenshot by Author

Create a Dockerfile

FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD ["node", "app.js"]

I’ve used the official Node Docker image from the docker hub and exposed port 8080 as I did in my node app. Once you’re done with the Dockerfile, go ahead and create your docker image for your node application.

You can refer to the below article to see how to create a docker image for your app if you’re not aware of creating Docker images.

Don’t forget to create a docker container, then run and see the response in the browser to make sure you’ve done it correctly up to this point.

Install Minikube

I’m using Minikube which is a one-node cluster and it’s really helpful for us to deploy our apps in Kubernetes. If you don’t have Minikube installed on your computer, take a couple of minutes and refer to this article to see how to install Minikube on your computer.

Create Kubernetes components

When you’re done up to this point, you can continue configuring Kubernetes components using YAML files. Let’s create our Kubernetes deployment first.

apiVersion: apps/v1
kind: Deployment
metadata:
name: node-app-deployment
labels:
app: node-app
spec:
replicas: 1
selector:
matchLabels:
app: node-app
template:
metadata:
labels:
app: node-app
spec:
containers:
- name: node-app
image: #path to your docker image
ports:
- containerPort: 8080

You don’t need to remember each line of the YAML configuration and most of the time this file could vary according to the requirements. You can always refer to the official Kubernetes documentation when creating these files.

When you’re creating a Kubernetes Deployment, you need to add the path of your docker image to this configuration file. In the above code, you can see, I’ve indicated that place with #path to your docker image.

You can use any container registry to store your docker image and you should set its’ path to this configuration file. Also, if you’re a beginner in Kubernetes, you can use Docker Hub as your container registry which is free and push your Docker image to it.

If you’re using Docker Hub, first create an account in Docker Hub via this link. Then, create a repository in Docker Hub and open your terminal and type docker login and provide your username and password. Once your login succeeded, you can tag your Docker image and push it to the Docker Hub using the docker tag & docker push commands. You can follow the on-screen instruction to complete this process. [if you found something is not working, don’t hesitate to leave a comment below, I’ll get back to you ASAP]

Alright, now you can apply your Kubernetes deployment to the cluster using the below command. [ Make sure, you’ve started Minikube on your computer]

$ kubectl apply -f node-app-deployment.yaml
// outputrandiltennakoon@Randils-MacBook-Pro node_app % kubectl apply -f node-app-deployment.yamldeployment.apps/node-app-deployment created

Run kubectl get pods to see the pods and this process might take some time since it usually takes some time to pull the image from Docker Hub. [ You can describe the pod using kubectl describe pod <pod_name> command and use kubectl get pod --watch command to see container creating live events]

Once your pod has been created, you can start creating Kubernetes Service.

apiVersion: v1
kind: Service
metadata:
name: node-app-service
spec:
selector:
app: node-app
type: LoadBalancer
ports:
- protocol: TCP
port: 5000
targetPort: 8080
nodePort: 31110

You can create the Service using the below command. In Kubernetes services, you can allocate any node port you prefer in between 30000–32767. So I’ve selected 31110 as the node port and you will see this port once your node app started running on your browser. Also, I’ve set service type as LoadBalancer , because using Minikube, we can get an External-IP and open our app in the browser easily.

$ kubectl apply -f node-app-service.yaml
// outputrandiltennakoon@Randils-MacBook-Pro node_app % kubectl apply -f node-app-service.yamlservice/node-app-service created

You can check your services inside the cluster by running the below command.

$ kubectl get service
Screenshot by Author | services inside the cluster

Here, EXTERNAL-IP is still pending for the service. But using Minikube, you can get an External-IP. Run the below command to enable External-IP for your application.

$ minikube service <service_name>
Screenshot by Author

Opening Node.js app in the browser

The above command will automatically open your Node.js application in the browser and you will see the URL as in the above screenshot. If not, you can go to the above URL on your browser, then you will see your app runs there. Also, notice that the node port has appended to the URL.

Screenshot by Author

Conclusion

Congratulations! 🎉

You have deployed a complete Node.js application in Kubernetes.

Thanks for reading. I hope you found the information useful in this article. If you have any questions, feel free to leave a response below.

Happy coding! 👨🏻‍💻

--

--