Deploying NodeJs Application Using Kubernetes

Somansh Kumar
Akeo
Published in
4 min readNov 29, 2019

As mentioned earlier, at Akeo we are always excited to not only learn about new technologies but also to help the developers community worldwide to grow with us. We have mentioned earlier how to create a high availability infrastructure using automated deployment. Now in this one, we’ll talk about how to deploy a sample Node.js application with Kubernetes and automated deployment with Jenkins.

To begin with, we assume that you have a sample Node.js application ready with you. Also, do take note that the steps mentioned in this article are tested on Ubuntu16.04 machine.

It’s time to look at the steps required to deploy Node.js application using Kubernetes:

Step 1: Creating a Simple Docker file

  • Create a Docker file with the following contents and place it in the root location of the project repository.
##DockerFile#Pulling node image from dockerhubFROM somansh1206/nodejs# Create app directoryWORKDIR /usr/src/app# Copy code from root repo to the working directoryCOPY . .#Run node commands to build and serve the applicationRUN \
npm install && \
npm install gulp -g && \
npm install swagger -g && \
gulp clean && \
gulp tsc
CMD [ “node”, “app.js” ]

Step 2: Creating Kubernetes yaml file

  • Create a Kubernetes yaml file (nodejs.yaml) with the following contents and place it in the root location of the project repository.
apiVersion: apps/v1
kind: Deployment
metadata:
name: sample-nodejs
labels:
app: sample-nodejs
spec:
selector:
matchLabels:
app: sample-nodejs
template:
metadata:
labels:
app: sample-nodejs
spec:
containers:
- image: ip_address_of_the_machine:5000/nodejs-api-image:latest
name: nodejs-api
imagePullPolicy: Always
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: sample-nodejs-svc
spec:
ports:
- name: “sample-nodejs”
targetPort: 3000
port: 3000
nodePort: 30253
protocol: TCP
selector:
app: sample-nodejs
type: NodePort

The above snippet comprises of:

  • apiVersion — which version of the Kubernetes API is being used to create this object
  • kind — what kind of object is being created
  • metadata — data that helps uniquely identify the object, including a name string, UID, and optional namespace
  • spec — what state is being desired for the object

The selector field defines how the deployment find the pods to manage, it is defined in the template (app: sample-nodejs). The template.spec defines the docker image being used to create the pod. Replace the ip_address_of_the_machine:5000 with the private docker registry details. The containerport defines the port on which application will run inside the pod.

The Kubernetes Service section: The Kubernetes services are responsible for enabling network access to a set of pods.

  • port: defines the port on which application is running inside the pod
  • nodePort: defines the port to be bind to the base machine
  • protocol: TCP default protocol for the network communication
  • selector: to identify the deployment created above

Step 3: Jenkins Configuration

  • Creating Jenkins job
  • Open the jenkins url and click on “New Item”
  • Provide a name for the job and select freestyle job
  • Provide the git repository url and credentials for cloning the project. Also specify the branch
  • Select “Execute Shell” from the add build step
  • Provide the steps as shown
  • Save the job and execute it
  • This will create docker image and push to the private docker registry. Also, the kubectl apply command will configure the pod with nodejs application running inside it. The purpose of pushing the docker image to the docker registry is that this image can be used on any other Ubuntu machine to run the application.
  • Access the sample application on the url http://ip_address_of_machine:30253

--

--