Deploy Mern App on Kubernetes:

Ghazanfar Ali
4 min readSep 2, 2023

--

We will be using full stack MERN app and that app will be deploy on kubernetes, the best part is that you do need to have full stack MERN app in fact we are not gonna write any app because containers are available for this from official people like MongoDB. We will be using mongo-express as our web app, this is the application that interacts with mondo-db database.

We should have install minikube and kubectl in our local system for this project.

Minikube:

Kubectl Version:

Lets start!

First we will write secret file for our mongo-db:

We will use base64 encode to encode the mongo db user and password that will be use in secret file:

Use this user and password in mongo secret file:

The next thing is the config, this is the thing where actually magics happens, let suppose you are currently hosting your entire database in kubernetes but one day you decide to take it outside of kubernetes at that time these config maps make your life easier.

Lets create a config map file:

For now we will not put the mongo url because we have not created it mongo deployment file yet, so let create it first:

Deployment is like you do not need to worry how many pods or worker node will be there just tell me i will manage it.

Now we will create kind:services , we can also create separate file for it but we will go in same mongo-app.yaml file:

In kubernetes our pods keep dying and new pods replaces them, but when new pod came up it gets new ip, once we enable or stick a service to pod it so next time request talk to service not with pod.

Also use service name: mongo-service in mongo.config file:

Now we have to deploy our web app we will use mongo-express docker image for this:

We will write the yaml file for web app and service also:

We also have to make nodeport for this webapp as this will interact with outside world also:

Now time to start minikube:

You should have virtual box or docker desktop in your system so that minikube will run inside that:

Now we will use kubectl to interact with kubernetes cluster:

First we will apply conf of secret file:

Then config file:

Mongo-app:

Web-app:

We have two ready pods now because 1/1 can be seen:

Minikube only creates single node:

services:

Now you can excess the service through minikube:

At last delete all the stuff:

--

--

Ghazanfar Ali
Ghazanfar Ali

Written by Ghazanfar Ali

I write technical blogs on DevOps and AWS, Azure and GCP

No responses yet