🚀 Deploying a NestJS App with MongoDB on GCP using Docker and Kubernetes

Step 1: Set Up GCP

1️⃣ Create a GCP Account

2️⃣ Enable Required APIs

  • Navigate to APIs & Services > Library and enable the following:
    • Kubernetes Engine API
    • Cloud Build API
    • Container Registry API
    • Compute Engine API

3️⃣ Install Google Cloud SDK

curl https://sdk.cloud.google.com | bash
exec -l $SHELL
gcloud init

4️⃣ Authenticate and Set Your Project

gcloud auth login
gcloud config set project YOUR_PROJECT_ID

Step 2: Install Dependencies

Ensure you have the following installed:

  • Docker: Download
  • Kubectl: Download
  • Google Cloud SDK: Installed in the previous step
  • Node.js and NestJS (already installed for your project)

Step 3: Dockerize Your NestJS Application

1️⃣ Create a Dockerfile

# Use official Node.js image
FROM node:18

# Set working directory
WORKDIR /app

# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install

# Copy the rest of the application
COPY . .

# Build the NestJS app
RUN npm run build

# Expose the port
EXPOSE 3000

# Start the application
CMD ["npm", "run", "start"]

2️⃣ Create a .dockerignore file

node_modules
dist
.env

3️⃣ Build and Tag the Docker Image

docker build -t gcr.io/YOUR_PROJECT_ID/nestjs-app .

4️⃣ Push the Image to Google Container Registry (GCR)

gcloud auth configure-docker
docker push gcr.io/YOUR_PROJECT_ID/nestjs-app

Step 4: Set Up MongoDB on GCP

Option 1️⃣: Use MongoDB Atlas (Recommended)

  • Sign up at MongoDB Atlas.
  • Create a free cluster and get the connection string.
  • Update your .env file:
DATABASE_URL=mongodb+srv://username:[email protected]/dbname

Option 2️⃣: Deploy MongoDB on GKE

Create a Persistent Volume

mongo-pv.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mongo-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Deploy MongoDB

mongo-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongo
  template:
    metadata:
      labels:
        app: mongo
    spec:
      containers:
        - name: mongo
          image: mongo:latest
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: mongo-storage
              mountPath: /data/db
      volumes:
        - name: mongo-storage
          persistentVolumeClaim:
            claimName: mongo-pvc

Expose MongoDB

mongo-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: mongo
spec:
  selector:
    app: mongo
  ports:
    - protocol: TCP
      port: 27017
      targetPort: 27017

Apply MongoDB Configurations

kubectl apply -f mongo-pv.yaml
kubectl apply -f mongo-deployment.yaml
kubectl apply -f mongo-service.yaml

Step 5: Deploy NestJS to GKE

1️⃣ Create a Kubernetes Deployment

nestjs-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nestjs-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nestjs-app
  template:
    metadata:
      labels:
        app: nestjs-app
    spec:
      containers:
        - name: nestjs-app
          image: gcr.io/YOUR_PROJECT_ID/nestjs-app
          ports:
            - containerPort: 3000
          env:
            - name: DATABASE_URL
              value: "mongodb://mongo:27017/mydatabase"

2️⃣ Expose the Application

nestjs-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nestjs-service
spec:
  type: LoadBalancer
  selector:
    app: nestjs-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000

3️⃣ Deploy the NestJS App

kubectl apply -f nestjs-deployment.yaml
kubectl apply -f nestjs-service.yaml

Step 6: Configure GKE (Google Kubernetes Engine)

1️⃣ Create a Kubernetes Cluster

gcloud container clusters create nestjs-cluster --num-nodes=2

2️⃣ Get Cluster Credentials

gcloud container clusters get-credentials nestjs-cluster

3️⃣ Deploy Kubernetes Resources

kubectl apply -f mongo-deployment.yaml
kubectl apply -f mongo-service.yaml
kubectl apply -f nestjs-deployment.yaml
kubectl apply -f nestjs-service.yaml

4️⃣ Get the External IP of the Application

kubectl get service nestjs-service

Use the EXTERNAL-IP to access your NestJS API.

Step 7: Set Up Domain and SSL (Optional)

  • Use Google Cloud Load Balancer for a custom domain.
  • Use Cert-Manager for HTTPS with Let's Encrypt.

Step 8: Monitor and Scale

Check Logs

kubectl logs -f deployment/nestjs-app

Scale Application

kubectl scale deployment nestjs-app --replicas=3

🎉 Congratulations! Your NestJS app is now running on GCP with MongoDB using Docker and Kubernetes. 🚀