Back to projects
This project demonstrates deploying a MongoDB database and Mongo Express web client on a local Kubernetes cluster using Minikube. It showcases how to manage configurations with ConfigMaps, secure credentials with Secrets, and expose applications through Kubernetes Services.
Firstly, we need to install Minikube for our project to run. Minikube is a tool that allows us to create a local Kubernetes cluster, which we’ll use to deploy MongoDB and Mongo Express as part of our demo project.
Next, we start our local Kubernetes cluster using Minikube. From the terminal with administrator access, we run minikube start and choose Docker as the container runtime for our demo project.
We need to create a mongodb deployment which will also create the underlying ReplicaSet and pods in order for mongodb container to be able to run and communicate internally with our mongo-express deployment later
Firstly we create a mongodb-secret.yaml file which contains our mongodb root username and password. We abstract these credentials in this secret file for security purposes
We apply this file by running the command:
Now we create the mongodb-deployment.yaml file which contains the blueprint for our mongodb deployment, we use the following params:
We apply this mongodb-deployment.yaml file:
Pods in kubernetes are ephemeral by default, meaning they get a new ip address whenever they die and get re-created... we need a stable static ip address so that other clients inside our cluster can communicate with our mongodb pods.
And for that we create a mongodb-service.yaml file with the following attributes:
We apply this service:
ConfigMap files are similar to Secret files by the fact that they both store configuration variables, env variables etc... but ConfigMap files have less strict security when dealing with them, for example Secret files are usually not pushed in version control systems like git etc...
Here we define just one property which is database_url which we set it to have mongodb-service as a value... This is simpler because we didnt need to specifiy the ip address of the mongodb-service and instead we let kubernetes handle DNS resolution for us by replacing mongodb-service by its actual internal ip address
We will later reference this variable in mongo-express deployment in order for its pods to be able to connect to mongodb database
We apply this ConfigMap file:
We use similar patterns to what we used when creating the mongodb-deployment file plus adding the ME_CONFIG_MONGODB_SERVER container variable needed for Mongodb Express to access our mongodb database
We apply this deployment by running:
This mongoexpress-service.yaml file will expose our mongoexpress-service pods to be able to be accessed from outside the cluster through our web browser. We used the following attributes to make that happen:
We apply this service
