Sharding:Shard Cluster 删除 Shard 节点

上篇Blog学习了给 Shard Cluster 增加 shard 节点,接下来学习下删除节点。

一 删除 Shard 节点基本思路

  1. 移走要删除 Shard 节点的数据;
  2. 确保要删除的 shard 节点不是任何库的 Primary 节点,如果是,将这些库的 Primary 节点移到其它 shard 节点;
  3. 从 Shard Cluster 配置中删除 shard 节点信息。

二 删除 Shard 节点步骤

2.1 查看当前 Shard Cluster 状态

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
mongos> sh.status();  
--- Sharding Status ---
sharding version: { "_id" : 1, "version" : 3 }
shards:
{ "_id" : "shard0000", "host" : "redhatB.example.com:5281" }
{ "_id" : "shard0001", "host" : "redhatB.example.com:5282" }
{ "_id" : "shard0002", "host" : "redhatB.example.com:5283" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0000" }
{ "_id" : "francs", "partitioned" : true, "primary" : "shard0000" }
francs.test_1 chunks:
shard0000 2
shard0001 2
shard0002 3
{ "id" : { $minKey : 1 } } -->> { "id" : 1 } on : shard0000 Timestamp(5000, 0)
{ "id" : 1 } -->> { "id" : 7282 } on : shard0001 Timestamp(5000, 1)
{ "id" : 7282 } -->> { "id" : 15651 } on : shard0001 Timestamp(4000, 3)
{ "id" : 15651 } -->> { "id" : 29868 } on : shard0002 Timestamp(4000, 4)
{ "id" : 29868 } -->> { "id" : 37149 } on : shard0002 Timestamp(5000, 2)
{ "id" : 37149 } -->> { "id" : 48037 } on : shard0002 Timestamp(5000, 3)
{ "id" : 48037 } -->> { "id" : { $maxKey : 1 } } on : shard0000 Timestamp(4000, 1)
{ "_id" : "records", "partitioned" : false, "primary" : "shard0000" }
{ "_id" : "fracns", "partitioned" : false, "primary" : "shard0002" }

mongos> db.test_1.count();
100000

备注:当前有三个 shard 节点,其中 test_1 集合有 10000 个文档。目标删除 “redhatB.example.com:5283” 节点。

2.2 从目标 shard 中转移数据
使用 removeShard 命令就能将 shard 节点的数据转移到其它节点,前提是已开启balancer 进程。

2.2.1 查看 balancer 进程是否已开启

1
2
mongos> sh.getBalancerState();  
true

2.2.2 转移数据

1
2
3
4
5
6
7
8
9
10
11
12
13
mongos> use admin;  
switched to db admin

mongos> db.runCommand({removeshard:"redhatB.example.com:5283"});{
"msg" : "draining started successfully",
"state" : "started",
"shard" : "shard0002",
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [
"fracns"
],
"ok" : 1
}

备注:这个步骤会将目标删除的 shard 节点数据转移到其它 shard 节点,数据量越大,耗时越长。

2.3 核查迁移状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mongos> db.runCommand({removeshard:"redhatB.example.com:5283"});  
{
"msg" : "draining ongoing",
"state" : "ongoing",
"remaining" : {
"chunks" : NumberLong(0),
"dbs" : NumberLong(1)
},
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [
"fracns"
],
"ok" : 1
}

备注: 提示所删除的 shard 节点是某个库的 Primary 节点,再根据 2.1 红色字体信息,发现 “redhatB.example.com:5283” 节点是 fracns 库的主节点。

2.4 转移 Unsharded Databases

1
2
3
4
5
mongos> use admin;  
switched to db admin

mongos> db.runCommand( { movePrimary: "fracns", to: "redhatB.example.com:5281" }) ;
{ "primary " : "shard0000:redhatB.example.com:5281", "ok" : 1 }

备注:这个命令将 fracns 库的主节点转移到 “redhatB.example.com:5281” shard 节点。

2.5 查看 Shard Cluster 状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
mongos> sh.status();  
--- Sharding Status ---
sharding version: { "_id" : 1, "version" : 3 }
shards:
{ "_id" : "shard0000", "host" : "redhatB.example.com:5281" }
{ "_id" : "shard0001", "host" : "redhatB.example.com:5282" }
{ "_id" : "shard0002", "draining" : true, "host" : "redhatB.example.com:5283" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0000" }
{ "_id" : "francs", "partitioned" : true, "primary" : "shard0000" }
francs.test_1 chunks:
shard0000 4
shard0001 3
{ "id" : { $minKey : 1 } } -->> { "id" : 1 } on : shard0000 Timestamp(5000, 0)
{ "id" : 1 } -->> { "id" : 7282 } on : shard0001 Timestamp(5000, 1)
{ "id" : 7282 } -->> { "id" : 15651 } on : shard0001 Timestamp(4000, 3)
{ "id" : 15651 } -->> { "id" : 29868 } on : shard0000 Timestamp(6000, 0)
{ "id" : 29868 } -->> { "id" : 37149 } on : shard0001 Timestamp(7000, 0)
{ "id" : 37149 } -->> { "id" : 48037 } on : shard0000 Timestamp(8000, 0)
{ "id" : 48037 } -->> { "id" : { $maxKey : 1 } } on : shard0000 Timestamp(4000, 1)
{ "_id" : "records", "partitioned" : false, "primary" : "shard0000" }
{ "_id" : "fracns", "partitioned" : false, "primary" : "shard0000" }

备注:注意最后一行,库 fracns 的主节点已变为 shard0000 了。

2.6 Finalize the Migration

1
2
3
4
5
6
7
mongos> db.runCommand({removeshard:"redhatB.example.com:5283"});  
{
"msg" : "removeshard completed successfully",
"state" : "completed",
"shard" : "shard0002",
"ok" : 1
}

备注:在转移 shard 节点数据之后,再次执行 removeshard 命令,收到以上信息,表示删除 shard 节点成功。

三:验证

3.1 查看 Shard Cluster 状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
mongos> sh.status();  
--- Sharding Status ---
sharding version: { "_id" : 1, "version" : 3 }
shards:
{ "_id" : "shard0000", "host" : "redhatB.example.com:5281" }
{ "_id" : "shard0001", "host" : "redhatB.example.com:5282" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0000" }
{ "_id" : "francs", "partitioned" : true, "primary" : "shard0000" }
francs.test_1 chunks:
shard0000 4
shard0001 3
{ "id" : { $minKey : 1 } } -->> { "id" : 1 } on : shard0000 Timestamp(5000, 0)
{ "id" : 1 } -->> { "id" : 7282 } on : shard0001 Timestamp(5000, 1)
{ "id" : 7282 } -->> { "id" : 15651 } on : shard0001 Timestamp(4000, 3)
{ "id" : 15651 } -->> { "id" : 29868 } on : shard0000 Timestamp(6000, 0)
{ "id" : 29868 } -->> { "id" : 37149 } on : shard0001 Timestamp(7000, 0)
{ "id" : 37149 } -->> { "id" : 48037 } on : shard0000 Timestamp(8000, 0)
{ "id" : 48037 } -->> { "id" : { $maxKey : 1 } } on : shard0000 Timestamp(4000, 1)
{ "_id" : "records", "partitioned" : false, "primary" : "shard0000" }
{ "_id" : "fracns", "partitioned" : false, "primary" : "shard0000" }

备注:上面已经没有 “redhatB.example.com:5283” 节点信息了。

3.2 验证集合 test_1 数据

1
2
3
4
mongos> use francs;use francs;  
switched to db francs
mongos> db.test_1.count();db.test_1.count();
100000

四 停 “redhatB.example.com:5283” 节点

1
2
3
4
5
[shard@redhatB ~]$ ps -ef | grep 5283  
shard 18089 17986 0 14:23 pts/2 00:00:00 grep 5283
shard 25864 1 0 02:51 ? 00:04:52 mongod -f /pgdata_xc/shard/shard3/shard3_5283.conf

[shard@redhatB ~]$ kill 25864

备注:以上就是完整的删除节点步骤。

五 附

primary shard :如果某个库已开启 sharding 功能,并将未分片的集合存储在一个单一的 shard 节点上,那么这个 shard 节点被称为这个库的 primary shard。

六 参考

http://docs.mongodb.org/manual/tutorial/remove-shards-from-cluster/
http://docs.mongodb.org/manual/reference/commands/#removeShard
http://docs.mongodb.org/manual/reference/glossary/#term-primary-shard

原创文章,作者:745907710,如若转载,请注明出处:https://blog.ytso.com/237930.html

(0)
上一篇 2022年1月29日
下一篇 2022年1月29日

相关推荐

发表回复

登录后才能评论