使用Docker部署MongoDB集群

使用Docker部署MongoDB Cluster

环境准备

  • 四台服务器,分别命名为ServerA、ServerB、ServerC、ServerD
  • 2 Shard(1 Primary 1 Secondary 1 Arbiter) Nodes
  • 3 Config Nodes
  • 4 Router Nodes
  • Docker版本:1.12.5
  • Docker Compose版本:1.9.0

Docker镜像

MongoDB官方镜像

docker-compose.yml

ServerA配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
version: '2'
services:
configsrv:
image: mongo
command: mongod --keyFile /data/configdb/mongodb-keyfile --oplogSize 1024 --replSet configrs --port 27017 --configsvr --wiredTigerCacheSizeGB 5
volumes:
- /data/configsrv_db:/data/configdb
ports:
- "27018:27017"
restart:
always
container_name:
configsrv
ulimits:
nofile:
soft: 300000
hard: 300000
rs1_node:
image: mongo
command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 10240 --replSet rs1 --directoryperdb --port 27017 --shardsvr
volumes:
- /data/rs1_node_db:/data/db
ports:
- "27019:27017"
restart:
always
container_name:
rs1_node
ulimits:
nofile:
soft: 300000
hard: 300000
router:
image: mongo
command: mongos --keyFile /data/db/mongodb-keyfile --configdb configrs/ServerA:27018,ServerB:27018,ServerC:27018
ports:
- "27017:27017"
volumes:
- /data/router_db:/data/db
restart:
always
container_name:
router
ulimits:
nofile:
soft: 300000
hard: 300000

ServerB配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
version: '2'
services:
configsrv:
image: mongo
command: mongod --keyFile /data/configdb/mongodb-keyfile --oplogSize 1024 --replSet configrs --port 27017 --configsvr --wiredTigerCacheSizeGB 5
volumes:
- /data/configsrv_db:/data/configdb
ports:
- "27018:27017"
restart:
always
container_name:
configsrv
ulimits:
nofile:
soft: 300000
hard: 300000
rs1_node:
image: mongo
command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 10240 --replSet rs1 --directoryperdb --port 27017 --shardsvr
volumes:
- /data/rs1_node_db:/data/db
ports:
- "27019:27017"
restart:
always
container_name:
rs1_node
ulimits:
nofile:
soft: 300000
hard: 300000
rs2_arbiter:
image: mongo
command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 1024 --replSet rs2 --directoryperdb --port 27017 --shardsvr --wiredTigerCacheSizeGB 1 --nojournal --smallfiles
volumes:
- /data/rs2_arbiter_db:/data/db
ports:
- "27020:27017"
restart:
always
container_name:
rs2_arbiter
ulimits:
nofile:
soft: 300000
hard: 300000
router:
image: mongo
command: mongos --keyFile /data/db/mongodb-keyfile --configdb configrs/ServerA:27018,ServerB:27018,ServerC:27018
ports:
- "27017:27017"
volumes:
- /data/router_db:/data/db
restart:
always
container_name:
router
ulimits:
nofile:
soft: 300000
hard: 300000

ServerC配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
version: '2'
services:
configsrv:
image: mongo
command: mongod --keyFile /data/configdb/mongodb-keyfile --oplogSize 1024 --replSet configrs --port 27017 --configsvr --wiredTigerCacheSizeGB 5
volumes:
- /data/configsrv_db:/data/configdb
ports:
- "27018:27017"
restart:
always
container_name:
configsrv
ulimits:
nofile:
soft: 300000
hard: 300000
rs2_node:
image: mongo
command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 10240 --replSet rs2 --directoryperdb --port 27017 --shardsvr
volumes:
- /data/rs2_node_db:/data/db
ports:
- "27019:27017"
restart:
always
container_name:
rs2_node
ulimits:
nofile:
soft: 300000
hard: 300000
rs1_arbiter:
image: mongo
command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 1024 --replSet rs1 --directoryperdb --port 27017 --shardsvr --wiredTigerCacheSizeGB 1 --nojournal --smallfiles
volumes:
- /data/rs1_arbiter_db:/data/db
ports:
- "27020:27017"
restart:
always
container_name:
rs1_arbiter
ulimits:
nofile:
soft: 300000
hard: 300000
router:
image: mongo
command: mongos --keyFile /data/db/mongodb-keyfile --configdb configrs/ServerA:27018,ServerB:27018,ServerC:27018
ports:
- "27017:27017"
volumes:
- /data/router_db:/data/db
restart:
always
container_name:
router
ulimits:
nofile:
soft: 300000
hard: 300000

ServerD配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
version: '2'
services:
rs2_node:
image: mongo
command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 10240 --replSet rs2 --directoryperdb --port 27017 --shardsvr
volumes:
- /data/rs2_node_db:/data/db
ports:
- "27019:27017"
restart:
always
container_name:
rs2_node
ulimits:
nofile:
soft: 300000
hard: 300000
router:
image: mongo
command: mongos --keyFile /data/db/mongodb-keyfile --configdb configrs/ServerA:27018,ServerB:27018,ServerC:27018
ports:
- "27017:27017"
volumes:
- /data/router_db:/data/db
restart:
always
container_name:
router
ulimits:
nofile:
soft: 300000
hard: 300000

启动前准备工作

  • 创建mongodb-keyfile文件
1
2
openssl rand -base64 741 > mongodb-keyfile
chmod 600 mongodb-keyfile
  • 创建宿主机的volume文件夹

初始化Config节点

重要:在初始化启动前需要去掉docker-compose.yml配置文件中的--keyFile参数

启动节点

在ServerA、ServerB和ServerC三台服务器上运行命令:docker-compose up -d configsrv

初始化

利用mongo连接到ServerA节点,输入以下命令创建管理用户:

1
2
3
4
5
use admin
db.createUser({user: "mongoUserAdmin", pwd: "123456", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })
db.createUser({user: "mongoRootAdmin", pwd: "123456", roles: [ { role: "root", db: "admin" } ] })

初始化ReplicaSet信息

1
2
3
4
5
6
7
8
9
10
11
rs.initiate(
{
_id: "configrs",
configsvr: true,
members: [
{ _id : 0, host : "ServerA:27018" },
{ _id : 1, host : "ServerB:27018" },
{ _id : 2, host : "ServerC:27018" }
]
}
)

初始化Shard1节点

重要:在初始化启动前需要去掉docker-compose.yml配置文件中的--keyFile参数

启动节点

在ServerA和ServerB两台服务器上运行命令:docker-compose up -d rs1_node

在ServerC服务器上运行命令:docker-compose up -d rs1_arbiter

初始化

利用mongo连接到ServerA节点,创建管理用户

1
2
3
4
5
use admin
db.createUser({user: "mongoUserAdmin", pwd: "123456", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })
db.createUser({user: "mongoRootAdmin", pwd: "123456", roles: [ { role: "root", db: "admin" } ] })

初始化ReplicaSet信息

1
2
3
4
5
6
7
8
9
rs.initiate(
{
_id : "rs1",
members: [
{ _id : 0, host : "ServerA:27019" },
{ _id : 1, host : "ServerB:27019" }
]
}
)

增加Arbiter节点

1
rs.addArb("ServerC:27020")

查看rs状态:rs.status()

初始化Shard2节点

与Shard1节点雷同,只需要修改对应的服务器IP

重启Config和Shard节点

取消--keyFile参数的注释,删掉上述创建的所有container

利用docker-compose再次启动上面所有节点

启动Router节点

使用命令docker-compose up -d router在四台服务器上启动路由节点

配置Cluster

增加Shard节点

使用mongo连接到任意一台服务器的router节点,然后执行以下命令将Shard节点加入到当前Cluster中

1
2
3
4
use admin
db.auth("<username>","<password>")
sh.addShard("rs1/ServerA:27019")
sh.addShard("rs2/ServerD:27019")

启动Sharding

在对collection进行sharding之前一定要先对数据库启动sharding

1
2
sh.enableSharding("<database>")
sh.shardCollection( "<database>.<collection>", { _id : "hashed" } )

参考资料

官方资料:
Deploy a Sharded Cluster
Hashed Sharding