Deploying TypeScript on Kubernetes: Basic Guide
Modern web development increasingly requires not only writing quality code, but also competent application deployment. TypeScript, thanks to strict typing and powerful tools, has become a popular choice for backend services on Node.js. However, without proper deployment, even perfect code will remain on the local machine. Kubernetes — the container orchestration standard — allows you to automate deployment, scaling, and management of applications. In this guide, we'll walk through the step-by-step process of deploying a TypeScript application on Kubernetes.
1. Preparing the TypeScript Application for Containerization
Before sending code to the cluster, you need to ensure the application is ready to work in a container. Let's look at a typical Express.js project with TypeScript.
Project Structure
my-ts-app/├── src/│ ├── index.ts│ └── routes/├── dist/├── package.json├── tsconfig.json└── DockerfileConfiguring tsconfig.json
Make sure the TypeScript compiler builds the project into the dist folder:
{ "compilerOptions": { "target": "ES2020", "module": "commonjs", "outDir": "./dist", "rootDir": "./src", "strict": true, "esModuleInterop": true }, "include": ["src/**/*"]}Simple Server (src/index.ts)
import express, { Request, Response } from 'express';
const app = express();const PORT = process.env.PORT || 3000;
app.get('/health', (_req: Request, res: Response) => { res.json({ status: 'ok', timestamp: new Date().toISOString() });});
app.listen(PORT, () => { console.log(`Server running on port ${PORT}`);});Add scripts to package.json:
"scripts": { "build": "tsc", "start": "node dist/index.js", "dev": "ts-node src/index.ts"}2. Building a Docker Image for TypeScript
Containerization is a key step before deploying to Kubernetes. We use multi-stage build to reduce the image size.
Dockerfile
# Stage 1: BuildFROM node:18-alpine AS builderWORKDIR /appCOPY package*.json ./RUN npm ciCOPY tsconfig.json ./COPY src/ ./src/RUN npm run build
# Stage 2: ProductionFROM node:18-alpine AS productionWORKDIR /appCOPY --from=builder /app/dist ./distCOPY --from=builder /app/package*.json ./RUN npm ci --only=productionEXPOSE 3000CMD ["node", "dist/index.js"]Build the image and push it to a registry (e.g., Docker Hub):
docker build -t yourusername/my-ts-app:v1 .docker push yourusername/my-ts-app:v13. Writing Kubernetes Manifests
For deployment in Kubernetes, you'll need at least two resources: Deployment (pod management) and Service (application access).
deployment.yaml
apiVersion: apps/v1kind: Deploymentmetadata: name: my-ts-app labels: app: my-ts-appspec: replicas: 2 selector: matchLabels: app: my-ts-app template: metadata: labels: app: my-ts-app spec: containers: - name: app image: yourusername/my-ts-app:v1 ports: - containerPort: 3000 env: - name: PORT value: "3000" livenessProbe: httpGet: path: /health port: 3000 initialDelaySeconds: 5 periodSeconds: 10 readinessProbe: httpGet: path: /health port: 3000 initialDelaySeconds: 3 periodSeconds: 5service.yaml
apiVersion: v1kind: Servicemetadata: name: my-ts-app-servicespec: selector: app: my-ts-app ports: - protocol: T