How to Deploy MongoDB on Google Kubernetes Engine (GKE)

Then the configurations for the two containers is shown.

The first one runs MongoDB with command line flags that configure the replica set name.

It also mounts the persistent storage volume to /data/db, the location where MongoDB saves its data.

The second container runs the sidecar.

This “sidecar” container will configure the MongoDB replica set automatically.

A “sidecar” is a helper container which helps the main container do its work.

Finally, there is the volumeClaimTemplates.

This is what talks to the StorageClass we created before to provision the volume.

It will provision a 100 GB disk for each MongoDB replica.

You can deploy both the Headless Service and the StatefulSet with this command:kubectl apply -f mongo-statefulset.

yamlConnect to the MongoDB Replica SetNow that we have a cluster running and our Replica Set deployed, we can connect to it.

Wait for the MongoDB Replica Set to be fully deployedKubernetes StatefulSets will deploy each pod sequentially.

It will wait for the MongoDB Replica Set member to fully boot up and create the backing disk before starting the next member.

In a few minutes, you should see the “Desired” and “Current” count to be equal:kubectl get statefulsetYou should eventually get back a result that looks something like:NAME DESIRED CURRENT AGEmongo 3 3 3mViewing the MongoDB replica setAt this point, you should have three pods created in your cluster.

These correspond to the three nodes in your MongoDB replica set.

You can see them with this command:kubectl get podsYou should get back a result that looks something like the following.

If not, wait for all three members to be created:NAME READY STATUS RESTARTS AGEmongo-0 2/2 Running 0 3mmongo-1 2/2 Running 0 3mmongo-2 2/2 Running 0 3mLet’s connect to the first Replica Set member.

kubectl exec -ti mongo-0 mongoScaling the MongoDB Replica SetA big advantage of Kubernetes and StatefulSets is that you can scale up and down the number of MongoDB Replicas with a single command!To scale up the number of Replica Set members from 3 to 5, run this command:kubectl scale –replicas=5 statefulset mongoIn a few minutes, you will see that there are 5 MongoDB pods.

kubectl get podsYou should get back a result that looks something like:NAME READY STATUS RESTARTS AGEmongo-0 2/2 Running 0 41mmongo-1 2/2 Running 0 39mmongo-2 2/2 Running 0 37mmongo-3 2/2 Running 0 4mmongo-4 2/2 Running 0 2mTo scale down the number of Replica Set members from 5 to 1, run this command:kubectl scale –replicas=1 statefulset mongoIn a few seconds, you will see that there is 1 MongoDB pod.

kubectl get podsYou should get back a result that looks something like:NAME READY STATUS RESTARTS AGEmongo-0 2/2 Running 0 41mUsing the MongoDB Replica SetEach pod in a StatefulSet backed by a Headless Service will have a stable DNS name.

The template follows this format: <pod-name>.

<service-name>This means the DNS names for the MongoDB replica set are:mongo-0.

mongomongo-1.

mongomongo-2.

mongoYou can use these names directly in the connection string URI of your app.

In this case, the connection string URI would be:"mongodb://mongo-0.

mongo,mongo-1.

mongo,mongo-2.

mongo:27017/dbname_?"Using the database is outside the scope of this codelab.

Clean UpTo clean up the deployed resources, delete the StatefulSet, Headless Service, and the provisioned volumes.

Delete the StatefulSet:kubectl delete statefulset mongoDelete the Service:kubectl delete svc mongoDelete the Volumes:kubectl delete pvc -l role=mongo.

. More details

Leave a Reply