Kubernetes Part 4

POC1: Deploy Mongo, DB and Mongo Express

I am going to deploy 2 applications, MongoDB and Mongo Express. So,  first we will create a Mongo DB pod and to talk to that pod we need a service and We are going to create an internal service which means that no external requests are allowed to the pod. only components inside the same cluster can talk to it .

Then We are gonna create a Mongo Express deployment. We need a database URL of MongoDB So,  that Mongo Express can connect to it. And need credentials. So,  username and password of the database So,  the way we can pass this information to Mongo Express deployment is through its deployment Configuration file through environmental variables.

So,  We are going to create a config map that contains database URL and we are going to create a secret that contains the credentials and We are going to reference both inside of that deployment file. So,  once we have that set up, We need Mongo express to be accessible through a browser. To do that, We are going to create an external service that will allow external requests to talk to the pod. So,  the URL will be HTTP , IP address of the node and the service port.

The request comes from the browser and it goes to the external service of the mongo express which will then forward it to the mongo express pod the pod will then connect to internal service of MongoDB that’s the database URL and it will forward it then to MongoDB pod it will authenticate The request using the credentials.

Now let’s go and create this whole setup using Kubernetes configuration files.

MongoDB Deployment :

default port of MongoDB container is 27017  we are going to use environmental variables the root username and root password .

 But remember, simply storing the data in a secret component does not automatically make it secure. There are built in mechanisms like encryption for basic security which are not enabled by default.

we must create secret before the deployment. If We are going to reference the secret inside of deployment So,  the order of creation matters.

we put the service configuration in the same yaml file. So,  if we put 3 dashes that’s basically a syntax for document separation in yaml So,  a new document is starting So,   I’m going to put both deployment and service in one configuration file because they usually belong together.

So,  now the next step We are going to create Mongo, Express deployment, and service and also,  an external configuration where We are going to put the database URL form on going to be.

how to create external service

Is by doing two things. So,  in the specification section. So,  below the selector. put a type. In a type of this external service is load balancer.
So,  another thing that We are going to do here to make this service external is right here We are going to provide third port. And this is going to be called node port.
And what this is the port where this external IP address will be open. So,  this will be the port that I’ll have to put in the browser to access this service.
And this node port  has a range, and that range is between 30000 and 32767.

cluster IP will give the service an internal IP address, which is this one right here. So,  this is an internal IP address of the service and load balancer will also,  give service an internal IP address. But in addition to that it will also,  give the service an external IP address where the external requests will be coming from and here it says pending because We are in minikube and it works a little bit differently. Regular Kubernetes setup here. You would also,  see an actual IP address, a public one, and this is another difference because  with internal IP address you just have port for that IP address with both internal and external IP addresses. You have ports for both and that is why we had to define third port which was for the external IP address.

As I said, pending means that it does not have the external IP address yet. So,  in Minikube the way to do that is using the command.

for example, I am going to create a new database, let us call it test DB, whatever, and I’m going to create a request. What just happened in background is that this request landed with The external service of Mongo Express which then forwarded it to the Mongo Express pod And the Mongo Express pod connected to the MongoDB service an internal service and MongoDB service then forwarded that request finally to the MongoDB pod and then all the way back and we have the changes here. So,  that’s how you deploy a simple application set up in a Kubernetes cluster.