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:
1minikube start --vm-driver hyperv --hyperv-virtual-switch "Primary Virtual Switch"
After which point we can stop minikube
1minikube stop
Enable Dynamic Memory from Hyper-V Manager, and then start minikube with:
1minikube start
VirtualBox
We can use VirtualBox as our driver as well with the following
1minikbe start --vm-driver virtualbox
Running Minikube
1minikube start
Note that this may download some files which will take a while, but you will eventually see the following output
1Starting local Kubernetes v1.10.0 cluster...2Starting VM...3Downloading Minikube ISO4 178.87 MB / 178.87 MB [============================================] 100.00% 0s5Getting VM IP address...6E1215 14:28:43.740427 9968 start.go:210] Error parsing version semver: Version string empty7Moving files into cluster...8Downloading kubelet v1.10.09Downloading kubeadm v1.10.010Finished Downloading kubeadm v1.10.011Finished Downloading kubelet v1.10.012Setting up certs...13Connecting to cluster...14Setting up kubeconfig...15Stopping extra container runtimes...16Starting cluster components...17Verifying kubelet health ...18Verifying apiserver health ...Kubectl is now configured to use the cluster.19Loading cached images from config file.20
21
22Everything looks great. Please enjoy minikube!
Next we can view our minikube dashboard with
1minikube 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
1docker build -t <USERNAME>/comments-app2docker 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
1kubectl create -f express.yaml2kubectl 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
1apiVersion: extensions/v1beta12kind: Deployment3metadata:4 annotations:5 deployment.kubernetes.io/revision: '1'6 labels:7 app: comments-app8 name: comments-app9spec:10 replicas: 111 selector:12 matchLabels:13 app: comments-app14 template:15 metadata:16 labels:17 app: comments-app18 name: comments-app19 spec:20 containers:21 - image: <USERNAME>/comments-app22 imagePullPolicy: Always23 name: comments-app24
25---26apiVersion: v127kind: Service28metadata:29 labels:30 app: comments-app31 name: comments-app32spec:33 ports:34 - name: tcp-8080-8080-comments-app35 nodePort: 3001636 port: 808037 protocol: TCP38 targetPort: 808039 selector:40 app: comments-app41 type: LoadBalancer
mongo.yaml
1apiVersion: v12kind: Service3metadata:4 name: mongo5 labels:6 run: mongo7spec:8 ports:9 - port: 2701710 targetPort: 2701711 protocol: TCP12 selector:13 run: mongo14
15---16apiVersion: extensions/v1beta117kind: Deployment18metadata:19 name: mongo20spec:21 template:22 metadata:23 labels:24 run: mongo25 spec:26 containers:27 - name: mongo28 image: mongo29 ports:30 - containerPort: 27017
Use the App
We can use minikube to View the application
1minikube 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