Tech Talk

How to Deploy a Resilient Node.js Application on Kubernetes From Scratch

Updated on September 29, 2020
How to Deploy a Resilient Node.js Application on Kubernetes From Scratch

Video

About the Talk

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.

💰 Use this free $200 credit to try out Kubernetes on DigitalOcean for free!

About the Presenter

Kamal Nasser is a Developer Advocate at DigitalOcean. If not automating and playing with modern software and technologies, you’ll likely find him penning early 17th-century calligraphy. You can find Kamal on Twitter at @kamaln7 or on GitHub at @kamaln7.

Resources

View the slides for this talk, or watch the recording on YouTube.

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
    
  2. Build the image: docker build -t kamaln7/node-hello-app .

  3. Edit index.js and replace the word Hi with Hello.

  4. Re-build the image and notice Docker re-using previous layers: docker build -t kamaln7/node-hello-app .

  5. Run a container to test it: docker run --rm -d -p 3000:3000 kamaln7/node-hello-app

  6. Look at the running containers: docker ps

  7. Stop the container: docker stop CONTAINER_ID

  8. 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 --type=NodePort --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
    1. Replace port: 3000 with port: 80
    2. Replace type: NodePort with type: LoadBalancer
  9. Verify that the service was updated: kubectl get service
  10. Run the above command every few seconds until you get the external IP address of the Load Balancer
  11. Browse to the IP of the Load Balancer

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
2 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

very good tutorial!

How do we apply SSL / lets encrypt in kubernetes? Do you have any tutorial about this?

thanks

This is a really good tutorial. Most tutorials focus too much on the .yml config which is not easy to understand when just getting started. Also, the delivery is high quality and engaging.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel