Deploying JavaScript (Node.js) on Kubernetes: A Step-by-Step Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Deploy JavaScript on Kubernetes?

\n

Modern web development with JavaScript (especially Node.js) increasingly requires not just running an application on a single server, but organizing a fault-tolerant, scalable, and easily manageable infrastructure. Kubernetes (K8s) is the de facto standard for container orchestration. If you write in Node.js and want your application to handle peak loads, automatically recover from failures, and be easily updated without downtime — you need Kubernetes.

\n

In this guide, we will walk through the basic process of deploying a simple Node.js application to a Kubernetes cluster. You will learn how to prepare a Docker image, write the necessary YAML manifests (Deployment, Service, Ingress), and run the application. The article is aimed at developers who are already familiar with the basics of Node.js and Docker but are just starting their journey into the world of Kubernetes.

\n
\n

1. Preparing the Node.js Application and Docker Image

\n

First, we need an application to deploy. Let's create a simple HTTP server using Express that responds to GET requests.

\n

1.1. Creating a Simple Server (app.js)

\n
const express = require('express');\nconst app = express();\nconst PORT = process.env.PORT || 3000;\n\napp.get('/', (req, res) => {\n  res.send('Hello from Node.js on Kubernetes!');\n});\n\napp.listen(PORT, () => {\n  console.log(`Server running on port ${PORT}`);\n});\n
\n

File package.json (dependencies):

\n
{\n  "name": "k8s-node-app",\n  "version": "1.0.0",\n  "main": "app.js",\n  "scripts": {\n    "start": "node app.js"\n  },\n  "dependencies": {\n    "express": "^4.18.2"\n  }\n}\n
\n

1.2. Dockerfile for Node.js

\n

For Kubernetes to run our application, it needs to be packaged into a container. We will use the official Node.js image (version 18 LTS).

\n
# Use the official Node.js image\nFROM node:18-alpine\n\n# Set the working directory inside the container\nWORKDIR /usr/src/app\n\n# Copy package.json and package-lock.json\nCOPY package*.json ./\n\n# Install dependencies\nRUN npm install --production\n\n# Copy the application source code\nCOPY . .\n\n# Specify the port the application will listen on\nEXPOSE 3000\n\n# Command to run the application\nCMD ["node", "app.js"]\n
\n

Building the image and pushing it to a registry:

\n
# Build the image\ndocker build -t my-registry/node-app:v1 .\n\n# Push to Docker Hub or your private registry\ndocker push my-registry/node-app:v1\n
\n

Replace my-registry with your registry name (e.g., docker.io/username/node-app).

\n
\n

2. Creating YAML Manifests for Kubernetes

\n

Kubernetes uses a declarative approach: we describe the desired state of the system in YAML files, and K8s itself brings everything to that state. We will need three main resources: Deployment, Service, and Ingress.

\n

2.1. Deployment (deployment.yaml)

\n

The Deployment is responsible for running and updating pods (containers). It ensures the required number of replicas is always running.

\n
apiVersion: apps/v1\nkind: Deployment\nmetadata:\n  name: node-app-deployment\n  labels:\n    app: node-app\nspec:\n  replicas: 3  # Number of replicas (copies) of the application\n  selector:\n    matchLabels:\n      app: node-app\n  template:\n    metadata:\n      labels:\n        app: node-app\n    spec:\n      containers:\n      - name: node-app\n        image: my-registry/node-app:v1\n        ports:\n        - containerPort: 3000\n        env:\n        - name: PORT\n          value: "3000"\n        resources:\n          requests:\n            memory: "64Mi"\n            cpu: "250m"\n          limits:\n            memory: "128Mi"\n            cpu: "500m"\n
\n

Key points:

\n
    \n
  • replicas: 3 — starts

Blogs

Book Recommendations