Logo
Back to home

How to Make use of a MongoDB Database with the StatefulSets on Google Kubernetes Engine

Sep 15, 2021

You wish to run the MongoDB on the Google Cloud Kubernetes Engine that demands access to the persistent disk.

Given that MongoDB demands a persistent disk, you should deploy it as a stateful application. This demands that you make use of the StateFulSet controller to roll out the MongoDB for a persistent identity whenever the pods have to be rescheduled or started out afresh.

These steps will certainly help out:

Step 1 : Sign in to the console of Google Cloud

Step 2 : Set up the Google Cloud shell

Step 3 : Run the command below to initiate a hello-world cluster:

gcloud container clusters create hello-world \
    --region us-east1

Step 4 : The command takes quite some time to run

Step 5 : Run these commands to generate your StorageClass which instructs the Kubernetes the kind of disk you want for your MongoDB database:


mkdir StatefulSet
cd StatefulSet
cat >> googlecloud_ssd.yaml <<EOL
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: fast
provisioner: kubernetes.io/gce-pd
parameters:
  type: pd-ssd
EOL

Step 6 : Run these commands to apply the StorageClass to the Kubernetes:

kubectl apply -f googlecloud_ssd.yaml

Step 7 : In the terms that govern the use of Kubernetes, ‘service’ denotes the set of rules or policies that oversee the access of specific pods. To summarize, a headless service is one that does not vouch for the servicing of the load.

Step 8 : When used alongside the StatefulSet, this grants the individual DNSs that you use to access the pods, and in return, a means by which you connect all your MongoDB nodes one at a time.

Step 9 : Run the command below to generate the mongo-statefulset.yaml file. You may also gain access to this code at:

cat >> mongo-statefulset.yaml <<EOL
apiVersion: v1
kind: Service
metadata:
  name: mongo
  labels:
    name: mongo
spec:
  ports:
  - port: 27017
    targetPort: 27017
  clusterIP: None
  selector:
    role: mongo
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongo
spec:
  serviceName: "mongo"
  replicas: 3
  selector:
    matchLabels:
      role: mongo
  template:
    metadata:
      labels:
        role: mongo
        environment: test
        replicaset: MainRepSet
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: mongo
          image: mongo
          command:
            - "mongod"
            - "--bind_ip"
            - "0.0.0.0"
            - "--replSet"
            - "MainRepSet"
          resources:
            requests:
              cpu: 0.2
              memory: 200Mi
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: mongo-persistent-storage
              mountPath: /data/db
  volumeClaimTemplates:
  - metadata:
      name: mongo-persistent-storage
      annotations:
        volume.beta.kubernetes.io/storage-class: "fast"
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 10Gi
EOL

Step 10 : To set up the Headless Service and the StatefulSet, run this command:

kubectl apply -f mongo-statefulset.yaml

Step 11 : Before interconnecting the MongoDB replica set, you have to validate that it is indeed running by invoking the following command:

kubectl get statefulset

Step 12 : You shall receive this input:

Step 13 : To generate the list of the pods in your cluster, run the command below:

kubectl get pods

Step 14 : Interconnect it to the first set of the replica:

kubectl exec -it mongo-0 -c mongo bash

Step 15 : To operationalize the replica set, run this command below:

mongo
rs.initiate()
exit

Step 16 : Just if you may have to upscale the replica set, run the command below to up the replica set from 3 to 5

kubectl scale --replicas=5 statefulset mongo

Step 17 : If on the other hand, you need to downscale the replica set, run this command as it decreases the set from 5 to 3.

kubectl scale --replicas=3 statefulset mongo

Step 18 : You should now go-ahead to connect your MongoDB replica set by adopting the URI formatting below:

"mongodb://mongo-0.mongo,mongo-1.mongo,mongo-2.mongo:27017/dbname_?"

Step 19 : Each pod that constitutes the StatefulSet is backed by a Headless Service that possesses a stable DNS name. Its template follows this format : <pod-name >.< service-name>

In this instance, you deployed MongoDB as a form of stateful application. The use of the Headless service empowers you to delineate policies or instructions for accessing specific pods. Nonetheless, it does not recommend load balancing.