MongoDB Sharding Cluster部署(docker方式)

1 环境介绍

MongoDB集群由三部分组成:

  • mongos:应用程序的路由接口,通过mongos应用程序与整个集群是透明的,一般每个应用服务器对应一个实例,可以跟应用部署到一台服务器上。它主要读取或缓存配置服务器中元数据,提供查询路由到每个分片的功能。
  • configure servers:建议采用副本集,存储集群的元数据。很重要,影响集群的使用。
  • shard:建议采用副本集,提供数据冗余和高可用,主要存储业务数据。

当前环境由3台mongos、3台configure servers、3个分片集群构成。

类型 角色 默认端口 映射端口
mongos mongos0 27017 37011
mongos1 27017 37012
mongos2 27017 37013
configure servers configsvr0 27019 37021
configsvr1 27019 37022
configsvr2 27019 37023
shard0 shardsvr00 27018 37031
shardsvr01  27018 37032
shardsvr02  27018 37033
shard1 shardsvr10  27018 37041
shardsvr11  27018 37042
shardsvr12  27018 370423
shard2 shardsvr20  27018 37051
shardsvr21  27018 37052
shardsvr22  27018 37053

2 部署MongoDB集群

2.1 准备工作

宿主机安装MongoDB Shell,后面方便管理。

 

生成keyfile文件

[root]# mkdir -p /etc/mongodb
[root]# openssl rand -base64 765 > /etc/mongodb/key.file
[root]# chmod 400 /etc/mongodb/key.file

2.2 部署configure servers

2.2.1 生成配置文件

生成configure servers配置文件

[root]# vi /etc/mongodb/config.conf 
systemLog:
  destination: file
  path: "/data/configdb/config.log"
  logAppend: true
storage:
  dbPath: /data/configdb
  wiredTiger:                                                                                                                   
    engineConfig:                                                                                                               
      cacheSizeGB: 0.25
net:
  bindIpAll: true
#security:
 # keyFile: /data/configdb/key.file
 # authorization: enabled
replication:
  replSetName: rs_configsvr
sharding:
  clusterRole: configsvr

2.2.2 创建配置服务

创建configure servers(端口映射、文件映射是为了后期调试方便)

[root]# docker run -d --name pantao_configsvr0 -p 37021:27019 -v /etc/mongodb/config.conf:/data/configdb/config.conf -v /etc/mongodb/key.file:/data/configdb/key.file mongo --config /data/configdb/config.conf
[root]# docker run -d --name pantao_configsvr1 -p 37022:27019 -v /etc/mongodb/config.conf:/data/configdb/config.conf -v /etc/mongodb/key.file:/data/configdb/key.file mongo --config /data/configdb/config.conf
[root]# docker run -d --name pantao_configsvr2 -p 37023:27019 -v /etc/mongodb/config.conf:/data/configdb/config.conf -v /etc/mongodb/key.file:/data/configdb/key.file mongo --config /data/configdb/config.conf

2.2.3 创建configure servers集群

configure servers集群初始化

[root]# docker exec -it pantao-configsvr0 bash
[root]# mongo --port 27019
> rs.initiate(
  {
    _id: "rs_configsvr",
    configsvr: true,
    members: [
      { _id : 0, host : "10.150.1.107:37021" },
      { _id : 1, host : "10.150.1.107:37022" },
      { _id : 2, host : "10.150.1.107:37023" }
    ]
  }
)
输出信息:
{
	"ok" : 1,
	"$gleStats" : {
		"lastOpTime" : Timestamp(1641429399, 1),
		"electionId" : ObjectId("000000000000000000000000")
	},
	"lastCommittedOpTime" : Timestamp(1641429399, 1)
}

2.3 部署shard

2.3.1 生成配置文件

生成分片集群(rs_shardsvr0)配置文件

[root]# vi /etc/mongodb/shard0.conf
systemLog:
  destination: file
  path: "/data/db/shards.log"
  logAppend: true
storage:
  dbPath: /data/db
  wiredTiger:
    engineConfig:
      cacheSizeGB: 0.25
net:
  bindIpAll: true
#security:
 # keyFile: /data/db/key.file
  #authorization: enabled
replication:
  replSetName: rs_shardsvr0
sharding:
  clusterRole: shardsvr

 

生成分片集群(rs_shardsvr1)配置文件

[root]# vi /etc/mongodb/shard1.conf
systemLog:
  destination: file
  path: "/data/db/shards.log"
  logAppend: true
storage:
  dbPath: /data/db
  wiredTiger:
    engineConfig:
      cacheSizeGB: 0.25
net:
  bindIpAll: true
#security:
 # keyFile: /data/db/key.file
  #authorization: enabled
replication:
  replSetName: rs_shardsvr1
sharding:
  clusterRole: shardsvr

 

生成分片集群(rs_shardsvr2)配置文件

[root]# vi /etc/mongodb/shard2.conf
systemLog:
  destination: file
  path: "/data/db/shards.log"
  logAppend: true
storage:
  dbPath: /data/db
  wiredTiger:
    engineConfig:
      cacheSizeGB: 0.25
net:
  bindIpAll: true
#security:
 # keyFile: /data/db/key.file
  #authorization: enabled
replication:
  replSetName: rs_shardsvr2
sharding:
  clusterRole: shardsvr

2.3.2 创建分片集群

创建分片(集群rs_shardsvr0)

[root]# docker run -d --name pantao_shardsvr00 -p 37031:27018 -v /etc/mongodb/shard0.conf:/data/db/shard0.conf -v /etc/mongodb/key.file:/data/db/key.file mongo --config /data/db/shard0.conf
[root]# docker run -d --name pantao_shardsvr01 -p 37032:27018 -v /etc/mongodb/shard0.conf:/data/db/shard0.conf -v /etc/mongodb/key.file:/data/db/key.file mongo --config /data/db/shard0.conf
[root]# docker run -d --name pantao_shardsvr02 -p 37033:27018 -v /etc/mongodb/shard0.conf:/data/db/shard0.conf -v /etc/mongodb/key.file:/data/db/key.file mongo --config /data/db/shard0.conf

 

创建分片(集群rs_shardsvr1)

[root]# docker run -d --name pantao_shardsvr10 -p 37041:27018 -v /etc/mongodb/shard1.conf:/data/db/shard1.conf -v /etc/mongodb/key.file:/data/db/key.file mongo --config /data/db/shard1.conf
[root]# docker run -d --name pantao_shardsvr11 -p 37042:27018 -v /etc/mongodb/shard1.conf:/data/db/shard1.conf -v /etc/mongodb/key.file:/data/db/key.file mongo --config /data/db/shard1.conf
[root]# docker run -d --name pantao_shardsvr12 -p 37043:27018 -v /etc/mongodb/shard1.conf:/data/db/shard1.conf -v /etc/mongodb/key.file:/data/db/key.file mongo --config /data/db/shard1.conf

 

创建分片(集群rs_shardsvr2)

[root]# docker run -d --name pantao_shardsvr20 -p 37051:27018 -v /etc/mongodb/shard2.conf:/data/db/shard2.conf -v /etc/mongodb/key.file:/data/db/key.file mongo --config /data/db/shard2.conf
[root]# docker run -d --name pantao_shardsvr21 -p 37052:27018 -v /etc/mongodb/shard2.conf:/data/db/shard2.conf -v /etc/mongodb/key.file:/data/db/key.file mongo --config /data/db/shard2.conf
[root]# docker run -d --name pantao_shardsvr22 -p 37053:27018 -v /etc/mongodb/shard2.conf:/data/db/shard2.conf -v /etc/mongodb/key.file:/data/db/key.file mongo --config /data/db/shard2.conf

2.3.3 分片集群初始化

rs_shardsvr0

[root]# docker exec -it pantao-shardsvr00 bash
[root]# mongo --port 27018
> rs.initiate(
  {
    _id : "rs_shardsvr0",
    members: [
      { _id : 0, host : "10.150.1.107:37031" },
      { _id : 1, host : "10.150.1.107:37032" },
      { _id : 2, host : "10.150.1.107:37033" }
    ]
  }
)

 

rs_shardsvr1

[root]# docker exec -it pantao-shardsvr10 bash
[root]# mongo --port 27018
> rs.initiate(
  {
    _id : "rs_shardsvr1",
    members: [
      { _id : 0, host : "10.150.1.107:37041" },
      { _id : 1, host : "10.150.1.107:37042" },
      { _id : 2, host : "10.150.1.107:37043" }
    ]
  }
)

 

rs_shardsvr2

[root]# docker exec -it pantao-shardsvr20 bash
[root]# mongo --port 27018
> rs.initiate(
  {
    _id : "rs_shardsvr2",
    members: [
      { _id : 0, host : "10.150.1.107:37051" },
      { _id : 1, host : "10.150.1.107:37052" },
      { _id : 2, host : "10.150.1.107:37053" }
    ]
  }
)

2.4 部署mongos

 

 

 

ok

上一篇:SQLAlchemy数据库


下一篇:Solon Web 开发,五、数据访问、事务与缓存应用