Deploy a MEAN Web App with Google Kubernetes Engine, Portworx
In this tutorial, we will deploy and manage a Node.js web application and a MongoDB database in Google Kubernetes Engine. To achieve high availability for MongoDB, we will use a Portworx storage cluster deployed on GKE.
Mar 1st, 2019 9:12am by
Launching a GKE Cluster
Let’s launch a three node GKE cluster based on Ubuntu OS with an SSD-based disk of 50GB attached to each node. Replace the PROJECT with your own GCP project id.
export PROJECT=’<Your GCP Project ID>’
gcloud container --project $PROJECT clusters create "tns-demo" \
--zone "asia-south1-a" \
--username "admin" \
--cluster-version "1.11.7-gke.4" \
--machine-type "n1-standard-4" \
--image-type "UBUNTU" \
--disk-type "pd-ssd" \
--disk-size "50" \
--scopes "https://www.googleapis.com/auth/compute","https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" \
--num-nodes "3" \
--enable-cloud-logging \
--enable-cloud-monitoring \
--network "default" \
--addons HorizontalPodAutoscaling,HttpLoadBalancing,KubernetesDashboard
gcloud container clusters get-credentials tns-demo \
--zone asia-south1-a \
--project $PROJECT
kubectl create clusterrolebinding cluster-admin-binding \
--clusterrole cluster-admin \
--user $(gcloud config get-value account)
Installing Portworx Storage Cluster
Portworx is installed as a DaemonSet on each node of GKE. We can install it by generating the YAML spec through an online tool. Visit the Portworx documentation page to get started. Get the version of Kubernetes with the following command. The installation tool needs to know the exact version of the distribution.
kubectl version --short | awk -Fv '/Server Version: / {print $3}'
Under the Storage tab, choose GKE and populate the information for the spec. We are choosing an SSD disk with 20GB as dedicated block storage for Portworx. Since we have three nodes, we will get aggregate storage of 60GB.
Choose defaults for the Network tab and click Next.
In the last tab, choose Google Kubernetes Engine and click Finish.
We are ready to install Portworx based on the generated specification. You can either download the spec or copy it.
Switch to the terminal and run the command copied from the spec generator. It will take a few minutes for Portworx cluster to get installed.
Verify the cluster by checking the Portworx Pods running in kube-system namespace. All the Pods should be running.
Deploying MongoDB
We will create a StorageClass for Portworx with a replication factor of 3. This ensures that the data is redundantly available on multiple nodes.
kind: StorageClass
apiVersion: storage.k8s.io/v1beta1
metadata:
name: px-sc
provisioner: kubernetes.io/portworx-volume
parameters:
repl: "3"
kubectl create -f px-sc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: px-mongo-pvc
annotations:
volume.beta.kubernetes.io/storage-class: px-sc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
kubectl create -f px-mongo-pvc.yaml
Let’s go ahead and create the MongoDB Pod.
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: db
labels:
name: mongo
app: todoapp
spec:
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
replicas: 1
template:
metadata:
labels:
name: mongo
app: todoapp
spec:
schedulerName: stork
containers:
- name: mongo
image: mongo
imagePullPolicy: "Always"
ports:
- containerPort: 27017
volumeMounts:
- mountPath: /data/db
name: mongodb
volumes:
- name: mongodb
persistentVolumeClaim:
claimName: px-mongo-pvc
kubectl create -f db-pod.yaml
apiVersion: v1
kind: Service
metadata:
name: db
labels:
name: mongo
app: todoapp
spec:
selector:
name: mongo
type: ClusterIP
ports:
- name: db
port: 27017
targetPort: 27017
kubectl create -f db-svc.yaml
Deploying Node.js Web Application
The web application is a simple todo task list that persists changes to MongoDB. Create the Deployment for the web app with replicas.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
labels:
name: web
app: todoapp
spec:
replicas: 3
selector:
matchLabels:
name: web
template:
metadata:
labels:
name: web
spec:
containers:
- name: web
image: janakiramm/todo
ports:
- containerPort: 3000
apiVersion: v1
kind: Service
metadata:
name: web
labels:
name: web
app: todoapp
spec:
selector:
name: web
type: LoadBalancer
ports:
- name: http
port: 80
targetPort: 3000
protocol: TCP
Accessing the web app shows the below UI.
In the next part of this series, I will show you how to perform failover of MongoDB database running within GKE.
Portworx is a sponsor of The New Stack.
Feature image via Pixabay.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.