Deploy an Express/MongoDB Application on k8s
Updated: 03 September 2023
Before reading through this, you may want to read the page about the application being deployed on the Build an Express App that uses MongoDB page
Prerequisites
- Docker
- Minicube
- Minikube k8s cluster
kubectl
installed- Helm and Tiller
Installation
Minikube
Hyper-V
Using minikube with Windows requires us to use Hyper-V as a driver, we can follow the instructions on Jock Reed’s Blog on how configure a new virtual switch, then we can start minikube using this switch as follows:
After which point we can stop minikube
Enable Dynamic Memory from Hyper-V Manager, and then start minikube with:
VirtualBox
We can use VirtualBox as our driver as well with the following
Running Minikube
Note that this may download some files which will take a while, but you will eventually see the following output
Next we can view our minikube dashboard with
Creating a Deployment
We can create a deployment based on a deployment yaml file
For the purpose of this, we will make use of the deployment configurations that are defined in the Build an Express App that uses MongoDB at the Comments App GitHub Repository
To see how the app is constructed and how it communicates with the DB, read the page on Building an Express App that uses Mongo
The Express App is exposed on port 8080
and will speak to the Mongo instance on mongo:27017
Building the Image
Before we can deploy our application we need to build it as a Docker image and push it to a repository, in the case of the Comments App, this will be as follows
From the application directory run
If we do not wish to redeploy our
Deploying on Kubernetes
Once logged into a kubernetes cluster we can make use of the express.yaml
to deploy the express app, and the mongo.yaml
file to deploy Mongo
This will create a deployment as well as a service for both the Express App and Mongo. The deployment configs are as follows
express.yaml
mongo.yaml
Use the App
We can use minikube to View the application
Once on the app we can create a comment, which will take us to the comments view page. When creating a comment a new record is inserted into Mongo, and when viewing them all existing comments are retrieved and displayed