Deploying TypeScript on Kubernetes: A Practical Basic 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 TypeScript and Kubernetes are the Perfect Pair

\n

Modern web development requires not only high-quality code but also reliable infrastructure. TypeScript, as a statically typed superset of JavaScript, allows you to create scalable server-side applications with minimal errors at the compilation stage. Kubernetes (K8s) is the de facto standard for container orchestration, providing automatic deployment, scaling, and management of applications.

\n

In this guide, we will practically walk through how to build a simple TypeScript application (Express API), package it into a Docker image, and deploy it in a Kubernetes cluster. You will get ready-made commands, configuration files, and an explanation of each step. The material is intended for developers who are already familiar with the basics of TypeScript and want to learn devops processes.

\n

By the end of the article, you will be able to independently deploy TypeScript projects into a production environment using Kubernetes.

\n\n

1. Preparing the TypeScript Application for Containerization

\n

1.1. Creating a Basic Express Server in TypeScript

\n

For demonstration, let's create a minimal API in TypeScript. Make sure you have Node.js and npm installed.

\n
mkdir ts-k8s-app\ncd ts-k8s-app\nnpm init -y\nnpm install express\nnpm install -D typescript @types/node @types/express ts-node nodemon
\n

Create tsconfig.json:

\n
{\n  "compilerOptions": {\n    "target": "ES2020",\n    "module": "commonjs",\n    "outDir": "./dist",\n    "rootDir": "./src",\n    "strict": true,\n    "esModuleInterop": true\n  },\n  "include": ["src/**/*"]\n}
\n

Now let's write the server itself. Create the file src/index.ts:

\n
import express, { Request, Response } from 'express';\n\nconst app = express();\nconst PORT = process.env.PORT || 3000;\n\napp.get('/health', (_req: Request, res: Response) => {\n  res.json({ status: 'ok', timestamp: new Date().toISOString() });\n});\n\napp.get('/', (_req: Request, res: Response) => {\n  res.send('Hello from TypeScript on Kubernetes!');\n});\n\napp.listen(PORT, () => {\n  console.log(`Server running on port ${PORT}`);\n});
\n

Add scripts to package.json:

\n
"scripts": {\n  "build": "tsc",\n  "start": "node dist/index.js",\n  "dev": "nodemon src/index.ts"\n}
\n

Check the build: npm run build && npm start. The server should start on port 3000.

\n\n

1.2. Optimization for Production

\n

For production, it is important to minimize the image size. Add .dockerignore:

\n
node_modules\ndist\n.git\n*.md\n.env
\n

Make sure all dependencies are listed in dependencies (not in devDependencies), if they are needed at runtime.

\n\n

2. Building a Docker Image for the TypeScript Application

\n

2.1. Writing the Dockerfile

\n

We use a multi-stage build to reduce the size of the final image.

\n
# Stage 1: Build\nFROM node:18-alpine AS builder\nWORKDIR /app\nCOPY package*.json ./\nRUN npm ci --only=production\nCOPY . .\nRUN npm run build\n\n# Stage 2: Production\nFROM node:18-alpine\nWORKDIR /app\nCOPY --from=builder /app/dist ./dist\nCOPY --from=builder /app/node_modules ./node_modules\nCOPY --from=builder /app/package.json ./\nEXPOSE 3000\nCMD ["node", "dist/index.js"]
\n

Key points:

\n
    \n
  • npm ci — fast and deterministic dependency installation.
  • \n
  • The second layer contains only the compiled JS and necessary modules.
  • \n
  • The base image node:18-alpine weighs only ~120 MB.
  • \n
\n\n

2.2. Building and Checking the Image

\n
docker build -t ts-k8s-app:latest .\ndocker run -p 3000:3000 ts-k8s-app:latest
\n

Open http://localhost:3000/health — you should see a JSON response. If in

Blogs

Book Recommendations