Skip to content

Anas-Altaf/Kubernete-Nodejs-Mongo-Deployment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kubernetes Node.js + MongoDB Deployment

Deploying a Node.js web app with MongoDB on Kubernetes. The app connects to Mongo using credentials stored in a Secret and the service URL stored in a ConfigMap — instead of hardcoding anything in the app config.


What's in here

mongo-config.yaml   → ConfigMap  : holds the mongo service URL
mongo-secret.yaml   → Secret     : holds mongo credentials (base64 encoded)
mongo.yaml          → Deployment + Service for MongoDB
webapp.yaml         → Deployment + Service for the Node.js app

How it's wired

Configs and Secrets first — before deploying anything, the ConfigMap and Secret need to exist in the cluster because both Deployments reference them.

mongo-config.yaml Just one key: mongo-url: mongo-service. This is the internal DNS name Kubernetes assigns to the Mongo Service. The webapp reads this to know where to connect.

mongo-secret.yaml Stores mongo-user and mongo-password as base64 encoded values. Kubernetes Secrets use Opaque type for arbitrary key-value data like this.

mongo.yaml MongoDB Deployment running mongo:8.2 on port 27017, with 1 replica. The root username and password are pulled directly from the Secret using secretKeyRef — so no plain text credentials in the deployment config. The Service (mongo-service) is ClusterIP by default, meaning it's only reachable inside the cluster.

webapp.yaml Node.js app (nanajanashia/k8s-demo-app:v1.0) running on port 3000. It gets:

  • USER_NAME and USER_PWD from the Secret (same keys mongo uses for root credentials)
  • DB_URL from the ConfigMap (points to mongo-service)

The Service is NodePort, exposed on port 30100 — so you can hit the app from outside the cluster at <NodeIP>:30100.


Apply order

Secrets and ConfigMap must be applied before the Deployments, otherwise the pods will fail to start.

kubectl apply -f mongo-config.yaml
kubectl apply -f mongo-secret.yaml
kubectl apply -f mongo.yaml
kubectl apply -f webapp.yaml

Check everything's running

kubectl get pods
kubectl get services

Access the app at http://<NodeIP>:30100

About

Deployed a Nodejs App using Kubernetes with Mongo, Practically approach of using ConfigMaps, Secrets encoded with base64, and Exposing Service using Noedport and Connecting Mongoab with cluserIP.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors