Deploying NestJS to Kubernetes

By Abdelilah Ommane ยท Backend Developer

Short answer

Containerize the API (you already have a Dockerfile), then define a Kubernetes Deployment (runs N pods), a Service (stable internal IP), and an Ingress (external HTTPS). Apply with kubectl apply -f.

1. Deployment

apiVersion: apps/v1
kind: Deployment
metadata: { name: nest-api }
spec:
  replicas: 3
  selector: { matchLabels: { app: nest-api } }
  template:
    metadata: { labels: { app: nest-api } }
    spec:
      containers:
        - name: nest-api
          image: your-registry/nest-api:1.0.0
          ports: [{ containerPort: 3000 }]
          readinessProbe:
            httpGet: { path: /health, port: 3000 }

2. Service

apiVersion: v1
kind: Service
metadata: { name: nest-api-svc }
spec:
  selector: { app: nest-api }
  ports: [{ port: 80, targetPort: 3000 }]

3. Ingress

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata: { name: nest-api-ingress }
spec:
  rules:
    - host: api.example.com
      http:
        paths:
          - path: / { pathType: Prefix, backend: { service: { name: nest-api-svc, port: { number: 80 } } } }

FAQ

How do I handle config/secrets?

Put connection strings in a Kubernetes Secret and mount them as env vars via envFrom: secretRef โ€” never bake them into the image.

How do rolling updates work?

By default Kubernetes does a rolling update: it brings up new pods and drains old ones gradually, so the API stays available with zero downtime.

← All guides