Docs HomeMongoDB Manual

Sharded Cluster Components分片化集群组件

A MongoDB sharded cluster consists of the following components:MongoDB分片集群由以下组件组成:

  • shard分片: Each shard contains a subset of the sharded data. :每个分片都包含分片数据的一个子集。As of MongoDB 3.6, shards must be deployed as a replica set.从MongoDB 3.6开始,分片必须作为副本集进行部署。
  • mongos: The mongos acts as a query router, providing an interface between client applications and the sharded cluster. mongos充当查询路由器,提供客户端应用程序和分片集群之间的接口。Starting in MongoDB 4.4, mongos can support hedged reads to minimize latencies.从MongoDB 4.4开始,mongos可以支持对冲读取,以最大限度地减少延迟。
  • config servers配置服务器: Config servers store metadata and configuration settings for the cluster. As of MongoDB 3.4, config servers must be deployed as a replica set (CSRS).:配置服务器存储群集的元数据和配置设置。从MongoDB 3.4开始,配置服务器必须作为副本集(CSRS)进行部署。

Production Configuration生产配置

In a production cluster, ensure that data is redundant and that your systems are highly available. 在生产集群中,请确保数据是冗余的,并且您的系统具有高可用性。Consider the following for a production sharded cluster deployment:对于生产分片集群部署,请考虑以下内容:

  • Deploy Config Servers as a 3 member replica set将配置服务器部署为3个成员的副本集
  • Deploy each Shard as a 3 member replica set将每个分片部署为一个由3个成员组成的副本集
  • Deploy one or more mongos routers部署一个或多个mongos路由器

Replica Set Distribution副本集分发

Where possible, consider deploying one member of each replica set in a site suitable for being a disaster recovery location.在可能的情况下,考虑在适合作为灾难恢复位置的站点中部署每个复制副本集的一个成员。

Note

Distributing replica set members across two data centers provides benefit over a single data center. 将复制副本集成员分布在两个数据中心提供了优于单个数据中心的优势。In a two data center distribution,在双数据中心分布中,

  • If one of the data centers goes down, the data is still available for reads unlike a single data center distribution.如果其中一个数据中心出现故障,则与单个数据中心分布不同,数据仍可用于读取。
  • If the data center with a minority of the members goes down, the replica set can still serve write operations as well as read operations.如果拥有少数成员的数据中心宕机,副本集仍然可以提供写操作和读操作。
  • However, if the data center with the majority of the members goes down, the replica set becomes read-only.但是,如果包含大多数成员的数据中心出现故障,则副本集将变为只读。

If possible, distribute members across at least three data centers. 如果可能,将成员分布在至少三个数据中心。For config server replica sets (CSRS), the best practice is to distribute across three (or more depending on the number of members) centers. 对于配置服务器副本集(CSRS),最佳做法是分布在三个(或更多,具体取决于成员数量)中心。If the cost of the third data center is prohibitive, one distribution possibility is to evenly distribute the data bearing members across the two data centers and store the remaining member in the cloud if your company policy allows.如果第三个数据中心的成本过高,一种分发可能性是在两个数据中心之间平均分配数据承载成员,并在公司政策允许的情况下将剩余成员存储在云中。

Number of Shards分片数量

Sharding requires at least two shards to distribute sharded data. 分片化至少需要两个分片来分发分片化的数据。Single shard sharded clusters may be useful if you plan on enabling sharding in the near future, but do not need to at the time of deployment.如果您计划在不久的将来启用分片,但在部署时不需要,那么单分片集群可能会很有用。

Number of mongos and Distributionmongos的数量和分布

mongos routers support high availability and scalability when deploying multiple mongos instances. 路由器在部署多个mongos实例时支持高可用性和可扩展性。If a proxy or load balancer is between the application and the mongos routers, you must configure it for client affinity. 如果代理或负载均衡器位于应用程序和mongos路由器之间,则必须将其配置为客户端关联Client affinity allows every connection from a single client to reach the same mongos. For shard-level high availability, either:客户端亲和性允许来自单个客户端的每个连接到达相同的mongos。对于分片级别的高可用性,请执行以下任一操作:

  • Add mongos instances on the same hardware where mongos instances are already running.mongos实例已经运行的同一硬件上添加mongos示例。
  • Embed mongos routers at the application level.在应用程序级别嵌入mongos路由器。

mongos routers communicate frequently with your config servers. 路由器经常与配置服务器通信。As you increase the number of routers, performance may degrade. 随着路由器数量的增加,性能可能会下降。If performance degrades, reduce the number of routers. 如果性能下降,请减少路由器的数量。Your deployment should not have more than 30 mongos routers.您的部署不应超过30个mongos路由器。

The following diagram shows a common sharded cluster architecture used in production:下图显示了生产中使用的常见分片集群体系结构:

Diagram that shows a production-level sharded cluster containing multiple shards and mongos routers.

Development Configuration开发配置

For testing and development, you can deploy a sharded cluster with a minimum number of components. 对于测试和开发,您可以部署具有最少组件数量的分片集群。These non-production clusters have the following components:这些非生产集群包含以下组件:

The following diagram shows a sharded cluster architecture used for development only:下图显示了仅用于开发的分片集群体系结构:

Diagram that shows a development sharded cluster containing a single shard and mongos router.
Warning

Use the test cluster architecture for testing and development only.仅将测试集群体系结构用于测试和开发。

Tip

See also: 另请参阅:

Deploy a Sharded Cluster部署共享群集