Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NotHarshhaa/aa9d698d7472142f568e54d5a7bdcf3f to your computer and use it in GitHub Desktop.
Save NotHarshhaa/aa9d698d7472142f568e54d5a7bdcf3f to your computer and use it in GitHub Desktop.

image

Deploy Node js App with Mongo DB and Mongo Express Using AKS

Deploying a Node.js application with MongoDB and Mongo Express on Azure Kubernetes Service (AKS) involves several steps. Here's a detailed guide to help you through the process:

Prerequisites

  1. Azure Subscription: Ensure you have an active Azure subscription.
  2. Azure CLI: Install the Azure CLI on your local machine.
  3. Kubectl: Install kubectl for interacting with your AKS cluster.
  4. Docker: Install Docker to build your application images.
  5. Helm: Install Helm for managing Kubernetes applications.

Steps to Deploy

1. Set Up AKS

  1. Create Resource Group:

    az group create --name myResourceGroup --location eastus
  2. Create AKS Cluster:

    az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 1 --enable-addons monitoring --generate-ssh-keys
  3. Get AKS Credentials:

    az aks get-credentials --resource-group myResourceGroup --name myAKSCluster

2. Create a Docker Image for Your Node.js Application

  1. Dockerfile: Create a Dockerfile for your Node.js application.

    FROM node:14
    
    # Create app directory
    WORKDIR /usr/src/app
    
    # Install app dependencies
    COPY package*.json ./
    RUN npm install
    
    # Bundle app source
    COPY . .
    
    # Expose port and start application
    EXPOSE 3000
    CMD [ "node", "app.js" ]
  2. Build and Push Docker Image:

    docker build -t myusername/myapp:v1 .
    docker push myusername/myapp:v1

3. Set Up MongoDB and Mongo Express with Helm

  1. Add Bitnami Repository:

    helm repo add bitnami https://charts.bitnami.com/bitnami
    helm repo update
  2. Install MongoDB:

    helm install my-mongodb bitnami/mongodb
  3. Install Mongo Express:

    helm install my-mongo-express bitnami/mongodb-express --set mongodb.host=my-mongodb-mongodb.default.svc.cluster.local

4. Deploy Node.js Application to AKS

  1. Create a Deployment YAML File: deployment.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-node-app
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: my-node-app
      template:
        metadata:
          labels:
            app: my-node-app
        spec:
          containers:
          - name: my-node-app
            image: myusername/myapp:v1
            ports:
            - containerPort: 3000
            env:
            - name: MONGO_URI
              value: "mongodb://my-mongodb-mongodb.default.svc.cluster.local:27017/mydatabase"
  2. Create a Service YAML File: service.yaml

    apiVersion: v1
    kind: Service
    metadata:
      name: my-node-app-service
    spec:
      selector:
        app: my-node-app
      ports:
        - protocol: TCP
          port: 80
          targetPort: 3000
      type: LoadBalancer
  3. Apply Deployment and Service:

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

5. Access Your Application

  1. Get External IP:

    kubectl get services my-node-app-service
  2. Access Mongo Express:

    kubectl get services my-mongo-express

Use the external IP address obtained from the above commands to access your Node.js application and Mongo Express in the browser.

Summary

  • AKS Setup: Create and configure the AKS cluster.
  • Docker Image: Build and push the Docker image for your Node.js application.
  • MongoDB & Mongo Express: Deploy MongoDB and Mongo Express using Helm.
  • Deployment: Deploy the Node.js application and expose it using a LoadBalancer service.

Following these steps, you will have your Node.js application running on AKS, connected to MongoDB, with Mongo Express available for database management.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment