![image](https://gist.github.com/assets/112948305/0852118c-39ea-49b9-90c5-78157fe70e40) ## 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**: ```bash az group create --name myResourceGroup --location eastus ``` 2. **Create AKS Cluster**: ```bash az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 1 --enable-addons monitoring --generate-ssh-keys ``` 3. **Get AKS Credentials**: ```bash 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. ```Dockerfile 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**: ```bash docker build -t myusername/myapp:v1 . docker push myusername/myapp:v1 ``` #### 3. Set Up MongoDB and Mongo Express with Helm 1. **Add Bitnami Repository**: ```bash helm repo add bitnami https://charts.bitnami.com/bitnami helm repo update ``` 2. **Install MongoDB**: ```bash helm install my-mongodb bitnami/mongodb ``` 3. **Install Mongo Express**: ```bash 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` ```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` ```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**: ```bash kubectl apply -f deployment.yaml kubectl apply -f service.yaml ``` #### 5. Access Your Application 1. **Get External IP**: ```bash kubectl get services my-node-app-service ``` 2. **Access Mongo Express**: ```bash 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. ### Credits: [H A R S H H A A](https://github.com/NotHarshhaa)