Introduction: Why Deploy JavaScript on Kubernetes?
\n\nModern web development is unthinkable without containerization. If you write in JavaScript (Node.js) and want your application to run stably, scale automatically, and be fault-tolerant — Kubernetes (K8s) will become your best friend.
\n\nKubernetes is a container orchestration platform. It manages the deployment, scaling, and operation of your applications. In this guide, we will walk through the basic deployment of a simple Node.js application on Kubernetes. You will learn how to package your code into a Docker image, create a Pod, Service, and Deployment.
\n\nThis article is intended for beginners. It is assumed that you already have Docker and Minikube installed (or access to a K8s cluster).
\n\nStep 1: Preparing the Node.js Application and Docker Image
\n\nFirst, let's create a simple web application using Express. It will respond to HTTP requests.
\n\n1.1. Application Code (app.js)
\n\nCreate a file app.js:
const express = require('express');\nconst app = express();\nconst PORT = process.env.PORT || 3000;\n\napp.get('/', (req, res) => {\n res.send('Hello from Kubernetes!');\n});\n\napp.listen(PORT, () => {\n console.log(`Server running on port ${PORT}`);\n});\n\n\n1.2. package.json File
\n\nCreate package.json:
{\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\n1.3. Dockerfile
\n\nNow let's create a Dockerfile to build the image:
FROM node:18-alpine\n\nWORKDIR /usr/src/app\n\nCOPY package*.json ./\nRUN npm install\n\nCOPY . .\n\nEXPOSE 3000\n\nCMD ["node", "app.js"]\n\n\n1.4. Building and Pushing the Image
\n\nRun the commands:
\n\ndocker build -t my-node-app:v1 .\n\n\nIf you are using Minikube, build the image inside its environment:
\n\nminikube image build -t my-node-app:v1 .\n\n\nOr push the image to Docker Hub/any registry and specify it in the manifest.
\n\nStep 2: Basic Kubernetes Objects (Pod, Deployment, Service)
\n\nKubernetes works with objects. For deployment, we will need three main ones:
\n\n- \n
- Pod — the smallest unit, one or more containers. \n
- Deployment — manages Pods, provides scaling and updates. \n
- Service — a stable access point to Pods (usually via ClusterIP or NodePort). \n
2.1. Creating a Deployment
\n\nCreate a file deployment.yaml:
apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: node-app-deployment\nspec:\n replicas: 3\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-node-app:v1\n ports:\n - containerPort: 3000\n env:\n - name: PORT\n value: "3000"\n\n\nExplanation:
\n- \n
replicas: 3— we will run 3 copies of the application for fault tolerance. \n selectorandlabels— link the Deployment to the Pods. \n image— the name of your Docker image. \n
2.2. Creating a Service
\n\nCreate a file service.yaml to access the application:
apiVersion: v1\nkind: Service\nmetadata:\n name: node-app-service\nspec:\n selector:\n app: node-app\n ports:\n - protocol: TCP\n port: 80\n targetPort: 3000\n type: NodePort\n\n\nWhat is happening here:
\n- \n
- The Service redirects traffic from port