This project demonstrates how to deploy a Node.js application on Kubernetes and connect it to a MongoDB database.
Kubernetes-App-Deployment.mov
- Docker
- Kubernetes cluster (Minikube for local development)
- kubectl
- Node.js
- npm
.
├── app/
│ ├── index.js
│ └── package.json
├── k8s/
│ ├── mongodb-configmap.yaml
│ ├── mongodb-secret.yaml
│ ├── mongodb-deployment.yaml
│ ├── nodejs-deployment.yaml
│ └── node-app-service.yaml
├── Dockerfile
└── README.md
Create the following YAML files in the k8s/
directory:
mongodb-configmap.yaml
: ConfigMap for MongoDB connection stringmongodb-secret.yaml
: Secret for MongoDB credentialsmongodb-deployment.yaml
: Deployment and Service for MongoDBnodejs-deployment.yaml
: Deployment for Node.js applicationnode-app-service.yaml
: Service for Node.js application
-
Apply the MongoDB Secret:
kubectl apply -f k8s/mongodb-secret.yaml
-
Apply the MongoDB ConfigMap:
kubectl apply -f k8s/mongodb-configmap.yaml
-
Deploy MongoDB:
kubectl apply -f k8s/mongodb-deployment.yaml
-
Update your
index.js
to use the MongoDB connection string from the environment:const mongoUri = process.env.database_url; mongoose .connect(mongoUri, { useNewUrlParser: true, useUnifiedTopology: true }) .then(() => console.log('Connected to MongoDB')) .catch((err) => console.error('Failed to connect to MongoDB:', err));
-
Create a
Dockerfile
in the project root:FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["node", "index.js"]
-
Build the Docker image:
docker build -t your-username/node-web-app .
-
Push the image to Docker Hub:
docker push your-username/node-web-app
-
Update
nodejs-deployment.yaml
with your Docker image:spec: containers: - name: nodejs image: your-username/node-app:latest
-
Apply the Node.js Deployment:
kubectl apply -f k8s/nodejs-deployment.yaml
-
Apply the Node.js Service:
kubectl apply -f k8s/nodejs-service.yaml
Check the status of your deployments:
kubectl get deployments
kubectl get pods
kubectl get services
For Minikube, use the following command to get the service URL:
minikube service nodejs-service
apiVersion: v1
kind: ConfigMap
metadata:
name: mongodb-configmap
data:
database_url: mongodb://$(MONGO_USER):$(MONGO_PASSWORD)@mongodb-service:27017/?authSource=admin
apiVersion: v1
kind: Secret
metadata:
name: mongodb-secret
type: Opaque
stringData:
mongo-root-username: your_username_here
mongo-root-password: your_password_here
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-deployment
labels:
app: mongodb
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: mongo
ports:
- containerPort: 27017
env:
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-username
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-password
---
apiVersion: v1
kind: Service
metadata:
name: mongodb-service
spec:
selector:
app: mongodb
ports:
- protocol: TCP
port: 27017
targetPort: 27017
apiVersion: apps/v1
kind: Deployment
metadata:
name: nodejs-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nodejs
template:
metadata:
labels:
app: nodejs
spec:
containers:
- name: nodejs
image: your-username/node-app:latest
ports:
- containerPort: 3000
envFrom:
- configMapRef:
name: mongodb-configmap
env:
- name: MONGO_USER
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-username
- name: MONGO_PASSWORD
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-password
apiVersion: v1
kind: Service
metadata:
name: node-app-service
spec:
selector:
app: nodejs
type: LoadBalancer
ports:
- protocol: TCP
port: 80
targetPort: 3000
This project demonstrates how to set up Kubernetes configurations, deploy MongoDB, containerize a Node.js application, and deploy it on Kubernetes with a connection to the MongoDB database. By following these steps, you can create a scalable and manageable deployment of your Node.js application.
This README provides clear instructions and a detailed walkthrough for deploying a Node.js application with MongoDB on Kubernetes.