Corosync簡介
Corosync是高可用集羣中基礎事務層 (Messaging Layer)的一個實現方案與heartbeat的功能類似,主要用來傳遞集羣的事務信息,但是Corosync的功能更加強大,正在逐漸地取代heartbeat。Corosync在傳遞信息的時候可以通過一個簡單的配置文件來定義信息傳遞的方式和協議等。
Pacemaker簡介
Pacemaker是一個集羣資源管理器,從heartbeat v3版本中分裂出來,功能強大。它利用集羣事務層提供的組件對各節點進行資源管理及監控並從節點或資源級別的故障中恢復,以實現羣集服務的最大可用性。
DRBD
使用DRBD實現對MariaDB數據存放目錄的備份和高可用。關於DRBD的具體應用,可參考前面的博客。
實現過程
實驗環境:
192.168.1.102 時間服務器
192.168.1.126 node1
192.168.1.127 node2
node1和node2爲集羣的兩個節點,在這兩個節點上部署Corosync、Pacemaker及MariaDB,利用DRBD在這兩個節點上創建兩個對等的存儲設備,用來存放MariaDB數據庫的數據文件以實現故障時的切換。
配置集羣的必要前提:
1)各節點時間同步
2)節點之間能夠基於主機名相互訪問
爲了方便也可以在各節點之間實現SSH基於祕鑰的通信。
利用ansible批量部署Corosync和Pacemaker
以下是role的目錄結構:
[root@www roles]# tree . ├── common │ ├── files │ │ ├── hosts │ │ └── ntp.conf │ └── tasks │ └── main.yml ├── crmsh │ ├── files │ │ └── crmsh-2.1-1.6.x86_64.rpm │ └── tasks │ └── main.yml └── ha ├── files │ ├── authkey │ └── corosync.conf ├── handlers │ └── main.yml └── tasks └── main.yml
common角色
[root@www roles]# vim common/tasks/main.yml - name: hosts file copy: src=hosts dest=/etc/hosts - name: sync time copy: src=ntp.conf dest=/etc/ntp.conf - name: start ntpd service: name=ntpd state=started enabled=no ############################################# [root@www roles]# vim common/files/ntp.conf ................ server 192.168.1.102 ................ ############################################# [root@www roles]# vim common/files/hosts ................ 192.168.1.126 node1.xiaoxiao.com node1 192.168.1.127 node2.xiaoxiao.com node2
common角色的作用就是將已經配置好的ntp服務配置文件及hosts文件發送至各節點,並啓動ntp服務。實現集羣的兩個必要前提。
crmsh角色
crmsh角色用於將crmsh-2.1-1.6.x86_64.rpm安裝包複製到各節點,並使用yum安裝(yum install crmsh-2.1-1.6.x86_64.rpm)自動解決依賴關係。這個包由http://download.opensuse.org/repositories/network:/ha-clustering:源提供,並且這個包依賴pssh,需要添加epel源。
crmsh是一個用戶層的工具,用於配置集羣資源。crmsh由SUSE提供。對應的工具還有pcs,這個由紅帽提供,也是專用於corosync+pacemaker的資源配置工具。
ha角色
ha角色用於安裝Corosync和Pacemaker,併爲之添加配置文件,祕鑰文件及啓動服務。Corosync和Pacemaker安裝包base源有提供可以直接安裝。
Corosync的配置文件/etc/corosync/corosync.conf
compatibility: whitetank totem { version: 2 #版本 secauth: on #是否基於安全認證的方式驗證每一個節點 threads: 0 #啓動的線程數(取決於cpu的核心),0表示不啓用線程機制 interface { ringnumber: 0 bindnetaddr: 192.168.1.0 #綁定在哪個網絡地址上 mcastaddr: 230.230.3.3 #多播地址 mcastport: 5405 #用於傳遞多播信息的端口 ttl: 1 } } logging { fileline: off to_stderr: no to_logfile: yes to_stderr: no to_logfile: yes #由程序自己記錄日誌 logfile: /var/log/cluster/corosync.log #日誌路徑 to_syslog: no #日誌是否由rsyslog記錄 debug: off timestamp: on #每一條日誌打上時間戳 logger_subsys { subsys: AMF debug: off } } amk { mode: disabled } service { #以插件化的方式調用pacemaker ver: 1 #版本號 name: pacemaker } aisexec { #以哪個用戶和組的身份來運行 user: root group: root }
以上是官方文檔對service中ver配置爲1時的解釋,這時,pacemaker服務需要單獨啓動。先啓動Corosync再啓動Pacemaker。
Corosync在各節點之間傳遞信息時要依賴祕鑰文件,通過corosync-keygen命令生成這個祕鑰文件。完成之後它會自動存放在/etc/corosync/目錄下,並且權限爲0400。
[root@node1 ~]# corosync-keygen Corosync Cluster Engine Authentication key generator. Gathering 1024 bits for key from /dev/random. Press keys on your keyboard to generate entropy. Press keys on your keyboard to generate entropy (bits = 768).
在生成祕鑰文件過程中需要不斷敲鍵盤才能完成執行過程,簡單起見,可以使用/dev/urandom代替/dev/random。
[root@node1 ~]# mv /dev/random /dev/random.bak [root@node1 ~]# mv /dev/urandom /dev/random
用完之後不要忘了改回來!!!
這裏使用ansible部署,將已經配置好的主配置文件corosync.conf和祕鑰文件authkey放在ha角色的files目錄下。playbook如下:
[root@www roles]# vim ha/tasks/main.yml - name: install corosync and pacemaker yum: name={{ item }} state=present with_items: - corosync - pacemaker tags: inst - name: copy keyFile copy: src=authkey dest=/etc/corosync/authkey owner=root group=root mode=0400 tags: authkey - name: copy configute file copy: src=corosync.conf dest=/etc/corosync/corosync.conf tags: conf - name: start corosync service: name=corosync state=started enabled=no tags: startCorosync - name: start pacemaker service: name=pacemaker state=started enabled=no tags: startPacemaker
若要重啓服務,需要先stop pacemaker,再stop corosync,然後再進行重啓。配置ha.yml,並執行。
[root@www corosync]# vim ha.yml - name: install corosync and crmsh remote_user: root hosts: web roles: - common - ha - crmsh ######################################### [root@www corosync]# ansible-playbook ha.yml ......
使用drbd創建設備
配置global_common.conf文件及drbd資源文件(在各節點上),資源文件如下(drbd的具體應用在前面的博客):
[root@node1 ~]# vim /etc/drbd.d/dbdata.res resource dbdata { on node1.xiaoxiao.com { device /dev/drbd0; disk /dev/sdb1; address 192.168.1.126:7789; meta-disk internal; } on node2.xiaoxiao.com { device /dev/drbd0; disk /dev/sdb1; address 192.168.1.127:7789; meta-disk internal; } }
在各節點上創建設備,啓動服務並完成掛載。
############在控制節點上################## [root@www ~]# ansible web -m shell -a 'drbdadm create-md dbdata' [root@www ~]# ansible web -m shell -a 'service drbd start' ############node1上##################### [root@node1 ~]# drbdadm primary --force resource [root@node1 ~]# mkfs -t ext4 -b 1024 /dev/drbd0 [root@node1 ~]# mkdir /data [root@node1 ~]# mount /dev/drbd0 /data [root@node1 ~]# mkdir /data/mydata #作爲MariaDB的數據存放目錄
安裝MariaDB
這裏使用二進制安裝方式在各節點上安裝MariaDB。在node2上也執行如下操作。
[root@node1 ~]# tar xf mariadb-10.0.10-linux-x86_64.tar.gz -C /usr/local/ [root@node1 ~]# cd /usr/local/ [root@node1 local]# ln -sv mariadb-10.0.10-linux-x86_64/ mysql
添加mysql用戶mysql組,兩個節點上的uid和gid都得一致,並修改目錄權限
[root@www ~]# ansible web -m shell -a 'groupadd -g 300 mysql' [root@www ~]# ansible web -m shell -a 'useradd -u 300 -g 300 mysql' [root@www ~]# ansible web -m shell -a 'chown -R root:mysql /usr/local/mysql/*'
在node1上完成數據庫的初始化(數據存放在drbd設備上,在一個節點上完成即可),在配置文件my.cnf中datadir要指向drbd設備掛載的目錄/data下的mydata:
[root@www ~]# chown -R mysql:mysql /data/mydata [root@node1 mysql]# mkdir /etc/mysql [root@node1 mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf [root@node1 mysql]# vim /etc/mysql/my.cnf datadir=/data/mydata #指定數據存放目錄 bind-address=192.168.1.200 #mysql服務僅監聽在指定地址上 ........... ############執行初始化腳本##################### [root@node1 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mydata/
同步配置文件,並添加啓動腳本:
[root@node1 mysql]# scp -p /etc/mysql/my.cnf node2:/etc/mysql/ [root@www ~]# ansible web -m shell -a 'cp -a /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld' [root@www ~]# ansible web -m shell -a 'chkconfig --add mysqld' ############確保各服務開機不自動啓動############## [root@www ~]# ansible web -m shell -a 'chkconfig mysqld off' [root@www ~]# ansible web -m shell -a 'chkconfig drbd off'
安裝完成之後啓動服務測試一下,看是否能夠正常運行。必要的話,drbd主從切換,在另一個節點上也進行測試。
配置集羣資源實現高可用
首先根據具體情況設置這兩個參數的值stonith-enabled,no-quorum-policy。
[root@node1 ~]# crm crm(live)# configure crm(live)configure# property stonith-enabled=false crm(live)configure# property no-quorum-policy=ignore
添加ip,mysqld,文件系統資源
crm(live)configure# primitive myip ocf:heartbeat:IPaddr params ip=192.168.1.200 op monitor timeout=20s interval=10s crm(live)configure# primitive myserver lsb:mysqld op monitor timeout=15 interval=15 crm(live)configure# primitive dataFS ocf:heartbeat:Filesystem params device=/dev/drbd0 directory=/data fstype="ext4" op monitor timeout=40 interval=20 op start timeout=60 op stop timeout=60
添加drbd資源,並添加其克隆資源
crm(live)configure# primitive mydatadrbd ocf:linbit:drbd params drbd_resource=dbdata op monitor role=Master timeout=20 interval=10 op monitor role=Slave timeout=20 interval=20 op start timeout=240 op stop timeout=100 crm(live)configure# master ms_mydata mydatadrbd meta master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true
其中第二條指令爲做主從克隆,ms_mydata爲克隆的資源名稱,mydatadrbd爲被克隆的資源對象,meta指明元數據。元數據的幾個參數說明如下:
clone-max #把資源克隆成幾份
clone-node-max #一個節點上最多能運行幾份克隆
notify #當在某一節點上啓動或停止一個克隆資源時,是否通知其他節點(默認通知true)
globally-unique #每一份克隆在全局是否唯一(默認唯一true)
ordered #每一個克隆是否按順序啓動,還是在各自的節點上並行啓動。默認true(按次序啓動)
interleave #當修改了順序行爲的約束之後,那麼實例是否能夠啓動停止(默認爲true,大多數情況下這
#個參數不作配置)
master-max #所有的克隆當中有幾份可以是主的(默認爲1)
master-node-max #一個節點上最多能運行幾份主資源(默認爲1)
定義資源約束及資源啓動的先後順序
crm(live)configure# colocation myip_with_myserver inf: myip myserver crm(live)configure# colocation myserver_with_dataFS inf: myserver dataFS crm(live)configure# colocation dataFS_with_ms_mydata inf: dataFS ms_mydata:Master crm(live)configure# order ms_mydata_before_dataFS inf: ms_mydata:promote dataFS:start crm(live)configure# order dataFS_before_myserver inf: dataFS:start myserver:start crm(live)configure# order myip_before_myserver inf: myip myserver:start
定義完成之後進行驗證,然後提交。
crm(live)configure# verify crm(live)configure# commit
所有資源定義如下:
crm(live)# configure show node node1.xiaoxiao.com \ attributes standby=off node node2.xiaoxiao.com \ attributes standby=off primitive dataFS Filesystem \ params device="/dev/drbd0" directory="/data" fstype=ext4 \ op monitor timeout=40 interval=20 \ op start timeout=60 interval=0 \ op stop timeout=60 interval=0 primitive mydatadrbd ocf:linbit:drbd \ params drbd_resource=dbdata \ op monitor role=Master timeout=20 interval=10 \ op monitor role=Slave timeout=20 interval=20 \ op start timeout=240 interval=0 \ op stop timeout=100 interval=0 primitive myip IPaddr \ params ip=192.168.1.200 \ op monitor timeout=20s interval=10s primitive myserver lsb:mysqld \ op monitor timeout=15 interval=15 ms ms_mydata mydatadrbd \ meta master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true colocation dataFS_with_ms_mydata inf: dataFS ms_mydata:Master colocation myip_with_myserver inf: myip myserver colocation myserver_with_dataFS inf: myserver dataFS order dataFS_before_myserver inf: dataFS:start myserver:start order ms_mydata_before_dataFS inf: ms_mydata:promote dataFS:start order myip_before_myserver inf: myip myserver:start property cib-bootstrap-options: \ dc-version=1.1.11-97629de \ cluster-infrastructure="classic openais (with plugin)" \ expected-quorum-votes=2 \ stonith-enabled=false \ no-quorum-policy=ignore \ last-lrm-refresh=1439387136
查看集羣的運行情況,顯示所有資源運行在node2上。
node2上對應地址上的對應端口已處於監聽狀態
模擬node2服務器故障:
############node2上##################### [root@node2 ~]# crm node standby
資源已全部切換至node1上
在其他主機上嘗試連接數據庫(首先在數據庫中創建對用的用戶):
完成連接.................^_^