How To Run NodeJS API on Minikube

Learn how to deploy and run it on Kubernetes on your local machine

Bhargav Bachina
Jan 6, 2020 · 6 min read
Image for post
Image for post
Photo by Alice Donovan Rouse on Unsplash

Most of the companies are adopting some kind of container orchestration and running all their modern apps on Kubernetes. It’s always convenient to run the apps on Minikube which is a single-node Kubernetes cluster that can be installed on your local machine. In this post, we can deploy and run simple nodeJS rest API on Minikube on our local machine.

Introduction

Minikube is a tool that runs a single-node Kubernetes cluster in a virtual machine on your personal computer. We are going to deploy and run NodeJS rest API on MInikube in this article. First, we create an API and run it normally, then we will run the same on the Docker and, finally, we create a deployment and service objects to deploy and run it on Kubernetes locally.

We are going to need some pre-requisites to complete this project or run it on your local machine.

Example Project

Since this post is all about how we can run NodeJS API on local Kubernetes we are not going to focus much on API itself. You can check this for the full post on NodeJS API.

Here is a simple NodeJS API with two routes and returnsHello World and returning whatever the name you entered respectively.

index.js

Here is the Github link. You can clone it and run it on your machine

// clone it
git clone https://github.com/bbachi/nodejs-restapi-minikube.git
// install and start the project
npm install
npm start

Run it On Docker

Docker is a container runtime that Kubernetes uses. You can use another container runtime but the Docker is most popular for now. Let’s run this Node API on Docker. We need to define Dockerfile first which is used to automate the Docker image creation. Docker builds images by reading instructions from the Dockerfile.

Here is the Dockerfile for this project. We are building the image from base image node:10 and copying the package.json to install all the dependencies. Copying just package.json to install all the dependencies is one of the best practices. Docker builds images from the layers in the cache. We don’t want to repeat installing if there is a change in the index.js.

Dockerfile

Let’s build the image and run it on docker with these instructions.

// building the image
docker build -t node-api .
// list images
docker images
// Run the container
docker run -d --name nodeapi -p 3000:3000 node-api

Once you run the above commands you can hit these URLs in the browser. You can exec into a running container and see the file system inside the container.

// urls
http://localhost:3000/name?name=somename
http://localhost:3000/
// exec into the running container
docker exec -it nodeapi /bin/sh

Create a Deployment and Service Objects

A pod is a group of one or more containers that share the storage and network and has the specification on how to run the container. You can check the pod documentation here.

Running Local images on Minikube

Kubernetes always try to pull images from the Docker registry. If you want to use your local docker images you need to do certain steps on your local machine. Here are the steps and if you want to know more check this post.

// set the environment
eval $(minikube docker-env)
// build the image
docker build -t node-api .
// list the images
docker images

Let’s create a pod with the below file. Before that, You need to start the Minikube on your local machine with this command minikube start and create a pod with this kubectl create -f pod.yml

pod.yml

You can exec into the file system with this command kubectl exec -it node-api-pod .bin/sh

Image for post
Image for post
exec into a running pod

Deployment

Creating just one pod is not enough and what if you want to scale out the application and want to run 10 replicas at the same time. What if you want to change the number of replicas depending on the demand. That’s where the deployment comes into the picture. We specify the desired state in the deployment object such as how many replicas you want to run etc.

Kubernetes makes sure that it always meets the desired state. It creates replica sets which inturn creates pods in the background. Let’s create a Deployment for our project with this command kubectl create -f deployment.yml

deployment.yml

We have 5 replicas in the specification and the deployment creates 5 pods and 1 replica set.

Image for post
Image for post
deployment

Service

Service is an abstract way to expose an application running on a set of Pods as a network service. Let’s create a service with type NodePort so that we can access the nodejs API from the browser. Here is the service object YAML

service.yml

Create a service with this command kubectl create -f service.yml and you can list the service with this kubectl get svc

Image for post
Image for post
Running services

How to access deployment from the browser

We have created deployment and services and now we need to access this deployment from the browser.

We need to get the public IP address of the Kubernetes with this command kubectl cluster-info

Image for post
Image for post
master is running at 192.168.64.2

Get the port from the service object that we just created with this command kubectl get svc

Image for post
Image for post
service is listening on port 32329

Let’s access the nodejs API with this IP address and port with the below URLs. Make sure that you use Http instead of Https.

// endpoints port and IP address might changehttp://192.168.64.2:32329/name?name=mynamehttp://192.168.64.2:32329/

Here is the result

Image for post
Image for post
Accessing API in the browser

Summary

  • Minikube is a tool that runs a single-node Kubernetes cluster in a virtual machine on your personal computer.
  • We need to install nodejs, Docker for desktop and Minikube as prerequisites
  • We can build the project with docker docker build -t node-api and run the container with this command docker run -d --name nodeapi -p 3000:3000 node-api
  • We need a container orchestration engine for auto-scaling of applications
  • A pod is a group of one or more containers that share the storage and network and has the specification on how to run the container.
  • You need to set the environment to use local Docker images with eval $(minikube docker-env) and change the ImagePullPolicy to Never in the Pods specification.
  • A Deployment is a declarative approach to specify the number of instances of the application and makes it easier to scale up and down based on the need.
  • Service is an abstract way to expose an application running on a set of Pods as a network service.
  • You can access the local deployment with the public IP address and port of the service. We should use Nodeport in the service object.

Conclusion

It’s always convenient to use Minikube and own docker images for the local testing. We don’t even deploy in some kind of environment to test our workflow.

Bachina Labs

Tutorials Ranging from Beginner guides to Advanced | Never…

Sign up for BB Tutorials & Thoughts

By Bachina Labs

Tutorials Ranging from Beginner guides to advanced on Frontend, Backend, Blockchain, Docker, k8s, DevOps, Cloud,AI, ML. Thank you for subscribing and let me know if you want me cover anything?  Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Bhargav Bachina

Written by

Software Architect — Sharing Experiences With Examples | Frontend, Backend, Blockchain, Docker, k8s, DevOps, Cloud,AI, ML | https://www.linkedin.com/in/bachina/

Bachina Labs

Tutorials Ranging from Beginner guides to Advanced | Never Stop Learning

Bhargav Bachina

Written by

Software Architect — Sharing Experiences With Examples | Frontend, Backend, Blockchain, Docker, k8s, DevOps, Cloud,AI, ML | https://www.linkedin.com/in/bachina/

Bachina Labs

Tutorials Ranging from Beginner guides to Advanced | Never Stop Learning

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store