资讯专栏INFORMATION COLUMN

kubernetes上部署mongodb高可用记录

_DangJin / 1107人阅读

摘要:部署复制集复制集结构只有节点能够执行写操作节点故障时节点可以切换为主要部署步骤使用启动个节点启动个每个节点执行以下初始化脚本,进行集群初始化设置副本集名称生成副本集各节点信息生成文件

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/19424.html

相关文章

  • 跨集群服务——如何利用Kubernetes 1.3实现跨区可用

    摘要:跨集群服务能够分布在不同的地理位置,使得混合和多云成为可能,相对于单一集群多可用区部署,更好地保证高可用。注例子中,我们利用谷歌容器引擎提供的集群,在该平台上,你可以把部署到想要的地区。 编者按:这篇文章是关于Kubernetes 1.3新功能的一系列深入文章的一部分。本文是第七篇。 用户使用Kubernetes 对生产环境上的部署进行弹性伸缩,同时我们听到一个明确的声音:希望跨区域、...

    wind3110991 评论0 收藏0
  • kubernetes部署Memcached可用记录

    摘要:部署实现高可用常用的两个工具连接多个,请求转发单单,互为主辅结构结构主要部署步骤使用启动个每个容器初始化脚本得到另一个节点的因为就两个节点,不是就是判断另一个节点的监听状态如果在监听说明是主节点需要复制它,否则反之复制节点 部署Memcached memcached实现高可用常用的两个工具:1)magent:连接多个memcached,请求转发2)repcached:单master单s...

    bawn 评论0 收藏0
  • 华尔街见闻基于istio的服务网格实践

    摘要:,托管于腾讯云容器平台容器编排工具。适配我们目前的服务部署在腾讯云托管,节点使用核的网络增强型机器,所有的后端服务都以部署,集群外部署高可用支持集群内服务发现,数据库以为主,消息队列采用。 距离2017年的见闻技术架构调整接近2年,随着业务线的发展,见闻技术部的项目数量、项目架构类型、基础设施规模、服务变更频率都在不断地增长,带给SRE的挑战是如何能更快地助力于开发人员更快更稳定地部署...

    stonezhu 评论0 收藏0
  • TOP100summit分享实录 | JFrog欣:Kubernetes is hard!JFro

    摘要:本文内容节选自由主办的第七届,架构师高欣分享的的实践实录。当然,在部署完成后,我们要做一个监测以便掌握它的运行状况。规划配置运行环境在正式部署前,还要考虑如何规划并配置好运行环境。在使用部署时,可以利用这些命令做验证,检验部署是否正常。 showImg(https://segmentfault.com/img/bVblRHj?w=2880&h=1920); 本文内容节选自由msup主办...

    邹强 评论0 收藏0
  • 用Docker和KubernetesMongoDB作为微服务来运行

    摘要:注意事项用容器和编排工具运行介绍了一些额外注意事项数据库是有状态的。尤其,在副本集合中,一个节点必须被用来执行和命令。名字叫做的被配置用来确保的单个实例是一直在运行的。 想要在你的手提电脑上尝试MongoDB吗?执行一个命令,然后拥有一个轻量级,独立的沙箱;再执行一个命令,删除你完成之后所有的痕迹。是不是需要一个在多个环境中都跟你的应用程序堆栈一样的应用程序?创建一你自己的容器镜像,然...

    Neilyo 评论0 收藏0

发表评论

0条评论

_DangJin

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<