This tutorial will show you how to set up a fault-tolerant MongoDB sharded cluster on a Ubuntu or Debian. For instance, if you would like to use remote shared storage for this sharded cluster set up, follow this tutorial.
A MongoDB sharded cluster consists of three components:
- Shard that can be deployed as a replica set.
- Mongos acts as a query router, providing an interface between client applications and the sharded cluster.
- Config store metadata and configuration settings for the cluster.
The following illustration describes the interaction of components within a sharded cluster:
Note: This guide is specifically written for Ubuntu 18.04, 19.04, 19.10, 20.04 and Debian 9, 10.
Prerequisites
To follow this tutorial along, you will need at least 10 (physical or virtual) machines installed with Ubuntu or Debian having sudo non-root user privileges.
We will use these ten machines with the information (as described above) for our sharded cluster throughout this tutorial. Make sure you substitute, hostname, ip address, domain and red highlighted text with yours wherever applicable.
Make sure you have completed basic network settings i.e. hostname, timezone and ip addresses on each of your servers.
You can set hostname on your servers with below command:
You can set timezone on your servers with below command:
Update Hosts Files
Login to your first server (cfg1.example.pk) in our case, edit /etc/hosts file:Add the following to the hosts file, make sure you replace the highlighted text with yours and (if possible) keep the hostnames same to avoid confusion:
Save and close when you are finished.
Repeat the same on each of your remaining servers before proceeding to next step.
Add MongoDB Source
You need to add MongoDB official repository in Ubuntu's source list in order to install latest release using apt package manager:Type below to add mongodb repository:
Make sure you repeat the same steps on each of your remaining servers before proceeding to next step.
Installing MongoDB
When you are finished adding mongodb official repository on all of the servers, login to your first server (cfg1.example.pk) in our case.
Type below command to install mongodb:
This command installs mongodb-org, a meta-package that includes the following:
mongodb-org-server is a standard MongoDB daemon, including init scripts and configurations
mongodb-org-mongos contains MongoDB shard daemon
mongodb-org-shell used to interact with MongoDB via the command line
mongodb-org-tools contains a few basic tools to restore, import, and export data, as well as a variety of other functions.
These packages provide a solid foundation that will serve most use cases, hence we recommend installing them all. However, if you want a more minimal installation, you can selectively install packages from the above list rather than using the mongodb-org meta-package.
Make sure you repeat the same installation step on each of your remaining servers before proceeding to next step.
Create an Administrative User
Login to your first config server (cfg1.example.pk) in our case, we intend to use as the primary member of replica set of config servers.
Start mongodb service with below command:
Access mongo shell with below command:
On mongo shell, type below to switch to the default admin database:
Create an administrative user with root privileges, make sure you replace “password” with a strong password of your choice:
This will return similar to the following output:
Type below to exit from mongo shell:
Set Up MongoDB Authentication
In this section, you need to create a key file that will be used to secure authentication between the members of MongoDB replica set.
On (cfg1.example.pk) in our case, type below command to generate a key file:
For this guide, we generated a key file under the home directory of user.
Copy ~/mongodb_key.pem file to /var/lib/mongodb/, and set appropriate permissions like below:
Next, copy mongodb_key.pem file from cfg1.example.pk to each of your remaining servers under the same location with identical permissions before proceeding to next step.Create the Config Server Replica Set
Login to your first config server (cfg1.example.pk) in our case, edit /etc/mongod.conf file:Add, update the following highlighted values, make sure you replace default port with 27019
Save and close file when you are finished.
Repeat the same on each of your remaining config servers (cfg2.example.pk, cfg3.example.pk) in our case, before proceeding to next step.
For reference, you can see below mongod.conf from cfg1.example.pk in our case.
This mongod.conf from cfg2.example.pk in our case.
and this mongod.conf from cfg3.example.pk in our case.
When you are finished with the above on all three config servers, restart the mongodb service to take changes into effect:
Confirm that the mongodb service is active and running:screenshot of cfg1.example.pk
screenshot of cfg2.example.pk
screenshot of cfg3.example.pk
Initiate the config replica set.
On your first config server, (cfg1.example.pk) in our case, connect to the MongoDB shell over port 27019 with the administrative user like below:
This will prompt you for password:From the mongo shell, initiate the config server's replica set like below:
You will see a message similar to below indicating the operation succeeded:
Notice that the MongoDB shell prompt has also changed to configReplSet:PRIMARY> or configReplSet:SECONDARY>.
If the replica set has been configured properly, you’ll see output similar to the following:
Now exit from mongo shell with below command:
Create the Shard Replica Sets
In this section we will configure shard replica set (rs0) on (shrd1, shrd2, shrd3) servers.
Login to your first shard server (shrd1.example.pk) in our case and edit /etc/mongod.conf file like below:
Add, update the following values, make sure you replace default port with 27018:Save and close file when you are finished.
Make sure you repeat the same on (shrd2.example.pk, shrd3.example.pk) as well.
For reference you can see below screenshot of mongod.conf file on shrd1.example.pk after the changes we made.
Below screenshot of mongod.conf from shrd2.example.pk
and below screenshot of mongod.conf from shrd3.example.pk
When you are finished with the above step, start mongodb service on (shrd1, shrd2, shrd3) to take changes into effect:
screenshot of mongod service status from shrd1.example.pkscreenshot of mongod service status from shrd2.example.pk
screenshot of mongod service status from shrd3.example.pk
When you are finished with the above step on (shrd1, shrd2, shrd3), proceed with the below.
In this step, we will configure shard replica set (rs1) on (shrd4, shrd5, shrd6) servers.
Login to (shrd4.example.pk) in our case, edit /etc/mongod.conf file:
Add, update the following values, make sure you replace default port with 27018:
Save and close file when you are finished.
Make sure you repeat the same on (shrd5.example.pk, shrd6.example.pk) as well.
For reference you scan see below screenshot of mongod.conf from shrd4.example.pk after the changes we made.
Below screenshot of mongod.conf from shrd5.example.pk
and below screenshot of mongod.conf from shrd6.example.pk
When you are finished with the above step, restart the mongod service on (shrd4, shrd5, shrd6) to take changes into effect:
mongod service status screenshot from shrd4.example.pkmongod service status screenshot from shrd5.example.pk
mongod service status screenshot from shrd6.example.pk
Initiate the shard replica set.
Login to your first shard server (shrd1.example.pk) in our case, connect to mongo shell on port 27018 with administrative user authentication like below:
Type below on mongo shell to initiate shard replica set (rs0):This will return { "ok" : 1 } indicating that shard replica set rs0 is initiated successfully.
Now exit from the mongo shell with below command:
Next, login to (shrd4.example.pk) in our case, connect to mongo shell on port 27018 with administrative user authentication like below:
Type below on mongo shell to initiate shard replica set (rs1):
This will return { "ok" : 1 } indicating that shard replica set rs1 is initiated successfully.
Now exit from the mongo shell with below command:
Configure Mongos (Query Router)
Log in to (qrt1.example.pk), and perform the following.Type below command to deactivate mongod service:
The mongos service that we will create in below step needs to obtain data locks that conflicts with mongod, so be sure mongod is stopped before proceeding:
As you can see mongod.service has been stopped on query router.
Type below to create /etc/mongos.conf file:
Add the following configuration directives:
Save and close file when you are finished.
You can see final mongos.conf file in below screenshot.
Next, create a systemd service unit file for mongos like below:
Add the following parameters:
Save and close file when you are finished.
You can see final mongos.service file in below screenshot.
Next, start mongos.service with below command to activate query router.
Confirm that mongos is running with below command:
You can see below screenshot of mongos status
Repeat the same configuration on (qrt2.example.pk) before proceeding to next step.
Add Shards to the Cluster
From (qrt1) or (qrt2), connect to mongo shell on port 27017 with administrative authentication like below:
On mongo shell, type below to add shard replica set (rs0) in the cluster:Type below to add shard replica set (rs1) in the cluster:
You will see the similar output indicating that shard replica set (rs0, rs1) added successfully.
At this stage, your MongoDB sharded cluster is active and running.
The last step is to enable sharding for database. This process takes place in stages due to the organization of data in MongoDB. To understand how data will be distributed, let’s quickly understand the main data structures:
- Databases - The broadest data structure in MongoDB, used to hold groups of related data.
- Collections - Analogous to tables in traditional relational database systems, collections are the data structures that comprise databases
- Documents - The most basic unit of data storage in MongoDB. Documents use JSON format for storing data using key-value pairs that can be queried by applications
Sharding Strategy
Before you enable sharding, you’ll need to decide on a sharding strategy. When data is distributed among the shards, MongoDB needs a way to sort it and know which data is on which shard. To do this, it uses a shard key, a designated field in your documents that is used by the mongos query router know where a given piece of data is stored.
The two most common sharding strategies are:
- Range-based sharding divides your data based on specific ranges of values in the shard key.
- Hash-based sharding distributes data by using a hash function on your shard key for a more even distribution of data among the shards.
This is not intended to be a comprehensive explanation to choosing a sharding strategy. You may wish to consult with official resource of MongoDB’s documentation on sharding strategy.
Enable Sharding
Now that we’ve decided to use a hash-based sharding strategy, we'll enable sharding at the collections level. This allows the documents within a collection to be distributed among your shards.
Login to (qrt1.example.pk) in our case, access the mongos shell:
From the mongos shell, type below to create a test database called testDBCreate a new collection called testCollection and hash its _id key. The _id key is already created by default as a basic index for new documents:
Type below to enable sharding for newly created database:
This enables sharding across any shards that you added to your cluster in the Add Shards to the Cluster step.
To verify that the sharding was successfully enabled, type below to switch to the config database:
Next, run below method:
This will return a list of all databases with useful information similar to like below:
In the above output, you can see only one entry for the testDB database we just created.
Once you enable sharding for a database, MongoDB assigns a primary shard for that database where MongoDB stores all data in that database.
Verify Shard Cluster
To ensure your data is being distributed evenly in the testDB database and collection you configured above, you can follow these steps to generate some basic test data and see how it is divided among the shards.
Switch to your testDB database:
Type the following code in the mongo shell to generate 10000 simple documents and insert them into testCollection:
Check your data distribution:
This will return information similar to the following:
The sections beginning with Shard give information about each shard in your cluster. Since we only added 2 shards with three members each, there are only two sections, but if you add more shards to the cluster, they’ll show up here as well.
The Totals section provides information about the collection as a whole, including its distribution among the shards. Notice that distribution is not perfectly equal. The hash function does not guarantee absolutely even distribution, but with a carefully chosen shard key it will usually be fairly close.
Wrapping up
Now that you have successfully deployed a highly available fault-tolerant MongoDB sharded cluster ready to use for your production environment, it is recommended to configure a firewall to limit ports 27018 and 27019 to only accept traffic between hosts within your cluster.