1. copy集原理
MongoDBcopy集 is 一组maintenance相同data集 MongoDBserver, providingdata冗余 and high availability性. copy集通常由一个主node (Primary) and many 个 from node (Secondary) 组成.
1.1 copy集architecture
copy集 basicarchitectureincluding:
- 主node (Primary) : processing所 has 写operation, 记录operation to oplog in
- from node (Secondary) : copy主node oplog 并application to 自己 data集
- 仲裁node (Arbiter) : 不storedata, 仅参 and 选举过程
1.2 copymechanism
MongoDBusing oplog (operationlog) 来implementationcopy:
- 主node将所 has 写operation记录 to oplog in
- from node定期轮询主node获取 new oplog 条目
- from nodeapplication这些operation to 自己 data集, 保持 and 主nodesynchronization
// 查看 oplog big small rs.printReplicationInfo();
2. configurationmethod
configurationMongoDBcopy集 步骤such as under :
2.1 准备server
至 few 需要3台server来构建一个basic copy集 (1个主node, 2个 from node) .
2.2 configurationMongoDBinstance
in 每台server on 启动MongoDBinstance, 并指定copy集名称:
// server1 mongod --port 27017 --dbpath /data/db1 --replSet rs0 // server2 mongod --port 27017 --dbpath /data/db2 --replSet rs0 // server3 mongod --port 27017 --dbpath /data/db3 --replSet rs0
2.3 初始化copy集
连接 to 其in一个MongoDBinstance, 初始化copy集:
// 连接 to MongoDB
mongo --port 27017
// 初始化copy集
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "server1:27017" },
{ _id: 1, host: "server2:27017" },
{ _id: 2, host: "server3:27017" }
]
});
2.4 verificationcopy集status
using rs.status() commandscheckcopy集status:
// checkcopy集status rs.status();
3. failure转移
MongoDBcopy集 failure转移mechanism确保 in 主node不可用时, systemable to自动选举 new 主node.
3.1 选举过程
当主node不可用时, from node会触发选举过程:
- 检测 to 主nodefailure (心跳检测失败)
- from node自我提名
- othernode投票
- 获得 many 数票 node成 for new 主node
3.2 选举条件
node被选 for new 主node 条件:
- able to and big many 数node通信
- datasynchronization程度 high (oplog 最 new )
- priority (priority) 值 high (such as果设置)
3.3 failure转移时间
failure转移通常需要 10-30 秒completion, including:
- 检测主nodefailure 时间 (默认10秒)
- 选举 new 主node 时间 (通常几秒钟)
注意: in failure转移期间, copy集可能暂时无法processing写operation, 但仍可以processing读operation ( from from node) .
4. monitor
monitorMongoDBcopy集 healthystatus for 于确保system stable run至关 important .
4.1 in 置monitorcommands
// checkcopy集status rs.status() // checkcopylatency rs.printSlaveReplicationInfo() // checkcopyinformation rs.printReplicationInfo()
4.2 关键monitor指标
- copylatency: from node and 主node 时间差
- oplog 窗口: oplog 覆盖 时间范围
- 心跳status: node间通信status
- 选举status: is 否 has 正 in for 选举
4.3 out 部monitortool
- MongoDB Atlas: MongoDB官方 云monitorservice
- MongoDB Compass: graph形化managementtool, package含monitorfunctions
- Nagios/Zabbix: through插件monitorMongoDB
- Prometheus + Grafana: usingMongoDB exporter收集指标
5. 实践case: 构建high availabilityMongoDBcluster
fake设我们需要 for 一个电商system构建high availability MongoDBcluster, 具体步骤such as under :
5.1 clusterplanning
- 3台server: server1 (主node) , server2 ( from node) , server3 ( from node)
- 端口: 27017
- copy集名称: rs0
5.2 configuration步骤
// 1. in 所 has server on creationdataTable of Contents
mkdir -p /data/db
// 2. in 所 has server on 启动MongoDBinstance
mongod --port 27017 --dbpath /data/db --replSet rs0 --bind_ip_all
// 3. 连接 to 其in一个instance并初始化copy集
mongo --port 27017
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "server1:27017" },
{ _id: 1, host: "server2:27017" },
{ _id: 2, host: "server3:27017" }
]
});
// 4. verificationcopy集status
rs.status()
5.3 testfailure转移
// 1. 查看当 before 主node rs.isMaster() // 2. mock主nodefailure ( in 主node on 执行) use admin db.shutdownServer() // 3. check new 主node ( in from node on 执行) rs.isMaster()
6. 互动练习
issues1: MongoDBcopy集 作用 is what?
A. improvingqueryperformance
B. providingdata冗余 and high availability性
C. 增加store容量
D. 简化management
issues2: copy集 最 small 推荐configuration is what?
A. 1个node
B. 2个node
C. 3个node
D. 5个node
issues3: failure转移过程in, copy集 is 否可以processing写operation?
A. 可以, 完全不影响
B. 暂时不可用, 直 to new 主node选举completion
C. 只能processing部分写operation
D. 取决于configuration
issues4: such as何查看copylatency?
A. rs.status()
B. rs.printSlaveReplicationInfo()
C. rs.printReplicationInfo()
D. db.serverStatus()