kubernetes上部署mongodb高可用记录

部署mongodb复制集

复制集结构

只有Primary节点能够执行写操作

kubernetes上部署mongodb高可用记录

节点故障时Secondary节点可以切换为Primary

kubernetes上部署mongodb高可用记录

主要部署步骤

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"
}

相关推荐