Deploying a TypeScript API on Kubernetes can seem complex, but with the right tools and steps, it becomes a manageable process. This article provides a real-world example of how to deploy a TypeScript API using Docker containers and Helm charts on a Kubernetes cluster.

Prerequisites

  • A running Kubernetes cluster (local or cloud-based)
  • Node.js and npm installed on your development machine
  • Docker installed and running
  • Helm installed on your machine
  • kubectl configured to interact with your cluster

Step 1: Create Your TypeScript API

Begin by setting up a simple TypeScript API using a framework like Express. Initialize your project and install necessary dependencies.

Example commands:

mkdir my-api && cd my-api

npm init -y

npm install express typescript @types/express ts-node --save

Create a tsconfig.json file and an index.ts file with your API code.

Sample index.ts

```typescript

import express from 'express';

const app = express();

app.get('/api', (req, res) => {

res.json({ message: 'Hello from TypeScript API!' });

});

app.listen(3000, () => {

console.log('API running on port 3000');

});

```

Step 2: Containerize the API with Docker

Create a Dockerfile in your project directory:

FROM node:14-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["node", "dist/index.js"]

Build the Docker image:

docker build -t my-typescript-api .

Step 3: Push Image to Container Registry

Push your Docker image to a registry like Docker Hub:

docker tag my-typescript-api yourdockerhubusername/my-typescript-api

docker push yourdockerhubusername/my-typescript-api

Step 4: Deploy Using Helm

Create a Helm chart directory:

helm create my-api-chart

Modify the values.yaml file to specify your Docker image:

```yaml

image:

repository: yourdockerhubusername/my-typescript-api

tag: latest

```

Deploy the Helm chart:

helm install my-api-release ./my-api-chart

Step 5: Verify Deployment

Check the status of your deployment:

kubectl get pods

Access your API through the service's external IP or port forwarding.

Conclusion

This example demonstrates how to deploy a TypeScript API on Kubernetes using Docker for containerization and Helm for deployment management. Adjust the steps to fit your specific environment and application needs for successful deployment.