Deploying TypeScript 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

Deploying TypeScript on Kubernetes: A Basic Guide for Beginners

\n\n

Modern web development is unthinkable without containerization and orchestration. If you write in TypeScript and want your application to run stably, scale automatically, and be fault-tolerant, Kubernetes (K8s) will become your best friend. In this guide, we will walk through step by step how to package a TypeScript application in Docker and deploy it in a Kubernetes cluster. The material is designed for beginners: no magic, only clear examples and explanations.

\n\n

1. Preparing the TypeScript Application

\n\n

Before thinking about Kubernetes, you need to ensure your application is ready to run in a container. Let's create a simple HTTP server using Express with TypeScript.

\n\n

1.1. Project Initialization

\n

Create a project folder and run the initialization:

\n
mkdir my-ts-app && cd my-ts-app\nnpm init -y\nnpm install express\nnpm install -D typescript @types/node @types/express ts-node nodemon
\n\n

1.2. TypeScript Configuration

\n

Create a tsconfig.json file:

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

1.3. Application Code

\n

Create a src folder and an index.ts file:

\n
import express, { Request, Response } from 'express';\n\nconst app = express();\nconst PORT = process.env.PORT || 3000;\n\napp.get('/', (req: Request, res: Response) => {\n  res.json({ message: 'Hello from TypeScript on Kubernetes!' });\n});\n\napp.listen(PORT, () => {\n  console.log(`Server running on port ${PORT}`);\n});
\n\n

1.4. Build and Run

\n

Add scripts to package.json:

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

Check local startup: npm run dev and open http://localhost:3000.

\n\n

2. Containerization with Docker

\n\n

Kubernetes works with containers. We need to create a Docker image of our TypeScript application.

\n\n

2.1. Dockerfile

\n

Create a Dockerfile in the project root:

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

We use multi-stage build: first compile TypeScript, then create a lightweight final image.

\n\n

2.2. Building the Image

\n
docker build -t my-ts-app:latest .
\n

Check locally:

\n
docker run -p 3000:3000 my-ts-app:latest
\n\n

2.3. .dockerignore

\n

To avoid copying unnecessary files, create a .dockerignore file:

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

3. Deploying to Kubernetes

\n\n

Now that we have a Docker image, we can move on to Kubernetes. For local testing, I recommend Minikube or Kind.

\n\n

3.1. Starting Minikube

\n
minikube start --driver=docker\nminikube status
\n\n

3.2. Loading the Image into Minikube

\n

For Kubernetes to find our image, load it into the Minikube environment:

\n
minikube image load my-ts-app:latest
\n\n

3.3. Creating a Deployment

\n

Create a deployment.yaml file:

\n
apiVersion: apps/v1\nkind: Deployment\nmetadata:\n  name: my-ts-app-deployment\nspec:\n  replicas: 2\n  selector:\n    matchLabels:\n      app: my-ts                

Blogs

Book Recommendations