翻译或纠错本页面

异地分布式复制集的部署

概述

本教程列出了部署 members in multiple locations replica set 的流程。本教程针对三成员的复制集和五成员的复制集。如果您是偶数数目的复制集成员,增加一个仲裁节点以部署一个奇数数目的复制集。

查阅 Replica Sets Distributed Across Two or More Data Centers 了解关于分布式复制集的更多信息。也可以查阅 复制集架构复制

注意事项

架构

在生产环境中,我们将复制集中各个节点部署在单独的机器上,且尽量使用默认的端口 27017 。通过 bind_ip 来配置我们需要接受连接的地址。

参见 复制集架构 以获得更多信息。

连接性

请确保所有的节点都在安全的网络环境中,且各节点之间可以正常通讯。有以下需要考虑的:

  • 建立一个虚拟网络。使所有节点在局域网络环境之间通讯。

  • 通过设置准入来阻挡来自未知客户端的连接请求。

  • 配置网络和防火墙规则来控制进出MongoDB的请求,尽让我们的环境中的请求得以通过。

最后请保证复制集的每个节点都能正常进行并通过DNS与主机名解析。我们可以配置DNS解析域名和系统的 /etc/hosts 来设定该配置。

配置

通过存在 /etc/mongodb.conf 或是其他地方的 configuration file 来指定启动参数。我们应该在启动MongoDB之前创建好其数据目录。

有关启动参数的更多信息请参考 配置文件选项

成员分布

如果可能的话,使用奇数数量的数据中心,选择一种分布,能够最大化即使一个数据中心丢失的情况下,剩下的复制集成员仍然可以构成大多数或者至少能够提供数据备份的可能性。

投票成员

千万不要部署多于7个的投票成员。

准备工作

对于本教程中的所有配置,在一个单独的系统中部署每个复制集成员。即使您在一个系统中部署超过一个复制集成员,这样做的话可以降低荣誉及复制集的容量。这样的部署一般用于测试目的。

本文假设我们将复制集中的各个节点部署在了不同的机器上。如果我们还未安装MongoDB,请参考 installation tutorials

流程

部署一个三个节点地理分离的复制集

对于一个地理分离的三成员复制集部署,您必须决定如何分布您的系统。对于三成员复制集一些可选的分布如下:

  • 三个数据中心:各地一个成员。

  • 两个数据中心:A地两个成员,B地1个成员。如果复制集中的一个成员是仲裁节点,将该仲裁节点和数据存储成员布置在A地。

1

Start each member of the replica set with the appropriate options.

For each member, start a mongod and specify the replica set name through the replSet option. Specify any other parameters specific to your deployment. For replication-specific parameters, see Replication Options.

If your application connects to more than one replica set, each set should have a distinct name. Some drivers group replica set connections by replica set name.

The following example specifies the replica set name through the --replSet command-line option:

mongod --replSet "rs0"

You can also specify the replica set name in a configuration file. To start mongod with a configuration file, specify the configuration file’s path with the --config option:

mongod --config <path-to-config>

In production deployments, you can configure a init script to manage this process. Init scripts are beyond the scope of this document.

2

Connect a mongo shell to a replica set member.

For example, to connect to a mongod running on localhost on the default port of 27017, simply issue:

mongo
3

Initiate the replica set.

Use rs.initiate() on one and only one member of the replica set:

rs.initiate()

MongoDB initiates a set that consists of the current member and that uses the default replica set configuration.

4

Verify the initial replica set configuration.

Use rs.conf() to display the replica set configuration object:

rs.conf()

The replica set configuration object resembles the following:

{
   "_id" : "rs0",
   "version" : 1,
   "members" : [
      {
         "_id" : 1,
         "host" : "mongodb0.example.net:27017"
      }
   ]
}
5

Add the remaining members to the replica set.

Add the remaining members with the rs.add() method. You must be connected to the primary to add members to a replica set.

rs.add() can, in some cases, trigger an election. If the mongod you are connected to becomes a secondary, you need to connect the mongo shell to the new primary to continue adding new replica set members. Use rs.status() to identify the primary in the replica set.

The following example adds two members:

rs.add("mongodb1.example.net")
rs.add("mongodb2.example.net")

When complete, you have a fully functional replica set. The new replica set will elect a primary.

6

Optional. Configure the member eligibility for becoming primary.

In some cases, you may prefer that the members in one data center be elected primary before the members in the other data centers. You can modify the priority of the members such that the members in the one data center has higher priority than the members in the other data centers.

Some members of the replica set, such as members that have networking restraint or limited resources, should not be able to become primary in a failover. Configure members that should not become primary to have priority 0.

For example, to lower the relative eligibility of the the member located in one of the sites (in this example, mongodb2.example.net), set the member’s priority to 0.5.

  1. View the replica set configuration to determine the members array position for the member. Keep in mind the array position is not the same as the _id:

    rs.conf()
    
  2. Copy the replica set configuration object to a variable (to cfg in the example below). Then, in the variable, set the correct priority for the member. Then pass the variable to rs.reconfig() to update the replica set configuration.

    For example, to set priority for the third member in the array (i.e., the member at position 2), issue the following sequence of commands:

    cfg = rs.conf()
    cfg.members[2].priority = 0.5
    rs.reconfig(cfg)
    

    注解

    The rs.reconfig() shell method can force the current primary to step down, causing an election. When the primary steps down, all clients will disconnect. This is the intended behavior. While most elections complete within a minute, always make sure any replica configuration changes occur during scheduled maintenance periods.

After these commands return, you have a geographically redundant three-member replica set.

7

Check the status of the replica set.

Use the rs.status() operation:

rs.status()

部署一个地理分离的五节点复制集

对于地理分离的5节点复制集部署,您必须决定如何分布您的系统。对于五成员复制集一些可选的分布如下:

  • 三个数据中心:两个成员在A地,两个成员在B地,一个成员在C地。

  • 四个数据中心:两个在一个地点,其他三个地点分别维护一个成员。

  • 五个数据中心:各地各一个成员。

  • 两个数据中心:A地三个成员,B地两个成员。

下面的五成员复制集包括一个仲裁节点。

1

Start each member of the replica set with the appropriate options.

For each member, start a mongod and specify the replica set name through the replSet option. Specify any other parameters specific to your deployment. For replication-specific parameters, see Replication Options.

If your application connects to more than one replica set, each set should have a distinct name. Some drivers group replica set connections by replica set name.

The following example specifies the replica set name through the --replSet command-line option:

mongod --replSet "rs0"

You can also specify the replica set name in a configuration file. To start mongod with a configuration file, specify the configuration file’s path with the --config option:

mongod --config <path-to-config>

In production deployments, you can configure a init script to manage this process. Init scripts are beyond the scope of this document.

2

Connect a mongo shell to a replica set member.

For example, to connect to a mongod running on localhost on the default port of 27017, simply issue:

mongo
3

Initiate the replica set.

Use rs.initiate() on one and only one member of the replica set:

rs.initiate()

MongoDB initiates a set that consists of the current member and that uses the default replica set configuration.

4

Verify the initial replica set configuration.

Use rs.conf() to display the replica set configuration object:

rs.conf()

The replica set configuration object resembles the following:

{
   "_id" : "rs0",
   "version" : 1,
   "members" : [
      {
         "_id" : 1,
         "host" : "mongodb0.example.net:27017"
      }
   ]
}
5

Add the remaining secondary members to the replica set.

Use rs.add() in a mongo shell connected to the current primary. The commands should resemble the following:

rs.add("mongodb1.example.net")
rs.add("mongodb2.example.net")
rs.add("mongodb3.example.net")

When complete, you should have a fully functional replica set. The new replica set will elect a primary.

6

Add the arbiter.

In the same shell session, issue the following command to add the arbiter (e.g. mongodb4.example.net):

rs.addArb("mongodb4.example.net")
7

Optional. Configure the member eligibility for becoming primary.

In some cases, you may prefer that the members in one data center be elected primary before the members in the other data centers. You can modify the priority of the members such that the members in the one data center has higher priority than the members in the other data centers.

Some members of the replica set, such as members that have networking restraint or limited resources, should not be able to become primary in a failover. Configure members that should not become primary to have priority 0.

For example, to lower the relative eligibility of the the member located in one of the sites (in this example, mongodb2.example.net), set the member’s priority to 0.5.

  1. View the replica set configuration to determine the members array position for the member. Keep in mind the array position is not the same as the _id:

    rs.conf()
    
  2. Copy the replica set configuration object to a variable (to cfg in the example below). Then, in the variable, set the correct priority for the member. Then pass the variable to rs.reconfig() to update the replica set configuration.

    For example, to set priority for the third member in the array (i.e., the member at position 2), issue the following sequence of commands:

    cfg = rs.conf()
    cfg.members[2].priority = 0.5
    rs.reconfig(cfg)
    

    注解

    The rs.reconfig() shell method can force the current primary to step down, causing an election. When the primary steps down, all clients will disconnect. This is the intended behavior. While most elections complete within a minute, always make sure any replica configuration changes occur during scheduled maintenance periods.

After these commands return, you have a geographically redundant five-member replica set.

8

Check the status of the replica set.

Use the rs.status() operation:

rs.status()