Mongodb Shard集群中的复制集主备切换

Mongodb shard active-Standby switch

Mongodb Shard集群中的复制集主备切换
Page content

最近公司准备上线新的业务,需要压测MongoDB Shard集群采用多个机器来回放日志进行压测, 使用js解析以往的日志,并拼装成新的请求来请求服务,进行压测。使用25台机器并发发起 请求,每分钟20000个请求,MongoDB Shard集群的primary负载很高,CPU load 在88%-98% 之间,目标是使用50台机器并发发起请求,所以需要升级机器的配置。公司使用的是AWS 机器,修改实例配置需要停机。

所以将primary升级之前需要将其角色变更为secondary(类似mysql中的slave,但是secondary在 primary停机后可以选择变更为primary提供服务),这样停止secondary就对业务零影响。

切换primary为secondary步骤

# 1. 在需要提升primary的机器修改配置
shard4:SECONDARY> conf=rs.conf()

# 2. 查看需要提为primary的member的优先级
shard4:SECONDARY> conf.members[1].priority
10
# 3. 修改优先级到高于目前复制集中的primary的优先级
shard4:SECONDARY> conf.members[1].priority = 21

# 4. primary stepdown
shard4:PRIMARY> rs.stepDown(60)

# 5. SECONDARY reconfig
shard4:SECONDARY> rs.reconfig(conf)

# 5. rs.status()
shard4:SECONDARY> rs.status()
shard4:PRIMARY> rs.status()
  • 即:
   SECONDARY                               PRIMARY
conf=rs.conf()
     |
     |
     |
conf.members[1].priority = xx
     |
     |--------------------------------->rs.stepDown(60)
                                             |
                                             |
                                             |
rs.reconfig(conf)<---------------------------|

切换操作后shard 复制集的日志

2020-09-08T19:50:21.197+0800 I REPL     [conn743] replSetReconfig admin command received from client  # 接收到客户端的命令
2020-09-08T19:50:21.198+0800 I REPL     [conn743] replSetReconfig config object with 3 members parses ok  # 复制集有三个成员(primary,secondary,hidden)
2020-09-08T19:50:21.198+0800 I REPL     [ReplicationExecutor] New replica set config in use: { _id: "shard3", version: 3, protocolVersion: 1, members: [ {
_id: 0, host: "mongodb-cake-m3:22003", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 20.0, tags: {}, slaveDelay: 0, votes: 1 }, { _id: 1,
host: "mongodb-cake-s3:22003", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 10.0, tags: {}, slaveDelay: 0, votes: 1 }, { _id: 2, host:
"mongodb-cake-h3:22003", arbiterOnly: false, buildIndexes: true, hidden: true, priority: 0.0, tags: {}, slaveDelay: 0, votes: 1 } ], settings: {
chainingAllowed: true, heartbeatIntervalMillis: 2000, heartbeatTimeoutSecs: 10, electionTimeoutMillis: 10000, getLastErrorModes: {}, getLastErrorDefaults: { w:
1, wtimeout: 0 }, replicaSetId: ObjectId('5f2bd80b03a41ce2ad492293') } } 
2020-09-08T19:50:21.198+0800 I REPL     [ReplicationExecutor] This node is mongodb-cake-m3:22003 in the config   # 执行rs.reconfig()命令的机器