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:
- Azure Subscription: Ensure you have an active Azure subscription.
- Azure CLI: Install the Azure CLI on your local machine.
- Kubectl: Install kubectl for interacting with your AKS cluster.
- Docker: Install Docker to build your application images.
- Helm: Install Helm for managing Kubernetes applications.
-
Create Resource Group:
az group create --name myResourceGroup --location eastus
-
Create AKS Cluster:
az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 1 --enable-addons monitoring --generate-ssh-keys
-
Get AKS Credentials:
az aks get-credentials --resource-group myResourceGroup --name myAKSCluster
-
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" ]
-
Build and Push Docker Image:
docker build -t myusername/myapp:v1 . docker push myusername/myapp:v1
-
Add Bitnami Repository:
helm repo add bitnami https://charts.bitnami.com/bitnami helm repo update
-
Install MongoDB:
helm install my-mongodb bitnami/mongodb
-
Install Mongo Express:
helm install my-mongo-express bitnami/mongodb-express --set mongodb.host=my-mongodb-mongodb.default.svc.cluster.local
-
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"
-
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
-
Apply Deployment and Service:
kubectl apply -f deployment.yaml kubectl apply -f service.yaml
-
Get External IP:
kubectl get services my-node-app-service
-
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.
- 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.