MongoDB Deployment
Kubernetes Deployment Overview
Goal: Deploy two applications — MongoDB and Express — in a Kubernetes cluster.
Components Used:
MongoDB pod with internal access
Express deployment with browser access
Kubernetes services: internal for MongoDB, external for Express
ConfigMap and Secret for configuration and credentials
Step 1: MongoDB Deployment
MongoDB Pod Setup
Deployment Configuration:
Kind:
Deployment
Metadata includes:
Name:
MongoDB-deployment
Labels and selectors to match pods
Replica Count: 1
Container Settings:
Image: Latest MongoDB image
Container name:
MongoDB
Port exposed:
27017
(default MongoDB port)
Environment Variables:
MONGO_INITDB_ROOT_USERNAME
: left blank (to be sourced from secret)MONGO_INITDB_ROOT_PASSWORD
: left blank (to be sourced from secret)
Secret Creation for Credentials
File Type:
Secret
Kind:
Secret
Metadata Name:
MongoDB-secret
Type:
Opaque
(default key-value secret type)Data (Base64 encoded):
root-username
: e.g.,username
encoded viaecho -n username | base64
root-password
: e.g.,password
encoded similarly
Referencing Secret in Deployment
Reference Block:
Use
valueFrom
→secretKeyRef
Keys:
Username:
root-username
Password:
root-password
Ensures no plain-text credentials in config files
Improves security by storing sensitive data outside the repo
Step 2: Internal Service for MongoDB
Purpose: Allow internal communication between pods.
Kind:
Service
Name:
MongoDB-service
Selector: Matches deployment labels to connect to MongoDB pod
Ports:
Service port:
27017
Target port:
27017
(matches container port)
Validation
Commands Used:
kubectl get service
kubectl describe service MongoDB-service
Match service endpoint IP with
kubectl get pod -o wide
output
Step 3: Express Deployment
Express Pod Setup
Deployment Configuration:
Kind:
Deployment
Name:
Express-deployment
Image:
mongo-express
Container port:
8081
(default port for mongo-express UI)
Environment Variables Required:
ME_CONFIG_MONGODB_ADMINUSERNAME
ME_CONFIG_MONGODB_ADMINPASSWORD
ME_CONFIG_MONGODB_SERVER
Source of Environment Variables
Credentials: Referenced from the previously created
MongoDB-secret
MongoDB Server URL: Provided via
ConfigMap
Step 4: ConfigMap for MongoDB URL
Kind:
ConfigMap
Name:
MongoDB-configmap
Data:
Key:
database-url
Value:
MongoDB-service
(internal service name of MongoDB)
Referencing in Deployment
ValueFrom Block:
Use
configMapKeyRef
ConfigMap Name:
MongoDB-configmap
Key:
database-url
Step 5: External Service for Express
Purpose: Allow browser-based access to the Express UI.
Kind:
Service
Name:
Express-service
Type:
LoadBalancer
(for external exposure)Selector: Matches Express pod
Ports:
port
:8081
targetPort
:8081
nodePort
:30000
(within the required range 30000–32767 for external exposure)
MiniKube Specific Behavior
External IP shown as "pending" under
kubectl get service
due to local environmentUse command
minikube service Express-service
to:Launch Express UI in browser
Display full external URL (IP + NodePort)
Request Flow Summary
Incoming Browser Request:
Sent to external service (LoadBalancer type)
Routed to Express pod
Express Pod Behavior:
Reads database config and credentials from environment variables
Connects to MongoDB via internal service
MongoDB Pod:
Authenticates request using credentials from secret
Processes request (e.g., create database, display collections)
Key Notes and Tips
Order of Resource Creation Matters:
Secrets and ConfigMaps must exist before referencing them in deployments
Best Practices:
Avoid hardcoding sensitive info in deployment files
Use ConfigMaps and Secrets for clean, secure, modular configurations
Use YAML document separator (
---
) to combine multiple resources in one file
Validation Commands:
kubectl get all
filters by componentkubectl describe
inspects configurations in-depth
Last updated