‘Deploy MongoDB to Kubernetes

Abdelmoumen
3 min readDec 20, 2021

Hello,in this article i will show you how i deployed a MongoDB and Mongo-express to Kubernetes, before we start let’s do a little bit of theories………

what is Kubernetes? Why we need to use it in the first place ? why we don’t just run containers in the production servers and we are done? is this not enough ?

according to the official documentation of Kubernetes “also known as K8s, is an open-source system for automating deployment, scaling, and management of containerized applications.” as we know with docker for example we have solved a lot problems by isolating applications from each other ,so each container has it’s own file system, not only that ,also we can run our docker images and deploy them anywhere docker is installed ,but in production stage we will encounter a lot of problems like Scaling and High-availability Issues . Kubernetes make the scaling automatic and provides reliability. If a container or entire node goes down, resources or a single process will be rescheduled by Kubernetes on a healthy node.
there are more advantage of using k8s but i will not list them all because this is not our subject.

So let’s come back to how to deploy a MongoDB to Kubernetes . in this tutorial we will use Yaml format so if you are not familiar with it do not worry, it’s intuitive.
First we need to create a Deployment for MongoDB,Deployment is used to tell Kubernetes how to create or modify instances of the pods that hold a containerized application

as you can see here we have made a reference to a secret file mongodb-secret` ,usually we use secrets for sensitive data ,like in our case we have credentials for mongoDB.
this is the structure of our secret file

now we need to apply our configuration to run our pod by using these commands
kubectl apply -f mongo-secret.yaml after this we run kubectl apply -f mongo-deployment.yaml , notice here the order is important the secret before the deployment .
after , in order to make our mongodb pod accessible inside the cluster we need to create an internal service

as you can see here in the selector section we gave it app:mongodb
which we have defined previously in the deployment file. now we are done with MongoDB pod
So now we are going to deploy mongo-express which is an admin interface for MongoDB ,so just like we did before with mongodb ,we will create a Deployment and service but now the only difference is that we have to create an external service in order to access the mongo-express pod out of the cluster .

as you can see in the env section we defined 3 environment variables ,2 of them are same as the mongodb pod’s environement variables,so we bring them from the same secret which we have deployed before,except the ME_CONFIG_MONGODB_SERVER` variable which reference to the internal service of mongodb pod ,so here we will create another configMap called,Actually we can set the value of ME_CONFIG_MONGODB_SERVER directly, but we use configMap just to centralize the configuration

Now we move on to create a An External Service for the mongo-express pod

notice here that just like a Internal Service but with 2 additional things
type:LoadBalancer and nodePort`:300000`

after applying all of these configuration we will be able to reach the mongodb from the browser

That’s it friends, the code used here is available on my github repo
https://github.com/amineabdelmoumen/mongo-k8sConfig

advises or critics are always welcome,Thank you.

--

--

Abdelmoumen

Computer Science Student Looking for an End of Studies Internship