摘要:部署復制集復制集結構只有節點能夠執行寫操作節點故障時節點可以切換為主要部署步驟使用啟動個節點啟動個每個節點執行以下初始化腳本,進行集群初始化設置副本集名稱生成副本集各節點信息生成文件
部署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 43s2.每個節點執行以下初始化腳本,進行集群初始化
#!/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" }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/19424.html
摘要:跨集群服務能夠分布在不同的地理位置,使得混合和多云成為可能,相對于單一集群多可用區部署,更好地保證高可用。注例子中,我們利用谷歌容器引擎提供的集群,在該平臺上,你可以把部署到想要的地區。 編者按:這篇文章是關于Kubernetes 1.3新功能的一系列深入文章的一部分。本文是第七篇。 用戶使用Kubernetes 對生產環境上的部署進行彈性伸縮,同時我們聽到一個明確的聲音:希望跨區域、...
摘要:部署實現高可用常用的兩個工具連接多個,請求轉發單單,互為主輔結構結構主要部署步驟使用啟動個每個容器初始化腳本得到另一個節點的因為就兩個節點,不是就是判斷另一個節點的監聽狀態如果在監聽說明是主節點需要復制它,否則反之復制節點 部署Memcached memcached實現高可用常用的兩個工具:1)magent:連接多個memcached,請求轉發2)repcached:單master單s...
摘要:,托管于騰訊云容器平臺容器編排工具。適配我們目前的服務部署在騰訊云托管,節點使用核的網絡增強型機器,所有的后端服務都以部署,集群外部署高可用支持集群內服務發現,數據庫以為主,消息隊列采用。 距離2017年的見聞技術架構調整接近2年,隨著業務線的發展,見聞技術部的項目數量、項目架構類型、基礎設施規模、服務變更頻率都在不斷地增長,帶給SRE的挑戰是如何能更快地助力于開發人員更快更穩定地部署...
摘要:本文內容節選自由主辦的第七屆,架構師高欣分享的的實踐實錄。當然,在部署完成后,我們要做一個監測以便掌握它的運行狀況。規劃配置運行環境在正式部署前,還要考慮如何規劃并配置好運行環境。在使用部署時,可以利用這些命令做驗證,檢驗部署是否正常。 showImg(https://segmentfault.com/img/bVblRHj?w=2880&h=1920); 本文內容節選自由msup主辦...
摘要:注意事項用容器和編排工具運行介紹了一些額外注意事項數據庫是有狀態的。尤其,在副本集合中,一個節點必須被用來執行和命令。名字叫做的被配置用來確保的單個實例是一直在運行的。 想要在你的手提電腦上嘗試MongoDB嗎?執行一個命令,然后擁有一個輕量級,獨立的沙箱;再執行一個命令,刪除你完成之后所有的痕跡。是不是需要一個在多個環境中都跟你的應用程序堆棧一樣的應用程序?創建一你自己的容器鏡像,然...
閱讀 2361·2023-04-25 19:27
閱讀 3491·2021-11-24 09:39
閱讀 3906·2021-10-08 10:17
閱讀 3397·2019-08-30 13:48
閱讀 1930·2019-08-29 12:26
閱讀 3121·2019-08-28 17:52
閱讀 3537·2019-08-26 14:01
閱讀 3534·2019-08-26 12:19