Upload a NodeJS API connected with MongoDB to Kubernetes on Google Cloud Platform.
- NodeJS Express
- MongoDB
- Docker
- Google Cloud Platform
- Kubernetes
First of all, download or clone this repository. It has the same project from CRUD-NodeJS-Swagger-MongoDB, but with some new files:
- Dockerfile: Used by Docker to build a NodeJS API image.
- deployment.yml: Exposes the NodeJS API to internet on Kubernetes.
- googlecloud_ssd.yaml: Creates a SSD storage on Kubernetes.
- mongo-statefulset.yaml: Creates a MongoDB volume on Kubernetes.
Also, the "server.js" was edited to receive a new connection string with MongoDB hosted in the cloud. To follow this guide you must have a basic understanding about Docker and containers. For any doubts, access the docs.
Why to use Google Cloud Platform? Accordingly to ZDNet:
You use a cloud platform such as GCP when you want to build and run an application that can leverage the power of hyperscale data centers in some way: to reach users worldwide, or to borrow sophisticated analytics and AI functions, or to utilize massive data storage, or to take advantage of cost efficiencies. You pay not for the machine but for the resources the machine uses.
Basically, if you want to host your apps in the web and do not want to worry about scaling, volume, security and performance, GCP is a great choice.
What is Kubernetes? As the Kubernetes documentation says:
Kubernetes Engine (GKE) is a managed, production-ready environment for deploying containerized applications (...) enables rapid application development and iteration by making it easy to deploy, update, and manage your applications and services (...) Use routine health checks to detect and replace hung, or crashed, applications inside your deployments (...) autoscaling allows you to handle increased user demand for your services, keeping them available when it matters most.
These are some basic characteristics about Kubernetes. In this guide, you will be able to create a cluster, nodes, pods and a persistent storage to make your services running on Kubernetes. Let's go!
Go to Google Cloud Platform and create a new project.
Access Kubernetes Engine > Clusters and click on "Create Cluster".
Rename the cluster and press on "Create". 3 nodes will be created by default with 3.75 GB each.
Wait for the cluster activation and this will be the result:
Now let's create a MongoDB storage in our cluster. First, install the GCloud CLI in your machine by following these docs. Kubernetes also has its own CLI, so install it here. At the root folder of this project, run the commands below:
Set your project for GCloud.
gcloud config set project nodejs-mongodb-kubernetes
Update GCloud components.
gcloud components update
Set your cluster for GCloud.
gcloud container clusters get-credentials my-cluster --zone us-central1-a
Create a SSD storage on Kubernetes.
kubectl apply -f googlecloud_ssd.yaml
At Storage > Storage classes, a SSD type storage named "fast" will be created.
Create a MongoDB volume inside the "fast" storage.
kubectl apply -f mongo-statefulset.yaml
Check if the stateful set is ready.
kubectl get statefulset
There must be 1 Desired and 1 Current.
Check if the pods are ready.
kubectl get pods
Both of them must be running.
The MongoDB volume will appear at Persistant Volume Claims tab.
A service pod will be created at Services & Ingress.
And at Workloads, a Stateful Set will be created as well.
Connect with the MongoDB instance.
kubectl exec -ti mongo-0 mongo
Inside the prompt, initialize the database.
rs.initiate()
Open the database configuration.
rs.conf()
Notice the host as "mongo-0:27017". It's similar to the connection string inside the "server.js" file.
Now, let's upload our NodeJS API to the cloud.
Build an image from this project with the name "gcr.io/nodejs-mongodb-kubernetes/nodejs" and tag "v1". Everytime you need to update the image, change only the tag.
docker build -t gcr.io/nodejs-mongodb-kubernetes/nodejs:v1 .
Upload the image to Google Cloud Platform.
gcloud docker -- push gcr.io/nodejs-mongodb-kubernetes/nodejs:v1
Run the image on Kubernetes.
kubectl run nodejs --image=gcr.io/nodejs-mongodb-kubernetes/nodejs:v1 --port 3000
Check if there is a pod running the API.
kubectl get pods
Expose the API to internet on port 3000.
kubectl expose deployment nodejs --type=LoadBalancer --port 3000 --target-port 3000
Finally, a public IP will be created for the NodeJS API on Google Cloud Platform.
Access it and the Swagger will be available.
Now, have fun with your CRUD running in the cloud ^_^.
For more informations about deployment of projects to Kubernetes and Google Cloud Platform, read these articles:
https://cloud.google.com/kubernetes-engine/docs/tutorials/hello-app
https://www.sitepoint.com/kubernetes-deploy-node-js-docker-app/