Home Explore Blog CI



kubernetes

6th chunk of `content/en/blog/_posts/2017-01-00-Running-Mongodb-On-Kubernetes-With-Statefulsets.md`
93e3fde12018c21029681d7feca7f277d04909fea2234b090000000100000b98
      spec:
        storageClassName: "fast"
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 100Gi
 ```

It’s a little long, but fairly straightforward.

The first second describes the StatefulSet object. Then, we move into the Metadata section, where you can specify labels and the number of replicas.

Next comes the pod spec. The terminationGracePeriodSeconds is used to gracefully shutdown the pod when you scale down the number of replicas, which is important for databases! 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.

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.

**Using the MongoDB replica set**

At 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 pods

NAME   READY STATUS RESTARTS AGE
mongo-0 2/2  Running 0     3m
mongo-1 2/2  Running 0     3m
mongo-2 2/2  Running 0     3m
 ```

Each 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.mongo
mongo-1.mongo
mongo-2.mongo
 ```

You can use these names directly in the [connection string URI](http://docs.mongodb.com/manual/reference/connection-string) of your app.

In this case, the connection string URI would be:

```
mongodb://mongo-0.mongo,mongo-1.mongo,mongo-2.mongo:27017/dbname\_?
 ```

That’s it!

**Scaling the MongoDB replica set**

A huge advantage of StatefulSets is that you can scale them just like Kubernetes ReplicaSets. If you want 5 MongoDB Nodes instead of 3, just run the scale command:

```
kubectl scale --replicas=5 statefulset mongo
 ```

The sidecar container will automatically configure the new MongoDB nodes to join the replica set.

Include the two new nodes (mongo-3.mongo & mongo-4.mongo) in your connection string URI and you are good to go. Too easy!

**Cleaning Up**

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

Delete the StatefulSet:

```
kubectl delete statefulset mongo
 ```

Delete the Service:

```
kubectl delete svc mongo
 ```

Delete the Volumes:

```
kubectl delete pvc -l role=mongo
 ```

Finally, you can delete the test cluster:

```
gcloud container clusters delete "test-cluster"
 ```

Happy Hacking!

For more cool Kubernetes and Container blog posts, follow me on [Twitter](https://twitter.com/sandeepdinesh) and [Medium](https://medium.com/@SandeepDinesh).


Title: Using, Scaling, and Cleaning Up the MongoDB Replica Set
Summary
This section explains how to interact with the deployed MongoDB replica set, detailing how to retrieve pod names and construct the connection string URI using the stable DNS names provided by the Headless Service. It then demonstrates how to scale the replica set using the 'kubectl scale' command and emphasizes the automatic configuration of new nodes by the sidecar container. Finally, it outlines the steps for cleaning up the deployed resources, including deleting the StatefulSet, Service, PersistentVolumeClaims, and the test cluster itself.