Skip to main content

Command Palette

Search for a command to run...

Deploy Nodejs application to Kubernetes

Updated
Deploy Nodejs application to Kubernetes
Y

Tech Lead & Architect | 13+ Years in Cloud, Backend, and AI - Experienced software engineer with expertise in Java, Spring Boot, Microservices, Angular, React, Kafka, DevOps, Python, PySpark, Databricks, and Generative AI. Certified in TOGAF, AWS, and Google Cloud. Passionate about building scalable, secure, and high-performance systems. Enthusiast in Data Engineering & Agentic AI. Author of 1,200+ technical articles sharing insights across diverse tech stacks.

Date: 2022-07-05

Deploying a Node.js Application to Kubernetes: A Comprehensive Guide

This article explains how to deploy a simple Node.js application to a Kubernetes cluster running on Docker Desktop. We will cover setting up the necessary tools, creating the application, building a Docker image, and finally, deploying it to Kubernetes. This tutorial assumes a basic understanding of the command line interface.

First, we need to prepare our development environment. This involves installing Node.js and npm (Node Package Manager). Node.js is the JavaScript runtime environment, and npm is used to manage project dependencies – essentially, the other software components your application needs to function. The installation process is straightforward, typically involving downloading an installer from the official Node.js website and following the on-screen instructions. After installation, verifying the setup is simple: open a command prompt and type node -v and npm -v. Successful installations will display the versions of Node.js and npm, respectively.

Next, we'll set up Docker. Docker is a containerization technology that packages applications and their dependencies into isolated units called containers. This ensures consistent execution across different environments. Docker Desktop simplifies the process of running Docker on your local machine. If you don't already have Docker installed, download and install it from the official Docker website. You can verify the installation by opening a command prompt and typing docker version. This should display the Docker version information.

Now we need Kubernetes. Kubernetes is an orchestration system for containerized applications. It simplifies the deployment, scaling, and management of applications running in containers across multiple hosts. For this tutorial, we will use Docker Desktop's built-in Kubernetes functionality, providing a single-node cluster for easy experimentation. If Kubernetes is not already enabled in your Docker Desktop settings, enable it. You can verify that Kubernetes is running by using the command kubectl version, which should show the Kubernetes version information.

With Node.js, npm, Docker, and Kubernetes set up, we can begin building our application. We will create a simple Node.js application that serves a "Hello, World!" message. We will start by creating a new project directory. Then, inside this directory, we will initialize a new Node.js project using the command npm init -y. This creates a package.json file, which is a crucial file containing metadata about our project, including dependencies and scripts. We'll then need to install the necessary packages for our application. In a simplified example, this might involve just the express framework for creating web servers. This is done via the command npm install express.

Now, we create the actual application code. This code will use the express framework to listen on a specific port (for example, 4489) and handle requests to a route (such as '/'). When a request is made to this route, the server sends back the "Hello, World!" message.

Next, we need to create a Dockerfile. The Dockerfile is a text file that contains instructions for building a Docker image. These instructions describe the base image (e.g., a Node.js image), the necessary files to copy into the image, and commands to run when the container starts. Once the Dockerfile is created, we build the Docker image using the command docker build -t my-nodejs-app . The -t flag specifies the name of the image (my-nodejs-app in this example), and the . specifies the context of the build (the current directory).

After successfully building the image, it's time to deploy it to Kubernetes. This involves creating Kubernetes deployment and service manifests. A deployment defines how many instances (pods) of our application to run, while a service exposes the application to the outside world. These are typically defined in YAML files (e.g., kube.yml). The deployment specifies the Docker image to use, and the service defines the port mapping (e.g., mapping port 5000 on the service to port 4489 on the application container). These YAML files are then applied to the Kubernetes cluster using the command kubectl apply -f kube.yml.

Finally, after the deployment and service are successfully created, we can access our application through the service's external IP address and port, which Kubernetes will provide. This will typically be a local address like http://localhost:5000. Accessing this address in a web browser should display the "Hello, World!" message from our application, successfully deployed to our Kubernetes cluster.

In summary, this process demonstrates a fundamental workflow for deploying containerized applications to Kubernetes. It involves preparing the development environment, creating the application, packaging it in a Docker image, and finally, orchestrating its deployment and exposure using Kubernetes. While this is a simplified example, it illustrates the core principles involved in deploying more complex applications to Kubernetes, emphasizing the importance of containerization and orchestration for modern application development and deployment. Remember to consult the official documentation for Node.js, Docker, and Kubernetes for detailed instructions and further learning.

Read more

More from this blog

The Engineering Orbit

1174 posts

The Engineering Orbit shares expert insights, tutorials, and articles on the latest in engineering and tech to empower professionals and enthusiasts in their journey towards innovation.