部署mongodb複製集
複製集結構
只有Primary節點可以執行寫操做
節點故障時Secondary節點能夠切換爲Primary
主要部署步驟
1.使用statefulset啓動N個mongodb節點
//啓動3個pod
mg-5c0ccdbd53df0-0 1/1 Running 0 48s
mg-5c0ccdbd53df0-1 1/1 Running 0 45s
mg-5c0ccdbd53df0-2 1/1 Running 0 43s
2.每一個節點執行如下初始化腳本,進行集羣初始化
#!/bin/bash
[[ -d /etc/mongodb/data/db ]] || mkdir -p /etc/mongodb/data/db
[[ -d /etc/mongodb/log ]] || mkdir -p /etc/mongodb/log
[[ -f /etc/mongodb/mongo.conf ]] || cp /tmp/mongo.conf /etc/mongodb
[[ -f /etc/mongodb/data/db/mongod.lock ]] && rm -rf /etc/mongodb/data/db/mongod.lock
# 設置副本集名稱
grep -w "replSet" /etc/mongodb/mongo.conf || echo "replSet=$RES_NAME" >> /etc/mongodb/mongo.conf
cd /etc/mongodb
# 生成副本集各節點信息
for ((i=0; i<=$RES_NUM-1; i ++))
do
MEMBERS+={_id:$i,host:\""${POD_NAME}-$i.${POD_NAME}.${NAMESPACE}.svc.cluster.local:${PORT}\""},
done
echo $MEMBERS
# 生成keyFile文件
if [ ! -d "/etc/mongodb/keyFile" ]; then
echo $KEY_FILE > keyFile
chmod 600 keyFile
fi
mongod -f mongo.conf --fork --logpath=/etc/mongodb/log/mongo.log
sleep 30s
mongo admin -u ${USERNAME} -p ${PASSWORD} <<EOF
EOF
num=$?
if [[ $num == 0 ]];
then
mongod -f mongo.conf --shutdown
mongod -f mongo.conf |tee -a /etc/mongodb/log/mongo.log
else
# 集羣初始化
mongo admin <<EOF
rs.initiate({_id:"${RES_NAME}",members:[${MEMBERS}]})
EOF
sleep 60s
# 建立用戶
mongo admin <<EOF
db.createRole({role:'sysadmin',roles:[], privileges:[{resource:{anyResource:true},actions:['anyAction']}]})
db.createUser({user: "${USERNAME}", pwd: "${PASSWORD}", roles: [ { role: "sysadmin", db: "admin" } ] } )
EOF
# 開啓登錄憑證
echo "keyFile=/etc/mongodb/keyFile" >> mongo.conf
mongod -f mongo.conf --shutdown
mongod -f mongo.conf |tee -a /etc/mongodb/log/mongo.log
fi
配置文件:
#數據存放目錄
dbpath=/etc/mongodb/data/db
#日誌存放目錄
logpath=/etc/mongodb/log/mongo.log
#進程文件,方便中止mongodb
pidfilepath=/etc/mongodb/mongo.pid
#開啓後爲每個數據庫按照數據庫名創建文件夾存放
directoryperdb=true
#以追加的方式記錄日誌
logappend=true
#指定mongodb綁定的ip地址
bind_ip=0.0.0.0
#mongodb進程所使用的端口號,默認爲27017
port=27017
#mongodb操做日誌文件的最大大小。單位爲Mb,默認爲硬盤剩餘空間的5%
oplogSize=10000
#不預先分配存儲
noprealloc=true
測試
登錄認證
//無用戶登錄
root@mg-5c0ccdbd53df0-0:/tmp# mongo admin
MongoDB shell version: 3.2.21
connecting to: admin
repls:SECONDARY> show dbs;
2018-12-09T08:59:25.819+0000 E QUERY [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
shellHelper.show@src/mongo/shell/utils.js:781:19
shellHelper@src/mongo/shell/utils.js:671:15
@(shellhelp2):1:1
//使用密碼登錄
root@mg-5c0ccdbd53df0-0:/tmp# mongo admin -u root -p FGL1184JP1CUIyQOR5fSRMTSv1GwSDRE
MongoDB shell version: 3.2.21
connecting to: admin
Server has startup warnings:
2018-12-09T08:50:19.509+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-12-09T08:50:19.509+0000 I CONTROL [initandlisten]
2018-12-09T08:50:19.509+0000 I CONTROL [initandlisten]
2018-12-09T08:50:19.509+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-12-09T08:50:19.509+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-12-09T08:50:19.509+0000 I CONTROL [initandlisten]
2018-12-09T08:50:19.509+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-12-09T08:50:19.509+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-12-09T08:50:19.509+0000 I CONTROL [initandlisten]
repls:SECONDARY> rs.slaveOk()
repls:SECONDARY> show dbs;
admin 0.000GB
local 0.000GB
repls:SECONDARY>
故障切換
//啓動3個pod
mg-5c0ccdbd53df0-0 1/1 Running 0 48s
mg-5c0ccdbd53df0-1 1/1 Running 0 45s
mg-5c0ccdbd53df0-2 1/1 Running 0 43s
//查看狀態
repls:SECONDARY> rs.status()
{
"set" : "repls",
"date" : ISODate("2018-12-09T09:07:52.090Z"),
"myState" : 2,
"term" : NumberLong(8),
"syncingTo" : "mg-5c0ccdbd53df0-1.mg-5c0ccdbd53df0.itfarm3.svc.cluster.local:27017",
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "mg-5c0ccdbd53df0-0.mg-5c0ccdbd53df0.itfarm3.svc.cluster.local:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1054,
"optime" : {
"ts" : Timestamp(1544345433, 1),
"t" : NumberLong(8)
},
"optimeDate" : ISODate("2018-12-09T08:50:33Z"),
"syncingTo" : "mg-5c0ccdbd53df0-1.mg-5c0ccdbd53df0.itfarm3.svc.cluster.local:27017",
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "mg-5c0ccdbd53df0-1.mg-5c0ccdbd53df0.itfarm3.svc.cluster.local:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1047,
"optime" : {
"ts" : Timestamp(1544345433, 1),
"t" : NumberLong(8)
},
"optimeDate" : ISODate("2018-12-09T08:50:33Z"),
"lastHeartbeat" : ISODate("2018-12-09T09:07:50.171Z"),
"lastHeartbeatRecv" : ISODate("2018-12-09T09:07:51.778Z"),
"pingMs" : NumberLong(0),
"electionTime" : Timestamp(1544345432, 1),
"electionDate" : ISODate("2018-12-09T08:50:32Z"),
"configVersion" : 1
},
{
"_id" : 2,
"name" : "mg-5c0ccdbd53df0-2.mg-5c0ccdbd53df0.itfarm3.svc.cluster.local:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1037,
"optime" : {
"ts" : Timestamp(1544345433, 1),
"t" : NumberLong(8)
},
"optimeDate" : ISODate("2018-12-09T08:50:33Z"),
"lastHeartbeat" : ISODate("2018-12-09T09:07:50.095Z"),
"lastHeartbeatRecv" : ISODate("2018-12-09T09:07:47.378Z"),
"pingMs" : NumberLong(0),
"configVersion" : 1
}
],
"ok" : 1
}
//可知集羣狀態信息
mg-5c0ccdbd53df0-0 SECONDARY
mg-5c0ccdbd53df0-1 PRIMARY
mg-5c0ccdbd53df0-2 SECONDARY
//模擬故障,將主節點宕機
$ kubectl.exe delete pod mg-5c0ccdbd53df0-1
pod "mg-5c0ccdbd53df0-1" deleted
//主節點變成了mg-5c0ccdbd53df0-0
repls:SECONDARY> rs.status()
{
"set" : "repls",
"date" : ISODate("2018-12-09T09:34:00.698Z"),
"myState" : 1,
"term" : NumberLong(9),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "mg-5c0ccdbd53df0-0.mg-5c0ccdbd53df0.itfarm3.svc.cluster.local:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 2622,
"optime" : {
"ts" : Timestamp(1544347941, 2),
"t" : NumberLong(9)
},
"optimeDate" : ISODate("2018-12-09T09:32:21Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1544347941, 1),
"electionDate" : ISODate("2018-12-09T09:32:21Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "mg-5c0ccdbd53df0-1.mg-5c0ccdbd53df0.itfarm3.svc.cluster.local:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 62,
"optime" : {
"ts" : Timestamp(1544347941, 2),
"t" : NumberLong(9)
},
"optimeDate" : ISODate("2018-12-09T09:32:21Z"),
"lastHeartbeat" : ISODate("2018-12-09T09:33:59.917Z"),
"lastHeartbeatRecv" : ISODate("2018-12-09T09:33:57.395Z"),
"pingMs" : NumberLong(0),
"configVersion" : 1
},
{
"_id" : 2,
"name" : "mg-5c0ccdbd53df0-2.mg-5c0ccdbd53df0.itfarm3.svc.cluster.local:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 2606,
"optime" : {
"ts" : Timestamp(1544347941, 2),
"t" : NumberLong(9)
},
"optimeDate" : ISODate("2018-12-09T09:32:21Z"),
"lastHeartbeat" : ISODate("2018-12-09T09:33:59.822Z"),
"lastHeartbeatRecv" : ISODate("2018-12-09T09:33:59.714Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "mg-5c0ccdbd53df0-0.mg-5c0ccdbd53df0.itfarm3.svc.cluster.local:27017",
"configVersion" : 1
}
],
"ok" : 1
}
高可用性
//主節點存儲模擬數據
repls:PRIMARY> use admin
switched to db admin
repls:PRIMARY> db.stu_name.insert({"name":"zhangsan"})
WriteResult({ "nInserted" : 1 })
repls:PRIMARY> db.stu_name.insert({"name":"lisi"})
WriteResult({ "nInserted" : 1 })
repls:PRIMARY>
//從節點查看數據
repls:SECONDARY> rs.slaveOk()
repls:SECONDARY> db.stu_name.find().pretty()
{ "_id" : ObjectId("5c0ce324d659094439a720c8"), "name" : "zhangsan" }
{ "_id" : ObjectId("5c0ce33bd659094439a720c9"), "name" : "lisi" }
repls:SECONDARY>
//模擬節點故障
//刪除全部pod
$ kubectl.exe delete po mg-5c0ccdbd53df0-0 mg-5c0ccdbd53df0-1 mg-5c0ccdbd53df0-2
pod "mg-5c0ccdbd53df0-0" deleted
pod "mg-5c0ccdbd53df0-1" deleted
pod "mg-5c0ccdbd53df0-2" deleted
$ kubectl.exe get po
mg-5c0ccdbd53df0-0 1/1 Terminating 0 1h
mg-5c0ccdbd53df0-1 1/1 Terminating 0 35m
mg-5c0ccdbd53df0-2 1/1 Terminating 0 1h
//重啓後進入查看節點,數據恢復
repls:SECONDARY> db.stu_name.find().pretty()
{ "_id" : ObjectId("5c0ce324d659094439a720c8"), "name" : "zhangsan" }
{ "_id" : ObjectId("5c0ce33bd659094439a720c9"), "name" : "lisi" }
repls:SECONDARY>
測試job
repls:PRIMARY> show dbs;
admin 0.000GB
local 0.000GB
mdb5c0dca494d748 0.000GB
repls:PRIMARY> show users;
{
"_id" : "mdb5c0dca494d748.root",
"user" : "root",
"db" : "mdb5c0dca494d748",
"roles" : [
{
"role" : "readWrite",
"db" : "mdb5c0dca494d748"
}
]
}
repls:PRIMARY> db.mdb5c0dca494d748_created_log.find().pretty()
{
"_id" : ObjectId("5c0dca4c69a1103a64c9fe45"),
"created_at" : "Mon Dec 10 02:07:07 UTC 2018"
}