高可用mariadb拓撲圖
一、設計前提
1、時間同步 # ntpdate 172.16.0.1 或者 # chronyc sources
2、所有的主機對應的IP地址解析可以正常工作, 主機名要與命令#uname -n 所得的結果一致
因此,/etc/hosts中的內容為以下內容
172.16.23.10 node1.rj.com node1 172.16.23.11 node2.rj.com node2 172.16.23.12 node3.rj.com node3
二、環境的建立及安裝orosync ,pacemaker ,crmsh
三臺機器都安裝好ansible (對于ansible集群管理工具而言需要雙機互信,其中node1當做堡壘機)
# ssh-keygen # for i in {10..12}; do ssh-copy-id -i 172.16.23.$i ; done ; 此條命令將公鑰發送給三臺機器,其中包括自己也就是堡壘機 # vim /etc/ansible/hosts [mariadb] 172.16.23.10 172.16.23.11 172.16.23.12 # ansible mariadb -m ping 測試三臺主機與堡壘機之間的連通性 # vim /etc/hosts 主機名解析配置 172.16.23.10 node1.rj.com node1 172.16.23.11 node2.rj.com node2 172.16.23.12 node3.rj.com node3 # ansible mariadb -m command -a "ntpdate 172.16.0.1" 同步三臺主機的時間 # ansible mariadb -m yum -a "name=pacemaker,mariadb-server state=present" 在三臺主機上安裝 mariadb ,corosync 和 pacemaker
注:yum 安裝pacemaker 的時候,其corosync也會自動安裝上
# vim /etc/corosync/corosync.conf 加入以下信息 totem { version: 2 crypto_cipher: aes256 crypto_hash: sha1 interface { ringnumber: 0 bindnetaddr: 172.16.0.0 mcastaddr: 239.255.23.1 mcastport: 5405 ttl: 1 } logging { fileline: off to_stderr: no to_logfile: yes logfile: /var/log/cluster/corosync.log to_syslog: no debug: off timestamp: on logger_subsys { subsys: QUORUM debug: off } quorum { provider: corosync_votequorum node { ring0_addr: node1.rj.com nodeid:1 } node { ring0_addr: node2.rj.com nodeid:2 } node { ring0_addr: node3.rj.com nodeid:3 } # corosync-keygen # ansible mariadb -m copy -a "src=/etc/corosync/authkey dest=/etc/corosync/" # ansible mariadb -m copy -a "src=/etc/corosync/corosync.conf dest=/etc/corosync/" # ansible mariadb -m service -a "name=corosync state=persent" # ansible mariadb -m service -a "name=pacemaker state=persent" # tcpdump -i eno16777736 -nn port 5405
使用tcpdump抓包工具可以來查看三臺主機之間傳遞的心跳信息
注:mariadb在集群資源的配置中必需是開機自啟動的
這樣corosync才能實別其Unitfile 文件,而不能在配置前啟動所以服務一定是關閉的
# ansible mariadb -m service -a "name=mariadb enabled=on" # ansible mariadb -m service -a "name=corosync enabled=on" # ansible mariadb -m service -a "name=pacemaker enabled=on" # mkdir rpm && cd rpm # wget 172.18.0.1/pub/Sources/7.x86_64/crmsh/crmsh-2.1.4-1.1.x86_64.rpm 下載crmsh及其所依賴的rpm包 # wget 172.18.0.1/pub/Sources/7.x86_64/crmsh/pssh*.rpm # wget 172.18.0.1/pub/Sources/7.x86_64/crmsh/python-passh*.rpm # ansible mariadb -m copy -a "src=/root/rpm dest=/root/" # for i in {1..3}; do ssh node$i yum -y install /root/rpm/*; done
查看corosync引擎是否已經正常啟動
# ansible mariadb -e command -a "grep -e 'Corosync Cluster Engine' -e 'configuration file' /var/log/messages" Feb 13 11:27:35 node1 systemd: Stopped Corosync Cluster Engine. Feb 13 14:08:03 node1 systemd: Starting Corosync Cluster Engine... Feb 13 14:08:04 node1 corosync: Starting Corosync Cluster Engine (corosync): [ OK ] Feb 13 14:08:04 node1 systemd: Started Corosync Cluster Engine. Feb 13 14:28:16 node1 systemd: Mounted NFSD configuration filesystem. Feb 13 14:28:44 node1 smartd[787]: Opened configuration file /etc/smartmontools/smartd.conf Feb 13 14:32:12 node1 systemd: Starting Corosync Cluster Engine... Feb 13 14:32:13 node1 corosync: Starting Corosync Cluster Engine (corosync): [ OK ] Feb 13 14:32:13 node1 systemd: Started Corosync Cluster Engine. Feb 13 14:43:03 node1 systemd: Started Corosync Cluster Engine.
查看其成員之間的結點通知信息是否正常
# ansible mariadb -e command -a "grep TOTEM /var/log/messages"
查看啟動過程中是否有錯誤信息產生
# ansible mariadb -e command -a "grep 'ERROR' /var/log/messages"
查看pacemaker是否已經正常啟動
# ansible mariadb -e command -a "grep 'pacemaker' /var/log/messages "
使用以下命令查看結點的狀態
# crm status Last updated: Mon Feb 13 15:43:08 2017 Last change: Mon Feb 13 14:33:58 2017 by hacluster via crmd on node3.rj.com Stack: corosync Current DC: node3.rj.com (version 1.1.13-10.el7-44eb2dd) - partition with quorum 3 nodes and 0 resources configured Online: [ node1.rj.com node2.rj.com node3.rj.com ]
查看pacemaker 和與corosync所啟動的進程
# ansible mariadb -m command -a "ps auxf " | grep pacemaker root 1720 0.0 1.3 130484 6384 ? Ss 14:33 0:00 /usr/sbin/pacemakerd -f haclust+ 1729 0.0 2.7 132816 13268 ? Ss 14:33 0:01 \_ /usr/libexec/pacemaker/cib root 1730 0.0 1.4 133968 6956 ? Ss 14:33 0:00 \_ /usr/libexec/pacemaker/stonithd root 1731 0.0 0.8 102936 4108 ? Ss 14:33 0:00 \_ /usr/libexec/pacemaker/lrmd haclust+ 1732 0.0 1.3 124780 6736 ? Ss 14:33 0:00 \_ /usr/libexec/pacemaker/attrd haclust+ 1733 0.0 0.7 114896 3668 ? Ss 14:33 0:00 \_ /usr/libexec/pacemaker/pengine haclust+ 1734 0.0 1.5 143160 7484 ? Ss 14:33 0:00 \_ /usr/libexec/pacemaker/crmd # ansible marriadb -m command -a "ps auxf" | grep corosync root 1483 0.6 7.9 134848 38436 ? Ssl 14:32 0:28 corosync
三、利用crmsh來配置corosync的IP地址資源及mariadb資源
如果想要查看某種類別下的所用資源代理的列表,可以使用類似以下的命令來實現
#crm ra list lsb #crm ra list ocf heartbeat #crm ra list ocf pacemaker #crm ra list ocf stonith
配置vip
# crm configure property stonith-enabled=false 關閉stonsth設備 # crm configure primitive DBIP ocf:heartbeat:IPaddr params ip=172.16.23.23 添加vip資源代理 # crm configure verify 查看是否有錯誤 # crm configure commit 用來提交配置信息 # crm configure show 可以用來查看配置信息 node 1: node1.rj.com node 2: node2.rj.com node 3: node3.rj.com primitive DBIP IPaddr \ params ip=172.16.23.23 property cib-bootstrap-options: \ have-watchdog=false \ dc-version=1.1.13-10.el7-44eb2dd \ cluster-infrastructure=corosync \ stonith-enabled=false # crm node standby node1.rj.com 當將node1結點成為備用結點時 # ansible mariadb -m command -a "ip addr list" 查看其vip的變化信息 # crm ra info systemd:mairadb 用來查看systemd類型的mariadb資源的語法格式 systemd unit file for mariadb (systemd:mariadb) MariaDB database server Operations' defaults (advisory minimum): start timeout=15 stop timeout=15 status timeout=15 restart timeout=15 monitor timeout=15 interval=15 start-delay=15
vip資源
當node1 # crm node standby node1.rj.com 時
定義mariadbg資源并設定監控
# crm configure primitive MDB systemd:mariadb op start timeout=15s op stop timeout=15s op monitor interval=15s timeout=15s # crm configure group DBservice DBIP MDB # crm configure verify # crm configure commit # crm configure show node 1: node1.rj.com \ attributes standby=on node 2: node2.rj.com \ attributes standby=off node 3: node3.rj.com \ attributes standby=off primitive DBIP IPaddr \ params ip=172.16.23.23 primitive MDB systemd:mariadb \ op start timeout=15s interval=0 \ op stop timeout=15s interval=0 \ op monitor interval=15s timeout=15s group DBservice DBIP MDB property cib-bootstrap-options: \ have-watchdog=false \ dc-version=1.1.13-10.el7-44eb2dd \ cluster-infrastructure=corosync \ stonith-enabled=false \ no-quorum-policy=ignore
為mariadb服務加入數據庫資源,并配置遠程用戶遠程接入進行測試
注:此時有一個資源已經啟動,但其它的兩個mariadb服務啟動之后才能進行對數據庫的更改
但更改完后旋得再將那兩個結點的數據庫停止調
node1 node2 node3上執行: # mysql -e "GRANT ALL ON *.* TO 'root'@'%.%.%.%' IDENTIFIED BY 'centos.123';" node1 # mysql -e "CREATE DATABASE NODE1" node2 # mysql -e "CREATE DATABASE NODE2" node3 # mysql -e "CREATE DATABASE NODE3"
啟用一臺測試機進行測試
node1 # systemctl stop corosync.service pacemaker.service 當node1的服務停止時
node2 # systemctl stop corosync.service pacemaker.service 當node2的的服務停止時
原創文章,作者:renjin,如若轉載,請注明出處:http://www.www58058.com/68313