Docker初识:mongo副本集的尝试
Linux系统:CentOS Linux release 7.4.1708 (Core)Docker版本: 17.03.0-ce, build 3a232c8在同一个服务器使用docker搭建一主两从一仲裁四个服务。同一IP不同端口进行测试(不开放认证)。副本集的相关说明在此不多做赘述,其相关服务目录如下:名称IP端口服务器根目录配置目录数据目录...
Linux系统:CentOS Linux release 7.4.1708 (Core)
Docker版本: 17.03.0-ce, build 3a232c8
在同一个服务器使用docker搭建一主两从一仲裁四个服务。同一IP不同端口进行测试(不开放认证)。副本集的相关说明在此不多做赘述,其相关服务目录如下:
名称 | IP | 端口 | 服务器根目录 | 配置目录 | 数据目录 | 日志 |
master | 172.17.0.6 | 27018 | /opt/mongodb/master | /conf/mongodb.conf | /data | /log/master.log |
slaver | 172.17.0.6 | 27019 | /opt/mongodb/slaver | /conf/mongodb.conf | /data | /log/slaver.log |
slaver1 | 172.17.0.6 | 27021 | /opt/mongodb/slaver1 | /conf/mongodb.conf | /data | /log/slaver1.log |
arbiter | 172.17.0.6 | 27020 | /opt/mongodb/arbiter | /conf/mongodb.conf | /data | /log/arbiter.log |
一、mongodb配置文件(mongodb.conf)
1、mongodb.conf(配置的相关说明以master.conf 为例)
#数据存放目录
dbpath=/opt/mongodb/master/data
#日志存放目录
logpath=/opt/mongodb/master/log/master.log
#进程文件,方便停止mongodb
pidfilepath=/opt/mongodb/master/master.pid
#为每一个数据库按照数据库名建立文件夹存放
directoryperdb=true
#以追加的方式记录日志
logappend=true
# 设置副本集名称id与replSet:rs0 要一致,rs0可以是自定义的名称
replSet=rs0
#mongodb所绑定的ip地址
bind_ip=172.17.0.6
#mongodb进程所使用的端口号,默认为27017
port=27018
#mongodb操作日志文件的最大大小。单位为Mb,默认为硬盘剩余空间的5%
oplogSize=10000
#以后台方式运行进程
fork=true
#不预先分配存储
noprealloc=true
2、具体配置文件
/master/conf/mongodb.conf
dbpath=/opt/mongodb/master/data
logpath=/opt/mongodb/master/log/master.log
pidfilepath=/opt/mongodb/master/master.pid
directoryperdb=true
logappend=true
replSet=rs0
bind_ip=172.17.0.6
port=27018
oplogSize=10000
fork=true
noprealloc=true
/slaver/conf/mongodb.conf
dbpath=/opt/mongodb/slaver/data
logpath=/opt/mongodb/slaver/log/slaver.log
pidfilepath=/opt/mongodb/slaver/slaver.pid
directoryperdb=true
logappend=true
replSet=rs0
bind_ip=172.17.0.6
port=27019
oplogSize=10000
fork=true
noprealloc=true
/slaver1/conf/mongodb.conf
dbpath=/opt/mongodb/slaver1/data
logpath=/opt/mongodb/slaver1/log/slaver.log
pidfilepath=/opt/mongodb/slaver1/slaver.pid
directoryperdb=true
logappend=true
replSet=rs0
bind_ip=172.17.0.6
port=27021
oplogSize=10000
fork=true
noprealloc=true
/arbiter/conf/mongodb.conf
dbpath=/opt/mongodb/arbiter/data
logpath=/opt/mongodb/arbiter/log/arbiter.log
pidfilepath=/opt/mongodb/arbiter/arbiter.pid
directoryperdb=true
logappend=true
replSet=rs0
bind_ip=172.17.0.6
port=27020
oplogSize=10000
fork=true
noprealloc=true
二、配置并启动
1、下载mongo
REPOSITORY TAG IMAGE ID CREATED SIZE
mongo latest a0e2e64ac939 3 weeks ago 364MB
2、分别启动master、slaver、slaver1以及arbiter
[root@VM_0_6_centos ~]# docker run --privileged=true -p 27018:27017 --name mongoMaster -v /opt/mongodb/master/conf:/data/configdb -id mongo --replSet "rs0"
[root@VM_0_6_centos ~]# docker run --privileged=true -p 27019:27017 --name mongoSlaver -v /opt/mongodb/slaver/conf:/data/configdb -id mongo --replSet "rs0"
[root@VM_0_6_centos ~]# docker run --privileged=true -p 27020:27017 --name mongoArbiter -v /opt/mongodb/arbiter/conf:/data/configdb -id mongo --replSet "rs0"
[root@VM_0_6_centos ~]# docker run --privileged=true -p 27021:27017 --name mongoSlaver1 -v /opt/mongodb/slaver1/conf:/data/configdb -id mongo --replSet "rs0"
[root@VM_0_6_centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
814101e0946d mongo "docker-entrypoint.s…" 8 days ago Up 19 seconds 0.0.0.0:27021->27017/tcp mongoSlaver1
fe2b6aa8f8c5 mongo "docker-entrypoint.s…" 8 days ago Up 13 seconds 0.0.0.0:27020->27017/tcp mongoArbiter
b5ddab848754 mongo "docker-entrypoint.s…" 8 days ago Up 25 seconds 0.0.0.0:27019->27017/tcp mongoSlaver
4317608a4df0 mongo "docker-entrypoint.s…" 8 days ago Up 34 seconds 0.0.0.0:27018->27017/tcp mongoMaster
[root@VM_0_6_centos ~]#
服务启动后,我们可以开展配置,主,备,仲裁节点,连接到mongoMaster这台服务器,切换到admin数据库,进行配置。
[root@VM_0_6_centos ~]# docker exec -it 4317608a4df0 mongo admin
MongoDB shell version v4.2.2
connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("07983ed5-d9df-40e6-9b5b-2813de1e8837") }
MongoDB server version: 4.2.2
Server has startup warnings:
2020-01-17T09:35:44.908+0000 I STORAGE [initandlisten]
2020-01-17T09:35:44.908+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-01-17T09:35:44.908+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-01-17T09:35:46.060+0000 I CONTROL [initandlisten]
2020-01-17T09:35:46.060+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-01-17T09:35:46.060+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2020-01-17T09:35:46.060+0000 I CONTROL [initandlisten]
2020-01-17T09:35:46.060+0000 I CONTROL [initandlisten]
2020-01-17T09:35:46.060+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-01-17T09:35:46.060+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2020-01-17T09:35:46.060+0000 I CONTROL [initandlisten]
2020-01-17T09:35:46.060+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-01-17T09:35:46.060+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2020-01-17T09:35:46.060+0000 I CONTROL [initandlisten]
2020-01-17T09:35:46.218+0000 I REPL [replexec-0]
2020-01-17T09:35:46.218+0000 I REPL [replexec-0] ** WARNING: This replica set uses arbiters, but readConcern:majority is enabled
2020-01-17T09:35:46.218+0000 I REPL [replexec-0] ** for this node. This is not a recommended configuration. Please see
2020-01-17T09:35:46.218+0000 I REPL [replexec-0] ** https://dochub.mongodb.org/core/psa-disable-rc-majority
2020-01-17T09:35:46.218+0000 I REPL [replexec-0]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
rs0:SECONDARY> rs.initiate({ _id:"rs0", members:[{_id:0,host:'172.17.0.6:27018'},{_id:1,host:'172.17.0.6:27019'},{_id:2,host:'172.17.0.6:27020',arbiterOnly:true},{_id:3,host:'172.17.0.6:27021'}]});
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1577957830, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1577957830, 1)
}
执行成功后,出现如下信息
{ "ok" : 1 }
arbiterOnly:true表示仲裁节点
可以执行rs.status()来检查执行配置结果信息,如果执行成功,会出现如下信息
rs0:SECONDARY> rs.status();
{
"set" : "rs0",
"date" : ISODate("2020-01-17T09:39:38.189Z"),
"myState" : 2,
"term" : NumberLong(4),
"syncingTo" : "172.17.0.6:27018",
"syncSourceHost" : "172.17.0.6:27018",
"syncSourceId" : 1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 3,
"writeMajorityCount" : 3,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1579253976, 1),
"t" : NumberLong(4)
},
"lastCommittedWallTime" : ISODate("2020-01-17T09:39:36.427Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1579253976, 1),
"t" : NumberLong(4)
},
"readConcernMajorityWallTime" : ISODate("2020-01-17T09:39:36.427Z"),
"appliedOpTime" : {
"ts" : Timestamp(1579253976, 1),
"t" : NumberLong(4)
},
"durableOpTime" : {
"ts" : Timestamp(1579253976, 1),
"t" : NumberLong(4)
},
"lastAppliedWallTime" : ISODate("2020-01-17T09:39:36.427Z"),
"lastDurableWallTime" : ISODate("2020-01-17T09:39:36.427Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1579253916, 1),
"lastStableCheckpointTimestamp" : Timestamp(1579253916, 1),
"electionParticipantMetrics" : {
"votedForCandidate" : true,
"electionTerm" : NumberLong(4),
"lastVoteDate" : ISODate("2020-01-17T09:36:05.509Z"),
"electionCandidateMemberId" : 1,
"voteReason" : "",
"lastAppliedOpTimeAtElection" : {
"ts" : Timestamp(1578675015, 1),
"t" : NumberLong(3)
},
"maxAppliedOpTimeInSet" : {
"ts" : Timestamp(1578675015, 1),
"t" : NumberLong(3)
},
"priorityAtElection" : 1,
"newTermStartDate" : ISODate("2020-01-17T09:36:06.417Z"),
"newTermAppliedDate" : ISODate("2020-01-17T09:36:07.260Z")
},
"members" : [
{
"_id" : 0,
"name" : "172.17.0.6:27018",
"ip" : "172.17.0.6",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 222,
"optime" : {
"ts" : Timestamp(1579253976, 1),
"t" : NumberLong(4)
},
"optimeDurable" : {
"ts" : Timestamp(1579253976, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2020-01-17T09:39:36Z"),
"optimeDurableDate" : ISODate("2020-01-17T09:39:36Z"),
"lastHeartbeat" : ISODate("2020-01-17T09:39:37.231Z"),
"lastHeartbeatRecv" : ISODate("2020-01-17T09:39:37.512Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1579253765, 1),
"electionDate" : ISODate("2020-01-17T09:36:05Z"),
"configVersion" : 3
},
{
"_id" : 1,
"name" : "172.17.0.6:27019",
"ip" : "172.17.0.6",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 234,
"optime" : {
"ts" : Timestamp(1579253976, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2020-01-17T09:39:36Z"),
"syncingTo" : "172.17.0.6:27019",
"syncSourceHost" : "172.17.0.6:27019",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 3,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "172.17.0.6:27020",
"ip" : "172.17.0.6",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 210,
"lastHeartbeat" : ISODate("2020-01-17T09:39:37.726Z"),
"lastHeartbeatRecv" : ISODate("2020-01-17T09:39:37.037Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 3
},
{
"_id" : 3,
"name" : "172.17.0.6:27021",
"ip" : "172.17.0.6",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 215,
"optime" : {
"ts" : Timestamp(1579253976, 1),
"t" : NumberLong(4)
},
"optimeDurable" : {
"ts" : Timestamp(1579253976, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2020-01-17T09:39:36Z"),
"optimeDurableDate" : ISODate("2020-01-17T09:39:36Z"),
"lastHeartbeat" : ISODate("2020-01-17T09:39:37.228Z"),
"lastHeartbeatRecv" : ISODate("2020-01-17T09:39:37.753Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "172.17.0.6:27019",
"syncSourceHost" : "172.17.0.6:27019",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 3
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1579253976, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1579253976, 1)
}
rs0:SECONDARY>
rs0:SECONDARY>
其中,"stateStr" : "PRIMARY"表示主节点, "stateStr" : "SECONDARY"表示从节点, "stateStr" : "ARBITER",表示仲裁节点。
3、测试集群节点。
1)、测试主从复制。
打开主节点mongoMaster,创建database--rs0 并且闯将collections--rs,并插入document。查看两个slaver节点
是否应同步。
2)、主节点宕机,是否从从节点选出一个为主节点。选择杀掉主节点。几秒后任意打开其中一个节点,查看是否已有主节点。
rs0:PRIMARY> rs.isMaster();
{
"hosts" : [
"172.17.0.6:27018",
"172.17.0.6:27019",
"172.17.0.6:27021"
],
"arbiters" : [
"172.17.0.6:27020"
],
"setName" : "rs0",
"setVersion" : 3,
"ismaster" : true,
"secondary" : false,
"primary" : "172.17.0.6:27019",
"me" : "172.17.0.6:27019",
"electionId" : ObjectId("7fffffff0000000000000004"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1579416740, 1),
"t" : NumberLong(4)
},
"lastWriteDate" : ISODate("2020-01-19T06:52:20Z"),
"majorityOpTime" : {
"ts" : Timestamp(1579416740, 1),
"t" : NumberLong(4)
},
"majorityWriteDate" : ISODate("2020-01-19T06:52:20Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2020-01-19T06:52:24.052Z"),
"logicalSessionTimeoutMinutes" : 30,
"connectionId" : 716,
"minWireVersion" : 0,
"maxWireVersion" : 8,
"readOnly" : false,
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1579416740, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1579416740, 1)
}
rs0:PRIMARY>
ismaster 是否是主节点,true 是;false 否。
三、问题
1、This node was not started with the replSet option.或not running with --replSet
> rs.initiate(cfg);
{
"ok" : 0,
"errmsg" : "This node was not started with the replSet option",
"code" : 76,
"codeName" : "NoReplicationEnabled"
}
> rs.status();
{
"ok" : 0,
"errmsg" : "not running with --replSet",
"code" : 76,
"codeName" : "NoReplicationEnabled"
}
启动参数或配置文件中必须有参数--replSet "rs0"。
四、备注
1、注意
- 优先级对主从节点切换造成的影响
- 投票节点上线不能超过7个,总结点上线不能超过50个
- 仅支持主节点对外提供读写,仅主节点可以对外提供服务,从节点仅供数据同步
- 主节点宕机,从节点转成主节点后IP改变
- 从多个节点并存,到多节点宕机仅存在一个节点时,无法工作,始终为从节点角色。
- 默认情况下,每个mongo的服务器节点既是数据节点也是投票节点,可以设置为仅具有投票功能的节点(rs.addArb)。
2、添加命令。
在主节点使用rs.add()添加从节点或使用rs.addArb()添加仲裁节点。
_id<int>
host<String> 必填IP:端口
arbiterOnly<boolean>true读写不可见,不会被选举为primary,可以投票,一般用于备份数据
priority<number>成员权重
tags<document>标记
slaveDelay<int>指定一个时间延迟从primary节点同步数据。
votes<number> 指定票数,votes的值一般是0或1
五、总结
以上是在搭建副本集的时候的一点尝试,其中还有一些问题待尝试,比如认证模式,分片等。
更多推荐
所有评论(0)