Skip to content

Instantly share code, notes, and snippets.

@lioneltchami
Created November 20, 2024 03:18
Show Gist options
  • Select an option

  • Save lioneltchami/8a4c6b33550477ab5cc6a4a735d6b309 to your computer and use it in GitHub Desktop.

Select an option

Save lioneltchami/8a4c6b33550477ab5cc6a4a735d6b309 to your computer and use it in GitHub Desktop.

Node.js App with MongoDB and Mongo Express on Azure Kubernetes Service

image

This guide will walk you through deploying a Node.js application with MongoDB and Mongo Express on Azure Kubernetes Service (AKS). We'll break down the process into clear, manageable steps.

Prerequisites

Before you begin, ensure you have:

  1. An active Azure subscription
  2. Azure CLI installed on your local machine
  3. kubectl installed for Kubernetes cluster management
  4. Docker installed for building and pushing container images
  5. Helm installed for package management in Kubernetes

Step-by-Step Deployment Process

1. Set Up Azure Kubernetes Service (AKS)

First, we'll create an AKS cluster in Azure.

# Create a resource group
az group create --name myNodeAppGroup --location eastus

# Create an AKS cluster
az aks create --resource-group myNodeAppGroup --name myNodeAppCluster --node-count 2 --enable-addons monitoring --generate-ssh-keys

# Get credentials for the cluster
az aks get-credentials --resource-group myNodeAppGroup --name myNodeAppCluster

2. Prepare Your Node.js Application

Ensure your Node.js application is ready for containerization. Create a Dockerfile in your project root:

FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]

Build and push your Docker image:

docker build -t yourusername/nodeapp:v1 .
docker push yourusername/nodeapp:v1

3. Deploy MongoDB and Mongo Express

We'll use Helm to deploy MongoDB and Mongo Express.

# Add Bitnami repository
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update

# Install MongoDB
helm install mongodb bitnami/mongodb --set auth.enabled=false

# Install Mongo Express
helm install mongo-express bitnami/mongodb-express --set mongodb.host=mongodb-mongodb.default.svc.cluster.local

4. Deploy Your Node.js Application

Create a deployment file named nodeapp-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nodeapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nodeapp
  template:
    metadata:
      labels:
        app: nodeapp
    spec:
      containers:
      - name: nodeapp
        image: yourusername/nodeapp:v1
        ports:
        - containerPort: 3000
        env:
        - name: MONGODB_URI
          value: "mongodb://mongodb-mongodb.default.svc.cluster.local:27017/myapp"

Apply the deployment:

kubectl apply -f nodeapp-deployment.yaml

5. Expose Your Application

Create a service file named nodeapp-service.yaml:

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

Apply the service:

kubectl apply -f nodeapp-service.yaml

6. Access Your Application

Retrieve the external IP for your Node.js application:

kubectl get service nodeapp-service

Use this IP to access your application in a web browser.

To access Mongo Express, get its external IP:

kubectl get service mongo-express

Monitoring and Management

  • View Pods: kubectl get pods
  • View Services: kubectl get services
  • View Deployments: kubectl get deployments
  • Application Logs: kubectl logs <pod-name>

Scaling Your Application

To scale your Node.js application, use:

kubectl scale deployment nodeapp --replicas=3

Cleaning Up

When you're done, delete the resource group to remove all resources:

az group delete --name myNodeAppGroup --yes --no-wait

This streamlined guide provides a clear path to deploying your Node.js application with MongoDB and Mongo Express on Azure Kubernetes Service. It emphasizes key steps and includes essential commands for deployment, management, and cleanup.

Citations: [1] https://gist.github.com/assets/112948305/0852118c-39ea-49b9-90c5-78157fe70e40

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