Introducing Dragonfly Cloud! Learn More

Question: How do you set up MongoDB sharding in Kubernetes?

Answer

Setting up MongoDB sharding in Kubernetes involves several steps, from deploying MongoDB instances to configuring the sharded cluster. Below is a comprehensive guide to achieve this.

Prerequisites

  • Kubernetes cluster: Ensure you have a running Kubernetes cluster.
  • MongoDB Docker image: Available on Docker Hub or your private registry.
  • kubectl: The command-line tool for interacting with Kubernetes.
  • Helm (optional): Helm helps manage Kubernetes applications through Helm charts.

Steps

1. Deploy MongoDB Replica Sets

Deploy multiple MongoDB Replica Sets that will act as shards within the Kubernetes cluster. Each replica set should consist of an odd number of nodes (minimum 3) for high availability.

Create StatefulSets for each replica set. For example, for one replica set:

apiVersion: apps/v1 kind: StatefulSet metadata: name: mongo-replica-set-1 spec: serviceName: "mongo" replicas: 3 selector: matchLabels: role: mongo replica-set: "1" template: metadata: labels: role: mongo replica-set: "1" spec: containers: - name: mongo image: mongo:4.4 command: ["mongod", "--replSet", "rs1"] ports: - containerPort: 27017

Repeat for other replica sets with appropriate modifications (name, replica-set, and --replSet command).

2. Initialize Replica Sets

After deployment, initialize each replica set by connecting to one pod per replica set and executing the rs.initiate() command.

Example commands for initiating the first replica set:

kubectl exec -it mongo-replica-set-1-0 -- mongo > rs.initiate({ _id: "rs1", members: [ { _id: 0, host: "mongo-replica-set-1-0.mongo:27017" }, { _id: 1, host: "mongo-replica-set-1-1.mongo:27017" }, { _id: 2, host: "mongo-replica-set-1-2.mongo:27017" } ] })

3. Deploy a Config Server Replica Set

Config servers store metadata about the cluster. Deploy them similarly to shard replica sets, but they must run with the --configsvr option.

4. Deploy mongos (Query Router)

Deploy mongos instances that route queries to the correct shard(s). mongos can be deployed using a Deployment or StatefulSet. Ensure that it's configured to connect to the config servers.

5. Configure Sharding

Connect to a mongos instance and add your replica set shards using sh.addShard().

For example:

kubectl exec -it mongos-0 -- mongo > sh.addShard("rs1/mongo-replica-set-1-0.mongo:27017")

Repeat for other shards.

6. Enable Sharding for Databases and Collections

Finally, enable sharding for your databases and collections. This step involves specifying which database and collections should be sharded and choosing a shard key.

> sh.enableSharding("mydatabase") > sh.shardCollection("mydatabase.mycollection", { myKey: 1 })

Conclusion

By following these steps, you can deploy a sharded MongoDB environment within a Kubernetes cluster. This setup enhances scalability and availability, making it suitable for production-grade applications. Adjust configurations based on your specific requirements and cluster size.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.