搭建集群

搭建集群

通过MongoDB sharded集群可以在多个机器存储数据。

每个集群包含:

  • 一个或多个*shards*. 每个shard包含三个备份集(三个实例组成一个备份集)。
  • 一个或多个*查询路由*。查询路由是你的应用所实际连接的机器。该机器负责与中心管理服务器通信以获取请求数据存储位置。它将从合适的shard访问并返回数据。
  • 一个或多个*配置服务器*。配置服务器储存了关联请求数据和包含数据的shard的原数据。

该例展示了如何搭建一个MongoDB sharded集群。

注解

开始之前 请确认:

  • 管理员已注册了MongoDB存储类型和版本。
  • 管理员已经创建了满足MongoDB最小要求的:ref:flavor that meets the MongoDB minimum requirements <create_db>.

搭建集群

  1. **创建集群*

    使用:command:`trove cluster-create`命令创建集群。该命令创建一个one-shard集群。传入:

    • 集群的名字:
    • 欲使用的存储的名称和版本。
    • The three instances you want to include in the replication set for the first shard. Specify each instance by using the --instance argument and the associated flavor ID and volume size. Use the same flavor ID and volume size for each instance. In this example, flavor 7 is a custom flavor that meets the MongoDB minimum requirements.
    $ trove cluster-create cluster1 mongodb "2.4" \
      --instance flavor=7,volume=2 --instance flavor=7,volume=2 \
      --instance flavor=7,volume=2
     +-------------------+--------------------------------------+
     | Property          | Value                                |
     +-------------------+--------------------------------------+
     | created           | 2014-08-16T01:46:51                  |
     | datastore         | mongodb                              |
     | datastore_version | 2.4                                  |
     | id                | aa6ef0f5-dbef-48cd-8952-573ad881e717 |
     | name              | cluster1                             |
     | task_description  | Building the initial cluster.        |
     | task_name         | BUILDING                             |
     | updated           | 2014-08-16T01:46:51                  |
     +-------------------+--------------------------------------+
    
  2. 显示集群信息

    使用:command:trove cluster-show 命令显示集群信息。使用集群ID作为参数:

    首次创建集群时显示集群ID. (如果需要再次查看,使用`trove cluster-list`命令列出系统中所有集群的名称和ID.)

    $ trove cluster-show CLUSTER_ID
     +-------------------+--------------------------------------+
     | Property          | Value                                |
     +-------------------+--------------------------------------+
     | created           | 2014-08-16T01:46:51                  |
     | datastore         | mongodb                              |
     | datastore_version | 2.4                                  |
     | id                | aa6ef0f5-dbef-48cd-8952-573ad881e717 |
     | ip                | 10.0.0.2                             |
     | name              | cluster1                             |
     | task_description  | No tasks for the cluster.            |
     | task_name         | NONE                                 |
     | updated           | 2014-08-16T01:59:33                  |
     +-------------------+--------------------------------------+
    

    注解

    你的应用连接此IP地址 trove cluster-show 命令显示要查询的路由器的IP地址。该IP是你的应用用来从数据库中获取数据的地址。

  3. 列出集群实例

    使用:command:trove cluster-instances 命令列出集群下的实例。

    $ trove cluster-instances CLUSTER_ID
    +--------------------------------------+----------------+-----------+------+
    | ID                                   | Name           | Flavor ID | Size |
    +--------------------------------------+----------------+-----------+------+
    | 45532fc4-661c-4030-8ca4-18f02aa2b337 | cluster1-rs1-1 | 7         |    2 |
    | 7458a98d-6f89-4dfd-bb61-5cf1dd65c121 | cluster1-rs1-2 | 7         |    2 |
    | b37634fb-e33c-4846-8fe8-cf2b2c95e731 | cluster1-rs1-3 | 7         |    2 |
    +--------------------------------------+----------------+-----------+------+
    

    备份集和实例的命名惯例 注意,``Name``列显示的实例名包含了备份集的名称。备份集的名称和实例名是自动生成的。它们遵循如下规则:

    • 备份集名称 该名称是由集群名后拼接-rs*n*(n为1则表示第一个备份,2表示第二个,以此类推)而成。在这个例子中,集群名称为``cluster1``, 由于只有一个备份集,因此备份集的名称为``cluster1-rs1``.
    • 实例名称 该名称由备份集的名称和-*n*拼接而成, *n*为1则代表备份集中的第一个实例,*n*为2则代表第二个实例,以此类推。在该例中,实例名为``cluster1-rs1-1``, cluster1-rs1-2, 和``cluster1-rs1-3``.
  4. 集群列表

    使用 :command:`trove cluster-list`命令列出所有的集群。

    $ trove cluster-list
    +--------------------------------------+----------+-----------+-------------------+-----------+
    | ID                                   | Name     | Datastore | Datastore Version | Task Name |
    +--------------------------------------+----------+-----------+-------------------+-----------+
    | aa6ef0f5-dbef-48cd-8952-573ad881e717 | cluster1 | mongodb   | 2.4               | NONE      |
    | b8829c2a-b03a-49d3-a5b1-21ec974223ee | cluster2 | mongodb   | 2.4               | BUILDING  |
    +--------------------------------------+----------+-----------+-------------------+-----------+
    
  5. 删除一个集群

    使用 trove cluster-delete 命令删除一个集群。

    $ trove cluster-delete CLUSTER_ID
    

查询路由和配置服务器

每个集群都至少包含一个查询路由和一个配置服务。查询路由和配置服务的计数可以使用配额来约束。当你删除一个集群时,系统也会将已经分配的查询路由(多个)和配置服务(多个)删除。

Creative Commons Attribution 3.0 License

Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.