Mongodb ReplicaSet 集群由Primary,Secondary,Arbiter这3个角色组成,如下图所示:
假设有3台机器,如下所示
机器名称 | IP地址 | 系统版本 | 用途规划 |
---|---|---|---|
k8s-node3 | 192.168.0.109 | Centos7 | 主节点 |
k8s-node4 | 192.168.0.110 | Centos7 | 从节点 |
k8s-node5 | 192.168.0.111 | Centos7 | 观察者节点 |
从Mongodb的官方网站上下载Mongodb,在192.168.0.109服务器上执行如下操作
[root@localhost software]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.6.tgz
[root@localhost software]# tar -xvf mongodb-linux-x86_64-rhel70-4.4.6.tgz
mongodb-linux-x86_64-rhel70-4.4.6/LICENSE-Community.txt
mongodb-linux-x86_64-rhel70-4.4.6/MPL-2
mongodb-linux-x86_64-rhel70-4.4.6/README
mongodb-linux-x86_64-rhel70-4.4.6/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel70-4.4.6/bin/install_compass
mongodb-linux-x86_64-rhel70-4.4.6/bin/mongo
mongodb-linux-x86_64-rhel70-4.4.6/bin/mongod
mongodb-linux-x86_64-rhel70-4.4.6/bin/mongos
[root@localhost software]# ls
mongodb-linux-x86_64-rhel70-4.4.6 mongodb-linux-x86_64-rhel70-4.4.6.tgz
[root@localhost software]# mv mongodb-linux-x86_64-rhel70-4.4.6 mongodb-4.4.6
[root@localhost software]# mv mongodb-4.4.6/ /home/
[root@localhost software]# cd /home/mongodb-4.4.6/
[root@localhost mongodb-4.4.6]# ls
bin LICENSE-Community.txt MPL-2 README THIRD-PARTY-NOTICES
[root@localhost mongodb-4.4.6]# mkdir conf
[root@localhost mongodb-4.4.6]# mkdir data
[root@localhost mongodb-4.4.6]# mkdir log
[root@localhost mongodb-4.4.6]# cd conf
[root@localhost conf]# touch mongodb.conf
#编辑配置文件
[root@localhost conf]# vi mongodb.conf
配置文件内容如下:
dbpath=/home/mongodb/data
logpath=/home/mongodb/log/mongodb.log
directoryperdb=true
logappend=true
replSet=Zealink
bind_ip=0.0.0.0
port=27017
oplogSize=10000
fork=true
说明:
[root@localhost bin]# /home/mongodb-4.4.6/bin/mongod -f /home/mongodb-4.4.6/conf/mongodb.conf
如此,mongodb服务安装完毕,在其他两台服务器上执行上述操作。
在3台服务器上随便选择一台,使用mongo客户端连接到mongodb服务器
[root@localhost bin]#/home/mongodb-4.4.6/bin/mongo -port 28010
MongoDB shell version:1.8.1
connecting to:127.0.0.1:28010/test
>config_rs1={_id:'Zealink',members:[
...{_id:0,host:'192.168.0.109:27017',priority:2},
...{_id:1,host:'192.168.0.109:27017',priority:1},
...{_id:2,host:'192.168.0.109:27017',arbiterOnly:true}]
...
}
>rs.initiate(config_rs1);
{
"info":"Config now saved locally.Should come online in about a minute.",
"ok":1
}
>rs.status()
{
"set" : "Zealink",
"date" : ISODate("2021-08-26T16:49:53.176Z"),
"myState" : 1,
"term" : NumberLong(5),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 3,
"writableVotingMembersCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1629996585, 1),
"t" : NumberLong(5)
},
"lastCommittedWallTime" : ISODate("2021-08-26T16:49:45.722Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1629996585, 1),
"t" : NumberLong(5)
},
"readConcernMajorityWallTime" : ISODate("2021-08-26T16:49:45.722Z"),
"appliedOpTime" : {
"ts" : Timestamp(1629996585, 1),
"t" : NumberLong(5)
},
"durableOpTime" : {
"ts" : Timestamp(1629996585, 1),
"t" : NumberLong(5)
},
"lastAppliedWallTime" : ISODate("2021-08-26T16:49:45.722Z"),
"lastDurableWallTime" : ISODate("2021-08-26T16:49:45.722Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1629996532, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "priorityTakeover",
"lastElectionDate" : ISODate("2021-08-09T01:40:40.529Z"),
"electionTerm" : NumberLong(5),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(1628473230, 1),
"t" : NumberLong(4)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1628473230, 1),
"t" : NumberLong(4)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 3,
"electionTimeoutMillis" : NumberLong(10000),
"priorPrimaryMemberId" : 1,
"targetCatchupOpTime" : {
"ts" : Timestamp(1628473240, 1),
"t" : NumberLong(4)
},
"numCatchUpOps" : NumberLong(1),
"newTermStartDate" : ISODate("2021-08-09T01:40:41.343Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2021-08-09T01:40:41.689Z")
},
"members" : [
{
"_id" : 0,
"name" : "192.168.0.109:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1523462,
"optime" : {
"ts" : Timestamp(1629996585, 1),
"t" : NumberLong(5)
},
"optimeDate" : ISODate("2021-08-26T16:49:45Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1628473240, 1),
"electionDate" : ISODate("2021-08-09T01:40:40Z"),
"configVersion" : 1,
"configTerm" : 5,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.0.110:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1519722,
"optime" : {
"ts" : Timestamp(1629996585, 1),
"t" : NumberLong(5)
},
"optimeDurable" : {
"ts" : Timestamp(1629996585, 1),
"t" : NumberLong(5)
},
"optimeDate" : ISODate("2021-08-26T16:49:45Z"),
"optimeDurableDate" : ISODate("2021-08-26T16:49:45Z"),
"lastHeartbeat" : ISODate("2021-08-26T16:49:52.993Z"),
"lastHeartbeatRecv" : ISODate("2021-08-26T16:49:52.998Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "192.168.0.110:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 5
},
{
"_id" : 3,
"name" : "192.168.0.111:27017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 1523459,
"lastHeartbeat" : ISODate("2021-08-26T16:49:51.559Z"),
"lastHeartbeatRecv" : ISODate("2021-08-26T16:49:52.451Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 5
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1629996585, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1629996585, 1)
}
rs.status()通过这个命令可以查看集群的状态,各个成员节点的状态是否正常。
rs.stepDown()这个命令可以强制primary和standard节点角色互换,从而验证是否能够实现failover功能。或者primary上执行 use admin;db.shutdownServer();断掉primary看效果。
rs.isMaster()显示当前哪个节点是primary节点
MongoDB replica sets中通过设置priority的值来决定优先权的大小,这个值的范围是0–100,值越大,优先权越高。
默认情况下mongdb是以普通程序运行的,如果服务器异常重启,需要手工去启动程序,给运维造成了极大的不便,所以我们需要将其配置为服务运行的方式,并设置成为开机自启动。
#!/bin/sh
#
#mongod - Startup script for mongod
#
# chkconfig: - 85 15
# description: Mongodb database.
# processname: mongod
# Source function library
. /etc/rc.d/init.d/functions
# things from mongod.conf get there by mongod reading it
# OPTIONS
OPTIONS=" --config /home/mongodb-4.4.6/conf/mongodb.conf &"
#mongod
mongod="/home/mongodb-4.4.6/bin/mongo"
lockfile=/var/lock/subsys/mongod
start()
{
echo -n $"Starting mongod-arbiter: "
daemon $mongod $OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $lockfile
}
stop()
{
echo -n $"Stopping mongod-arbiter: "
killproc $mongod -QUIT
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $lockfile
}
restart () {
stop
start
}
ulimit -n 12000
RETVAL=0
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload|force-reload)
restart
;;
condrestart)
[ -f $lockfile ] && restart || :
;;
status)
status $mongod
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
RETVAL=1
esac
exit $RETVAL