Deploying JavaScript on Kubernetes: A Basic Guide for Beginners

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\n

Modern 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\n

Kubernetes 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\n

This article is intended for beginners. It is assumed that you already have Docker and Minikube installed (or access to a K8s cluster).

\n\n

Step 1: Preparing the Node.js Application and Docker Image

\n\n

First, let's create a simple web application using Express. It will respond to HTTP requests.

\n\n

1.1. Application Code (app.js)

\n\n

Create a file app.js:

\n\n
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\n

1.2. package.json File

\n\n

Create package.json:

\n\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\n

1.3. Dockerfile

\n\n

Now let's create a Dockerfile to build the image:

\n\n
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\n

1.4. Building and Pushing the Image

\n\n

Run the commands:

\n\n
docker build -t my-node-app:v1 .\n
\n\n

If you are using Minikube, build the image inside its environment:

\n\n
minikube image build -t my-node-app:v1 .\n
\n\n

Or push the image to Docker Hub/any registry and specify it in the manifest.

\n\n

Step 2: Basic Kubernetes Objects (Pod, Deployment, Service)

\n\n

Kubernetes 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
\n\n

2.1. Creating a Deployment

\n\n

Create a file deployment.yaml:

\n\n
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\n

Explanation:

\n
    \n
  • replicas: 3 — we will run 3 copies of the application for fault tolerance.
  • \n
  • selector and labels — link the Deployment to the Pods.
  • \n
  • image — the name of your Docker image.
  • \n
\n\n

2.2. Creating a Service

\n\n

Create a file service.yaml to access the application:

\n\n
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\n

What is happening here:

\n
    \n
  • The Service redirects traffic from port

Blogs

Book Recommendations