Deploying TypeScript on Kubernetes: A Basic Guide for Beginners
\n\nModern 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\n1. Preparing the TypeScript Application
\n\nBefore 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\n1.1. Project Initialization
\nCreate a project folder and run the initialization:
\nmkdir 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\n1.2. TypeScript Configuration
\nCreate a tsconfig.json file:
{\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\n1.3. Application Code
\nCreate a src folder and an index.ts file:
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\n1.4. Build and Run
\nAdd scripts to package.json:
"scripts": {\n "build": "tsc",\n "start": "node dist/index.js",\n "dev": "ts-node src/index.ts"\n}\nCheck local startup: npm run dev and open http://localhost:3000.
2. Containerization with Docker
\n\nKubernetes works with containers. We need to create a Docker image of our TypeScript application.
\n\n2.1. Dockerfile
\nCreate a Dockerfile in the project root:
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"]\nWe use multi-stage build: first compile TypeScript, then create a lightweight final image.
\n\n2.2. Building the Image
\ndocker build -t my-ts-app:latest .\nCheck locally:
\ndocker run -p 3000:3000 my-ts-app:latest\n\n2.3. .dockerignore
\nTo avoid copying unnecessary files, create a .dockerignore file:
node_modules\ndist\n.git\n*.md\n\n3. Deploying to Kubernetes
\n\nNow that we have a Docker image, we can move on to Kubernetes. For local testing, I recommend Minikube or Kind.
\n\n3.1. Starting Minikube
\nminikube start --driver=docker\nminikube status\n\n3.2. Loading the Image into Minikube
\nFor Kubernetes to find our image, load it into the Minikube environment:
\nminikube image load my-ts-app:latest\n\n3.3. Creating a Deployment
\nCreate a deployment.yaml file:
apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: my-ts-app-deployment\nspec:\n replicas: 2\n selector:\n matchLabels:\n app: my-ts