MySQL復(fù)制拓?fù)涔芾砉ぞ逴rchestrator

1. 簡(jiǎn)介

Orchestrator是一款開源的MySQL復(fù)制拓?fù)涔芾砉ぞ撸捎胓o語(yǔ)言編寫,支持MySQL主從復(fù)制拓?fù)潢P(guān)系的調(diào)整、支持MySQL主庫(kù)故障自動(dòng)切換、手動(dòng)主從切換等功能。

Orchestrator后臺(tái)依賴于MySQL或者SQLite存儲(chǔ)元數(shù)據(jù),能夠提供Web界面展示MySQL集群的拓?fù)潢P(guān)系及實(shí)例狀態(tài),通過(guò)Web界面可更改MySQL實(shí)例的部分配置信息,同時(shí)也提供命令行和api接口,以便更加靈活的自動(dòng)化運(yùn)維管理。

相比于MHA,Orchestrator更加偏重于復(fù)制拓?fù)潢P(guān)系的管理,能夠?qū)崿F(xiàn)MySQL任一復(fù)制拓?fù)潢P(guān)系的調(diào)整,并在此基礎(chǔ)上,實(shí)現(xiàn)MySQL高可用,另外Orchestrator自身可以部署多個(gè)節(jié)點(diǎn),通過(guò)raft分布式一致性協(xié)議,保證自身的高可用。

2. 源碼編譯

源碼地址:https://github.com/github/orchestrator.git
目前最新版:v3.0.11
編譯(需要聯(lián)網(wǎng)):

git clone https://github.com/github/orchestrator.git
cd orchestrator
script/build

編譯完成后生成可執(zhí)行文件: ./bin/orchestrator

3. 環(huán)境搭建

3.1 配置文件

在源碼的orchestrator/conf目錄中有3個(gè)配置文件模板,可參考使用。

  • orchestrator-sample.conf.json
  • orchestrator-sample-sqlite.conf.json
  • orchestrator-simple.conf.json

這里列出一個(gè)簡(jiǎn)化的參數(shù)配置orchestrator.conf.json

{
    "Debug": true,
    "ListenAddress": ":3000",                       #http開放端口

    "MySQLTopologyUser": "admin",                   #mysql管理賬號(hào),所有被管理的MySQL集群都需要有該賬號(hào)
    "MySQLTopologyPassword": "123456",              #mysql管理賬號(hào)密碼

    "MySQLOrchestratorHost": "127.0.0.1",            #后臺(tái)mysql數(shù)據(jù)庫(kù)地址,orchestrator依賴MySQL或者SQLite存儲(chǔ)管理數(shù)據(jù)
    "MySQLOrchestratorPort": 3306,                   #后臺(tái)mysql數(shù)據(jù)庫(kù)端口 
    "MySQLOrchestratorDatabase": "orchestrator",     #后臺(tái)mysql數(shù)據(jù)庫(kù)名
    "MySQLOrchestratorUser": "root",                 #后臺(tái)mysql數(shù)據(jù)庫(kù)賬號(hào)
    "MySQLOrchestratorPassword": "123456"            #后臺(tái)mysql數(shù)據(jù)庫(kù)密碼

    "BackendDB": "sqlite",
    "SQLite3DataFile": "/root/orchestrator/orchestrator.sqlite3",

    "RecoverMasterClusterFilters": ["*"],
    "RecoverIntermediateMasterClusterFilters": ["*"],
}

MySQLTopologyUser 這個(gè)配置項(xiàng)為被管理的MySQL集群的admin賬號(hào),該賬號(hào)需要有super,process,reload,select,replicatiopn slave,replicatiopn client 權(quán)限。

3.2 后臺(tái)數(shù)據(jù)庫(kù)

orchestrator后臺(tái)依賴MySQL或者SQLite存儲(chǔ)管理數(shù)據(jù),以MySQL為例,搭建Orchestrator環(huán)境,需要先搭建一個(gè)MySQL后臺(tái)數(shù)據(jù)庫(kù),MySQL具體搭建過(guò)程不再詳細(xì)介紹,搭建完,將MySQL賬號(hào)密碼等信息寫入配置文件,如下:
"MySQLOrchestratorHost": "127.0.0.1",
"MySQLOrchestratorPort": 3306,
"MySQLOrchestratorDatabase": "orchestrator",
"MySQLOrchestratorUser": "root",
"MySQLOrchestratorPassword": "123456",

如果覺得安裝MySQL太麻煩,只想快速體驗(yàn)一下Orchestrator,建議使用SQLite,只需在配置文件中寫入如下配置:
"BackendDB": "sqlite",
"SQLite3DataFile": "/root/orchestrator/orchestrator.sqlite3",

4. 執(zhí)行命令

orchestrator 通過(guò) -c 來(lái)執(zhí)行具體的命令,通過(guò) orchestrator help 查看所有命令的幫助文檔, orchestrator help relocate 查看具體命令relocate的幫助文檔。
orchestrator 提供的命令很多,這里提一些比較重要和常用的命令,沒有提到的可自行去文檔或者源碼中查看。
比如執(zhí)行一個(gè)命令:
./orchestrator --config=./orchestrator.conf.json -c discover -i mysql_host_name

4.1 MySQL實(shí)例管理命令

discover
forget
begin-maintenance
end-maintenance
in-maintenance
begin-downtime
end-downtime

discover
用于發(fā)現(xiàn)實(shí)例以及該實(shí)例的主、從庫(kù)信息,將獲取到的信息寫入后臺(tái)數(shù)據(jù)庫(kù)database_instance等相關(guān)表
orchestrator --config=./orchestrator.conf.json -c discover -i host_name

forget
移除實(shí)例信息,即從database_instance表中刪除相關(guān)記錄
orchestrator --config=./orchestrator.conf.json -c forget -i host_name

begin-maintenance
標(biāo)記一個(gè)實(shí)例進(jìn)入維護(hù)模式,在database_instance_maintenance表中插入記錄
orchestrator -c begin-maintenance -i instance.to.lock.com --duration=3h --reason="load testing; do not disturb"

end-maintenance
標(biāo)記一個(gè)實(shí)例退出維護(hù)模式,即更新 database_instance_maintenance 表中相關(guān)記錄
orchestrator -c end-maintenance -i locked.instance.com

in-maintenance
查詢實(shí)例是否處于維護(hù)模式,從表database_instance_maintenance中查詢
orchestrator -c in-maintenance -i locked.instance.com

begin-downtime
標(biāo)記一個(gè)實(shí)例進(jìn)入下線模式,在database_instance_downtime表中插入記錄
orchestrator -c begin-downtime -i instance.to.downtime.com --duration=3h --reason="dba handling; do not do recovery"

end-downtime
標(biāo)記一個(gè)實(shí)例退出下線模式,在database_instance_downtime表中刪除記錄
orchestrator -c end-downtime -i downtimed.instance.com

4.2 MySQL實(shí)例信息查詢命令

find
search
clusters
clusters-alias
all-clusters-masters
topology
topology-tabulated
all-instances
which-instance
which-cluster
which-cluster-domain
which-heuristic-domain-instance
which-cluster-master
which-cluster-instances
which-cluster-osc-replicas
which-cluster-gh-ost-replicas
which-master
which-downtimed-instances
which-replicas
which-lost-in-recovery
instance-status
get-cluster-heuristic-lag

find
通過(guò)正則表達(dá)式搜索實(shí)例名
orchestrator -c find -pattern "backup.*us-east"

search
通過(guò)關(guān)鍵字匹配搜索實(shí)例名
orchestrator -c search -pattern "search string"

clusters
輸出所有的MySQL集群名稱,通過(guò)sql查詢database_instance相關(guān)表獲取
orchestrator -c clusters

clusters-alias
輸出所有MySQL集群名稱以及別名
orchestrator -c clusters-alias

all-clusters-masters
輸出所有MySQL集群可寫的主庫(kù)信息
orchestrator -c all-clusters-masters

topology
輸出實(shí)例所屬集群的拓?fù)湫畔?br> orchestrator -c topology -i instance.belonging.to.a.topology.com

topology-tabulated
輸出實(shí)例所屬集群的拓?fù)湫畔?,類似topology命令,輸出格式稍有不同
orchestrator -c topology-tabulated -i instance.belonging.to.a.topology.com

all-instances
輸出所有已知的實(shí)例
orchestrator -c all-instances

which-instance
輸出實(shí)例的完整的信息
orchestrator -c which-instance -i instance.to.check.com

which-cluster
輸出MySQL實(shí)例所屬的集群名稱
orchestrator -c which-cluster -i instance.to.check.com

which-cluster-domain
輸出MySQL實(shí)例所屬集群的域名
orchestrator -c which-cluster-domain -i instance.to.check.com

which-heuristic-domain-instance
給定一個(gè)集群域名,輸出與其關(guān)聯(lián)的可寫的實(shí)例
orchestrator -c which-heuristic-domain-instance -alias some_alias

which-cluster-master
輸出實(shí)例所屬集群的主庫(kù)信息
orchestrator -c which-cluster-master -i instance.to.check.com

which-cluster-instances
輸出實(shí)例所屬集群的所有實(shí)例信息
orchestrator -c which-cluster-instances -i instance.to.check.com

which-master
列出實(shí)例所屬集群的主庫(kù)信息,與which-cluster-master類似
orchestrator -c which-master -i a.known.replica.com

which-downtimed-instances
列出處于下線狀態(tài)的實(shí)例
orchestrator -c which-downtimed-instances

which-replicas
輸出實(shí)例的從庫(kù)信息
orchestrator -c which-replicas -i a.known.instance.com

which-lost-in-recovery
輸出處于下線狀態(tài),在故障恢復(fù)過(guò)程中丟失的實(shí)例
orchestrator -c which-lost-in-recovery

instance-status
輸出實(shí)例的狀態(tài)信息
orchestrator -c instance-status -i instance.to.investigate.com

get-cluster-heuristic-lag
輸出實(shí)例所屬集群的最大延遲信息
orchestrator -c get-cluster-heuristic-lag -i instance.that.is.part.of.cluster.com

4.3 故障恢復(fù)命令

recover
recover-lite
force-master-failover
force-master-takeover
graceful-master-takeover
replication-analysis
ack-all-recoveries
ack-cluster-recoveries
ack-instance-recoveries
relocate

recover
主庫(kù)故障切換,主庫(kù)必須關(guān)閉,執(zhí)行才有效果, -i 參數(shù)必須是已經(jīng)關(guān)閉的主庫(kù), 新主庫(kù)不需要指定,由orchestrator自己選擇。
orchestrator -c recover -i dead.instance.com --debug

recover-lite
主庫(kù)故障切換,與recover類似,簡(jiǎn)化的部分操作,更加輕量化。
orchestrator -c recover-lite -i dead.instance.com --debug

force-master-failover
不管主庫(kù)是否正常,強(qiáng)制故障切換,切換后主庫(kù)不關(guān)閉,新主庫(kù)不需要指定,由orchestrator選擇。這個(gè)操作比較危險(xiǎn),謹(jǐn)慎使用。
orchestrator -c force-master-failover

force-master-takeover
不管主庫(kù)是否正常,強(qiáng)制主從切換,-i指定集群中任一實(shí)例,-d 指定新主庫(kù), 注意 切換后舊主庫(kù)不會(huì)指向新主庫(kù),需要手動(dòng)操作。
orchestrator -c force-master-takeover -i instance.in.relevant.cluster.com -d immediate.child.of.master.com

graceful-master-takeover
主從切換,舊主庫(kù)會(huì)指向新主庫(kù),但是復(fù)制線程是停止的,需要人工手動(dòng)執(zhí)行start slave,恢復(fù)復(fù)制。
orchestrator -c graceful-master-takeover -i instance.in.relevant.cluster.com -d immediate.child.of.master.com

replication-analysis
根據(jù)已有的拓?fù)潢P(guān)系分析潛在的故障事件,分析結(jié)果輸出格式不穩(wěn)定,未來(lái)可能改變,建議不要使用該功能。
orchestrator -c replication-analysis

ack-all-recoveries
ack-cluster-recoveries
ack-instance-recoveries
確認(rèn)已有的故障恢復(fù),防止未來(lái)再次發(fā)生故障時(shí),會(huì)阻塞故障切換
orchestrator -c ack-all-recoveries --reason="dba has taken taken necessary steps"
orchestrator -c ack-cluster-recoveries -i instance.in.a.cluster.com --reason="reson message"
orchestrator -c ack-instance-recoveries -i instance.that.failed.com --reason="reson message"

relocate
調(diào)整拓?fù)浣Y(jié)構(gòu),-i 指定的實(shí)例更改為 -d 指定實(shí)例的從庫(kù)。
orchestrator -c relocate -i replica.to.relocate.com -d instance.that.becomes.its.master

5. 自動(dòng)故障切換

Orchestrator能夠配置成自動(dòng)檢測(cè)主庫(kù)故障,并完成故障切換。

  1. 以http方式啟動(dòng)后臺(tái)Web服務(wù)
    orchestrator --config=./orchestrator.conf.json --debug http
    成功啟動(dòng)后,可通過(guò)瀏覽器訪問(wèn)Web頁(yè)面:
    http://192.168.56.110:3000
  2. 參數(shù)配置
"RecoverMasterClusterFilters": ["*"],
"RecoverIntermediateMasterClusterFilters": ["*"],
"FailureDetectionPeriodBlockMinutes": 60,
"RecoveryPeriodBlockSeconds": 3600

RecoverMasterClusterFilters 和 RecoverIntermediateMasterClusterFilters 必須配置為["*"],否則自動(dòng)切換不會(huì)觸發(fā)。
FailureDetectionPeriodBlockMinutes 和 RecoveryPeriodBlockSeconds 參數(shù)默認(rèn)值為1個(gè)小時(shí),也就是如果發(fā)生了故障切換,在1個(gè)小時(shí)之內(nèi),該主庫(kù)再次出現(xiàn)故障,將不會(huì)被監(jiān)測(cè)到,也不會(huì)觸發(fā)故障切換。

6. Orchestrator 高可用

Orchestrator多節(jié)點(diǎn)部署,通過(guò)raft一致性協(xié)議實(shí)現(xiàn)自身高可用。
例如在如下3臺(tái)機(jī)器部署Orchestrator節(jié)點(diǎn):

  • 192.168.56.110
  • 192.168.56.111
  • 192.168.56.112

在每個(gè)節(jié)點(diǎn)上修改orchestrator.conf.json配置文件:

"RaftEnabled": true, 
"RaftDataDir": "/var/lib/orchestrator", 
"RaftBind": "192.168.56.110", 
"DefaultRaftPort": 10008, 
"RaftNodes": [ "192.168.56.110", "192.168.56.111", "192.168.56.112" ],

RaftBind配置為當(dāng)前節(jié)點(diǎn)ip,在每個(gè)節(jié)點(diǎn)上啟動(dòng)orchestrator服務(wù):
./orchestrator --config=./orchestrator.conf.json --debug http

在瀏覽器中訪問(wèn):
http://192.168.56.110:3000/api/leader-check
返回 "OK",當(dāng)前l(fā)eader為192.168.56.110
http://192.168.56.110:3000/api/raft-health
返回 "healthy"

http://192.168.56.111:3000/api/leader-check
返回 "Not leader"
http://192.168.56.111:3000/api/raft-health
返回 "healthy"

關(guān)閉192.168.56.110節(jié)點(diǎn)上的orchestrator服務(wù),leader自動(dòng)切換到192.168.56.111或者192.168.56.112,如果192.168.56.110重新啟動(dòng)后,加入集群,它將作為follower。

7. 注意事項(xiàng)

  1. Orchestrator官方文檔部分內(nèi)容不準(zhǔn)確,比如 MySQLTopologyUser 賬號(hào)的權(quán)限應(yīng)該設(shè)置為super,process,reload,select,replicatiopn slave,replicatiopn client,文檔中缺少了select權(quán)限,orchestrator切換過(guò)程中需要通過(guò)讀取從庫(kù)的mysql.slave_master_info表,獲取復(fù)制賬號(hào)和密碼,如果沒有select權(quán)限,將導(dǎo)致讀取失敗,并且不會(huì)有任何錯(cuò)誤信息報(bào)出來(lái)。
  2. orchestrator 建議使用機(jī)器名,而不是ip來(lái)管理MySQL實(shí)例,比如change master to 中的 master_host 如果指定的是ip,有可能導(dǎo)致主從切換或者故障切換出現(xiàn)問(wèn)題。

最后附上Web頁(yè)面圖:


Orchestrator
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容