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:

Terminal window
1
minikube start --vm-driver hyperv --hyperv-virtual-switch "Primary Virtual Switch"

After which point we can stop minikube

Terminal window
1
minikube stop

Enable Dynamic Memory from Hyper-V Manager, and then start minikube with:

Terminal window
1
minikube start

VirtualBox

We can use VirtualBox as our driver as well with the following

Terminal window
1
minikbe start --vm-driver virtualbox

Running Minikube

Terminal window
1
minikube start

Note that this may download some files which will take a while, but you will eventually see the following output

1
Starting local Kubernetes v1.10.0 cluster...
2
Starting VM...
3
Downloading Minikube ISO
4
178.87 MB / 178.87 MB [============================================] 100.00% 0s
5
Getting VM IP address...
6
E1215 14:28:43.740427 9968 start.go:210] Error parsing version semver: Version string empty
7
Moving files into cluster...
8
Downloading kubelet v1.10.0
9
Downloading kubeadm v1.10.0
10
Finished Downloading kubeadm v1.10.0
11
Finished Downloading kubelet v1.10.0
12
Setting up certs...
13
Connecting to cluster...
14
Setting up kubeconfig...
15
Stopping extra container runtimes...
16
Starting cluster components...
17
Verifying kubelet health ...
18
Verifying apiserver health ...Kubectl is now configured to use the cluster.
19
Loading cached images from config file.
20
21
22
Everything looks great. Please enjoy minikube!

Next we can view our minikube dashboard with

Terminal window
1
minikube dashboard

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

Terminal window
1
docker build -t <USERNAME>/comments-app
2
docker push

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

1
kubectl create -f express.yaml
2
kubectl create -f mongo.yaml

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

1
apiVersion: extensions/v1beta1
2
kind: Deployment
3
metadata:
4
annotations:
5
deployment.kubernetes.io/revision: '1'
6
labels:
7
app: comments-app
8
name: comments-app
9
spec:
10
replicas: 1
11
selector:
12
matchLabels:
13
app: comments-app
14
template:
15
metadata:
16
labels:
17
app: comments-app
18
name: comments-app
19
spec:
20
containers:
21
- image: <USERNAME>/comments-app
22
imagePullPolicy: Always
23
name: comments-app
24
25
---
26
apiVersion: v1
27
kind: Service
28
metadata:
29
labels:
30
app: comments-app
31
name: comments-app
32
spec:
33
ports:
34
- name: tcp-8080-8080-comments-app
35
nodePort: 30016
36
port: 8080
37
protocol: TCP
38
targetPort: 8080
39
selector:
40
app: comments-app
41
type: LoadBalancer

mongo.yaml

1
apiVersion: v1
2
kind: Service
3
metadata:
4
name: mongo
5
labels:
6
run: mongo
7
spec:
8
ports:
9
- port: 27017
10
targetPort: 27017
11
protocol: TCP
12
selector:
13
run: mongo
14
15
---
16
apiVersion: extensions/v1beta1
17
kind: Deployment
18
metadata:
19
name: mongo
20
spec:
21
template:
22
metadata:
23
labels:
24
run: mongo
25
spec:
26
containers:
27
- name: mongo
28
image: mongo
29
ports:
30
- containerPort: 27017

Use the App

We can use minikube to View the application

Terminal window
1
minikube service comments-app

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