Your latest Node.js content, news and updates in one place.

maxresdefault (1).jpg
How To Deploy a Resilient Node.js Application on Kubernetes From Scratch

This post was first published in Digital Ocean blog post.


You may have heard the buzz around Kubernetes and noticed that many companies have been rapidly adopting it. Due to its many components and vast ecosystem it can be quite confusing to find where the path starts to learn it.

In this session, you will learn the basics of containers and Kubernetes. Step by step, we will go through the entire process of packaging a Node.js application into a Docker container image and then deploying it on Kubernetes. We will demonstrate scaling to multiple replicas for better performance. The end result will be a resilient and scalable Node.js deployment.

You will leave this session with sufficient knowledge of containerization, Kubernetes basics, and the ability to deploy highly available, performant, and scalable Node.js applications on Kubernetes.

Resources

View the slides for this talk

Transcript of The Commands and Manifests Used

Be sure to follow along with the recording for an explanation and replace kamaln7 with your own DockerHub username.

Node App

  1. Create an empty node package: npm init -y
  2. Install express as a dependency: npm install express
  3. index.js
const express = require('express')
const os = require('os')

const app = express()
app.get('/', (req, res) => {
        res.send(`Hi from ${os.hostname()}!`)
})

const port = 3000
app.listen(port, () => console.log(`listening on port ${port}`))

Docker

  1. Dockerfile
FROM node:13-alpine

WORKDIR /app

COPY package.json package-lock.json ./

RUN npm install --production

COPY . .

EXPOSE 3000

CMD node index.js
  1. Build the image: docker build -t kamaln7/node-hello-app .
  2. Edit index.js and replace the word Hi with Hello.
  3. Re-build the image and notice Docker re-using previous layers: docker build -t kamaln7/node-hello-app .
  4. Run a container to test it: docker run --rm -d -p 3000:3000 kamaln7/node-hello-app
  5. Look at the running containers: docker ps
  6. Stop the container: docker stop CONTAINER_ID
  7. Push the image to DockerHub: docker push kamaln7/node-hello-app

Kubernetes

  1. Get worker nodes: kubectl get nodes
  2. Create a deployment: kubectl create deployment --image kamaln7/node-hello-app node-app
  3. Scale up to 3 replicas: kubectl scale deployment node-app --replicas 3
  4. Expose the deployment as a NodePort replica: kubectl expose deployment node-app --port 3000
  5. Look at the newly created service (and the assigned port): kubectl get services
  6. Grab the public IP of one of the worker nodes: kubectl get nodes -o wide
  7. Browse to IP:port to test the service
  8. Edit the service: kubectl edit service node-app
  9. Replace port: 3000 with port: 80
  10. Replace type: NodePort with type: LoadBalancer
  11. Verify that the service was updated: kubectl get service
  12. Run the above command every few seconds until you get the external IP address of the Load Balancer
  13. Browse to the IP of the Load Balancerhhuy

Read More