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.
Before you begin, ensure you have:
- An active Azure subscription
- Azure CLI installed on your local machine
- kubectl installed for Kubernetes cluster management
- Docker installed for building and pushing container images
- Helm installed for package management in Kubernetes
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 myNodeAppClusterEnsure 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:v1We'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.localCreate 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.yamlCreate 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: LoadBalancerApply the service:
kubectl apply -f nodeapp-service.yamlRetrieve the external IP for your Node.js application:
kubectl get service nodeapp-serviceUse this IP to access your application in a web browser.
To access Mongo Express, get its external IP:
kubectl get service mongo-express- View Pods:
kubectl get pods - View Services:
kubectl get services - View Deployments:
kubectl get deployments - Application Logs:
kubectl logs <pod-name>
To scale your Node.js application, use:
kubectl scale deployment nodeapp --replicas=3When you're done, delete the resource group to remove all resources:
az group delete --name myNodeAppGroup --yes --no-waitThis 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
