目錄:php
介紹環境node
藉助pcs安裝與配置corosync和pacemaker(pcs只是一個管理工具)python
DRBD安裝配置參考以前的博客《DRBD-MYSQL分佈式塊設備實現高可用》http://legehappy.blog.51cto.com/13251607/1975804mysql
Mysql安裝和配置nginx
Crmsh安裝和資源管理git
聽說這種Corosync+pacemaker+DRBD+mysql(mariadb)的架構能夠達到99.99%,就是說這種架構很是的穩定,因此以爲仍是須要了解一下的。github
先給你們介紹一下高可用的衡量標準:高可用性集羣能夠經過系統可靠性(reliability)和可維護性(maintainablilty)來度量的,工程上,一般使用平均故障時間(MTTF)來度量系統的可靠性,用平均維修時間(MTTR)來度量系統的可維護性。因而可靠性被定義爲:HA=MTTF/(MTTF+MTTR)*100% 具體HA衡量標準:web
99%---一年宕機時間不超過4天sql
99,.9%---一年宕機時間不超過10小時數據庫
99.99%---一年宕機時間不超過1小時
99.999%---一年宕機時間不超過6分鐘
環境:
[root@cml1 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@cml2 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
cml1:serve ip:192.168.5.101r vip:192.168.5.250
cml2:server ip:192.168.5.102 vip:192.168.5.250
cml3:client ip:192.168.5.104
配置集羣的前提:
(1)時間同步
[root@cml1~]# ntpdate cn.pool.ntp.org
[root@cml2~]# ntpdate cn.pool.ntp.org
(2)主機名互相訪問
[root@cml1~]# ssh-keygen
[root@cml1~]# ssh-copy-id cml2
[root@cml1~]# hostname
cml1
[root@cml1~]# cat /etc/hosts
192.168.5.101 cml1
192.168.5.102 cml2
192.168.5.104 cml3
(3)是否使用仲裁設備。
Centos7上面不須要使用
生命週期管理工具:
Pcs:agent(pcsd)
Crash:pssh
一、在兩節點上執行:
[root@cml1 ~]# yum install -y pacemaker pcspsmisc policycoreutils-python
二、兩節點上啓動pcs而且開機啓動:
[root@cml1 ~]# systemctl start pcsd.service [root@cml1 ~]# systemctl enable pcsd.service
三、兩節點上修改用戶hacluster的密碼(用戶已經固定不能夠改變)
[root@cml1 ~]# echo redhat | passwd --stdin hacluster
四、註冊pcs集羣主機(默認註冊使用用戶名hacluster,和密碼):
[root@cml1 corosync]# pcs cluster auth cml1 cml2 ##設置註冊那個集羣節點 cml1: Already authorized cml2: Already authorized
五、在集羣上註冊兩臺集羣:
[root@cml1 corosync]# pcs cluster setup--name mycluster cml1 cml2 --force。 ##設置集羣
六、接下來就在某個節點上已經生成來corosync配置文件:
[root@cml1 corosync]# ls
corosync.conf corosync.conf.example corosync.conf.example.udpu corosync.xml.example uidgid.d
#咱們看到生成來corosync.conf配置文件:
七、咱們看一下注冊進來的文件:
[root@cml1 corosync]# cat corosync.conf totem { version: 2 secauth: off cluster_name: webcluster transport: udpu } nodelist { node { ring0_addr: cml1 nodeid: 1 } node { ring0_addr: cml2 nodeid: 2 } } quorum { provider: corosync_votequorum two_node: 1 } logging { to_logfile: yes logfile: /var/log/cluster/corosync.log to_syslog: yes }
八、啓動集羣:
[root@cml1 corosync]# pcs cluster start--all cml1: Starting Cluster... cml2: Starting Cluster... ##至關於啓動來pacemaker和corosync: [root@cml1 corosync]# ps -ef | grepcorosync root 57490 1 1 21:47 ? 00:00:52 corosync root 75893 51813 0 23:12 pts/0 00:00:00 grep --color=auto corosync [root@cml1 corosync]# ps -ef | greppacemaker root 57502 1 0 21:47 ? 00:00:00 /usr/sbin/pacemakerd -f haclust+ 57503 57502 0 21:47 ? 00:00:03 /usr/libexec/pacemaker/cib root 57504 57502 0 21:47 ? 00:00:00/usr/libexec/pacemaker/stonithd root 57505 57502 0 21:47 ? 00:00:01 /usr/libexec/pacemaker/lrmd haclust+ 57506 57502 0 21:47 ? 00:00:01 /usr/libexec/pacemaker/attrd haclust+ 57507 57502 0 21:47 ? 00:00:00 /usr/libexec/pacemaker/pengine haclust+ 57508 57502 0 21:47 ? 00:00:01 /usr/libexec/pacemaker/crmd root 75938 51813 0 23:12 pts/0 00:00:00 grep --color=auto pacemaker
八、查看集羣的狀態(顯示爲no faults就是ok)
[root@cml1 corosync]# corosync-cfgtool -s Printing ring status. Local node ID 1 RING ID 0 id = 192.168.5.101 status = ring 0 active with no faults [root@cml1 corosync]# ssh cml2corosync-cfgtool -s Printing ring status. Local node ID 2 RING ID 0 id = 192.168.5.102 status = ring 0 active with no faults
十、能夠查看集羣是否有錯:
[root@cml1 corosync]# crm_verify -L -V error: unpack_resources: Resourcestart-up disabled since no STONITH resources have been defined error: unpack_resources: Either configure some or disable STONITH with the stonith-enabled option error: unpack_resources: NOTE:Clusters with shared data need STONITH to ensure data integrity Errors found during check: config not valid
##由於咱們沒有配置STONITH設備,因此咱們下面要關閉
十一、關閉STONITH設備:
[root@cml1 corosync]# pcs property setstonith-enabled=false [root@cml1 corosync]# crm_verify -L -V [root@cml1 corosync]# pcs property list Cluster Properties: cluster-infrastructure: corosync cluster-name: mycluster dc-version: 1.1.16-12.el7_4.2-94ff4df have-watchdog: false stonith-enabled: false
參考以前的博客《DRBD-MYSQL分佈式塊設備實現高可用》http://legehappy.blog.51cto.com/13251607/1975804
##這裏我是使用yum直接安裝的,若是使用編譯安裝也是同樣的可是就要在
[
root@cml1 ~]# cd /etc/systemd/system [root@cml1 system]# cat mariadb.service [Unit] Description=mariadb After=network.target [Service] Type=forking ExecStart=/usr/local/mysql/sbin/mysql ExecReload=/usr/local/mysql/sbin/mysql -sreload ExecStop=/usr/local/mysql/sbin/mysql -squit PrivateTmp=true [Install] WantedBy=multi-user.target
一、兩節點上面安裝:
[root@cml1 ~]# yum install mariadb-servermariadb -y
二、在/etc/my.cnf文件中修改mysql數據庫存放目錄爲/data
[root@cml1 ~]# cat /etc/my.cnf [mysqld] datadir=/data socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommendedto prevent assorted security risks symbolic-links=0 # Settings user and group are ignored whensystemd is used. # If you need to run mysqld under adifferent user or group, # customize your systemd unit file formariadb according to the # instructions inhttp://fedoraproject.org/wiki/Systemd [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid # # include all files from the configdirectory # !includedir /etc/my.cnf.d
##先不要啓動由於後面直接使用crm配置啓動。
一、安裝crmsh:
集羣咱們能夠下載安裝crmsh來操做(從github來下載,而後解壓直接安裝):只在一個節點安裝便可。(但最好選擇兩節點上安裝這樣測試時方便點)
[root@cml1 ~]# cd /usr/local/src/ [root@cml1 src]# ls nginx-1.12.0 php-5.5.38.tar.gz crmsh-2.3.2.tar nginx-1.12.0.tar.gz zabbix-3.2.7.tar.gz [root@cml1 src]# tar -xf crmsh-2.3.2.tar [root@cml1 crmsh-2.3.2]# python setup.pyinstall
二、用crmsh來管理:
[root@cml1 ~]# crm help
Help overview for crmsh
Available topics:
Overview Help overview forcrmsh
Topics Available topics
Description Program description
CommandLine Command lineoptions
Introduction Introduction
Interface User interface
Completion Tab completion
Shorthand Shorthand syntax
Features Features
Shadows Shadow CIB usage
Checks Configurationsemantic checks
Templates Configurationtemplates
Testing Resource testing
Security Access ControlLists (ACL)
Resourcesets Syntax: Resourcesets
AttributeListReferences Syntax: Attribute list references
AttributeReferences Syntax: Attribute references
RuleExpressions Syntax: Rule expressions
Lifetime Lifetime parameterformat
Reference Command reference
三、藉助crmsh配置mysql高可用
[root@cml1 ~]# crm crm(live)# status Stack: corosync Current DC: cml2 (version1.1.16-12.el7_4.4-94ff4df) - partition with quorum Last updated: Wed Oct 25 09:22:31 2017 Last change: Wed Oct 25 09:22:19 2017 byroot via cibadmin on cml1 2 nodes configured 0 resources configured Online: [ cml1 cml2 ] No resources ##配置以前最好檢查服務沒有啓動 [root@cml1 ~]# systemctl stop mariadb [root@cml1 ~]# umount /dev/drbd1 [root@cml1 ~]# systemctl stop drbd [root@cml1 ~]# systemctl enable mariadb ####須要設置開機啓動在下面配置systemd:mariadb時纔會出現。
(1)增長DRBD資源:
[root@cml1 data]# crm crm(live)# configure crm(live)configure# propertystonith-enabled=false crm(live)configure# propertyno-quorum-policy=ignore crm(live)configure# property migration-limit=1 crm(live)configure# verify crm(live)configure# primitive mysqldrbdocf:linbit:drbd params drbd_resource=mysql op start timeout=240 op stoptimeout=100 op monitor role=Master interval=20 timeout=30 op monitor role=Slaveinterval=30 timeout=30 crm(live)configure# ms ms_mysqldrbdmysqldrbd meta master-max=1 master-node-max=1 clone-max=2 clone-node-max=1notify=true crm(live)configure# verify crm(live)configure# show node 1: cml1 node 2: cml2 primitive mysqldrbd ocf:linbit:drbd \ params drbd_resource=mysql \ op start timeout=240 interval=0 \ op stop timeout=100 interval=0 \ op monitor role=Master interval=20 timeout=30 \ op monitor role=Slave interval=30 timeout=30 ms ms_mysqldrbd mysqldrbd \ meta master-max=1 master-node-max=1 clone-max=2 clone-node-max=1notify=true property cib-bootstrap-options: \ have-watchdog=false \ dc-version=1.1.16-12.el7_4.2-94ff4df \ cluster-infrastructure=corosync \ cluster-name=mycluster \ stonith-enabled=false \ no-quorum-policy=ignore property migration-limit=1 crm(live)configure# commit
(2)增長文件系統資源:
crm(live)configure# primitive mystore ocf:heartbeat:Filesystemparams device=/dev/drbd1 directory=/data fstype=ext4 op start timeout=60 opstop timeout=60 crm(live)configure# verify
(3)給文件系統類型和drbd作親緣性綁定(inf爲證書爲接近,當位負數時候爲分離)。
crm(live)configure# colocationmystore_with_ms_mysqldrbd inf: mystore ms_mysqldrbd:Master crm(live)configure# verify
(4)作順序約束,當drbd起來以後纔對文件系統進行綁定:
crm(live)configure# ordermystore_after_ms_mysqldrbd mandatory: ms_mysqldrbd:promote mystore:start crm(live)configure# verify crm(live)configure# commit
(5)查看一下資源當前的配置:
crm(live)# status Stack: corosync Current DC: cml1 (version1.1.16-12.el7_4.2-94ff4df) - partition with quorum Last updated: Sun Oct 22 14:18:52 2017 Last change: Sun Oct 22 14:18:05 2017 byroot via cibadmin on cml1 2 nodes configured 3 resources configured Online: [ cml1 cml2 ] Full list of resources: Master/Slave Set: ms_mysqldrbd [mysqldrbd] Masters: [ cml1 ] Slaves: [ cml2 ] mystore (ocf::heartbeat:Filesystem): Started cml1
##查看當前配置:
crm(live)configure# show node 1: cml1 node 2: cml2 primitive mysqldrbd ocf:linbit:drbd \ params drbd_resource=mysql \ op start timeout=240 interval=0 \ op stop timeout=100 interval=0 \ op monitor role=Master interval=20 timeout=30 \ op monitor role=Slave interval=30 timeout=30 primitive mystore Filesystem \ params device="/dev/drbd1" directory="/data"fstype=xfs \ op start timeout=60 interval=0 \ op stop timeout=60 interval=0 ms ms_mysqldrbd mysqldrbd \ meta master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true order mystore_after_ms_mysqldrbd Mandatory:ms_mysqldrbd:promote mystore:start colocation mystore_with_ms_mysqldrbd inf:mystore ms_mysqldrbd:Master property cib-bootstrap-options: \ have-watchdog=false \ dc-version=1.1.16-12.el7_4.2-94ff4df \ cluster-infrastructure=corosync \ cluster-name=mycluster \ stonith-enabled=false \ no-quorum-policy=ignore property migration-limit=1
##查看下cml1你應該會發現已經掛載了/data目錄:
[root@cml1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/cl-root 17G 9.0G 8.1G 53% / devtmpfs 588M 0 588M 0% /dev tmpfs 599M 39M 560M 7% /dev/shm tmpfs 599M 8.1M 591M 2% /run tmpfs 599M 0 599M 0% /sys/fs/cgroup /dev/sda1 1014M 168M 847M 17% /boot tmpfs 120M 0 120M 0% /run/user/0 /dev/drbd1 5.0G 62M 5.0G 2% /data
(6)接下來增長一下mysql資源,並做親緣性綁定:
crm(live)configure# primitive mysqldsystemd:mariadb op start timeout=100 op stop timeout=100 crm(live)configure# primitive mysqldsystemd:mariadb crm(live)configure# colocationmysqld_with_mystore inf: mysqld mystore crm(live)configure# verify
(7)提交而且查看:
crm(live)configure# commit crm(live)configure# show node 1: cml1 node 2: cml2 primitive mysqld systemd:mariadb \ op start timeout=100 interval=0 \ op stop timeout=100 interval=0 primitive mysqldrbd ocf:linbit:drbd \ params drbd_resource=mysql \ op start timeout=240 interval=0 \ op stop timeout=100 interval=0 \ op monitor role=Master interval=20 timeout=30 \ op monitor role=Slave interval=30 timeout=30 primitive mystore Filesystem \ params device="/dev/drbd1" directory="/data"fstype=xfs \ op start timeout=60 interval=0 \ op stop timeout=60 interval=0 ms ms_mysqldrbd mysqldrbd \ meta master-max=1 master-node-max=1 clone-max=2 clone-node-max=1notify=true colocation mysqld_with_mystore inf: mysqld mystore order mystore_after_ms_mysqldrbd Mandatory:ms_mysqldrbd:promote mystore:start colocation mystore_with_ms_mysqldrbd inf:mystore ms_mysqldrbd:Master property cib-bootstrap-options: \ have-watchdog=false \ dc-version=1.1.16-12.el7_4.2-94ff4df \ cluster-infrastructure=corosync \ cluster-name=mycluster \ stonith-enabled=false \ no-quorum-policy=ignore property migration-limit=1
(8)做順序約束,先掛載文件系統,而後在啓動msyqld資源:
crm(live)configure# ordermysqld_after_mystore mandatory: mystore mysqld crm(live)configure# verify crm(live)configure# show node 1: cml1 node 2: cml2 primitive mysqld systemd:mariadb \ op start timeout=100 interval=0 \ op stop timeout=100 interval=0 primitive mysqldrbd ocf:linbit:drbd \ params drbd_resource=mysql \ op start timeout=240 interval=0 \ op stop timeout=100 interval=0 \ op monitor role=Master interval=20 timeout=30 \ op monitor role=Slave interval=30 timeout=30 primitive mystore Filesystem \ params device="/dev/drbd1" directory="/data"fstype=xfs \ op start timeout=60 interval=0 \ op stop timeout=60 interval=0 ms ms_mysqldrbd mysqldrbd \ meta master-max=1 master-node-max=1clone-max=2 clone-node-max=1 notify=true order mysqld_after_mystore Mandatory:mystore mysqld colocation mysqld_with_mystore inf: mysqldmystore order mystore_after_ms_mysqldrbd Mandatory:ms_mysqldrbd:promote mystore:start colocation mystore_with_ms_mysqldrbd inf:mystore ms_mysqldrbd:Master property cib-bootstrap-options: \ have-watchdog=false \ dc-version=1.1.16-12.el7_4.2-94ff4df \ cluster-infrastructure=corosync \ cluster-name=mycluster \ stonith-enabled=false \ no-quorum-policy=ignore property migration-limit=1 crm(live)configure# commit
(9)檢測資源,而且看一下node2的mysql是否已經啓動:
crm(live)# status Stack: corosync Current DC: cml1 (version1.1.16-12.el7_4.4-94ff4df) - partition with quorum Last updated: Wed Oct 25 23:45:57 2017 Last change: Wed Oct 25 23:36:20 2017 byroot via crm_attribute on cml1 2 nodes configured 5 resources configured Online: [ cml1 cml2 ] Full list of resources: Master/Slave Set: ms_mysqldrbd [mysqldrbd] Masters: [ cml1 ] Slaves: [ cml2 ] mystore (ocf::heartbeat:Filesystem): Started cml1 mysqld (systemd:mariadb): Started cml1 myvip (ocf::heartbeat:IPaddr): Started cml1 ####咱們看到mysql已經啓動了:
(10)增長VIP資源,做虛擬IP調度:
crm(live)# configure crm(live)configure# primitive myvipocf:heartbeat:IPaddr params ip="192.168.5.200" op monitor interval=20timeout=20 on-fail=restart crm(live)configure# colocation vip_with_ms_mysqldrbd inf:ms_mysqldrbd:Master myvip crm(live)configure# verify crm(live)configure# commit crm(live)configure# show node 1: cml1 \ attributesstandby=off node 2: cml2 \ attributesstandby=off primitive mysqld systemd:mariadb \ opstart timeout=100 interval=0 \ opstop timeout=100 interval=0 primitive mysqldrbd ocf:linbit:drbd \ paramsdrbd_resource=mysql \ opstart timeout=240 interval=0 \ opstop timeout=100 interval=0 \ opmonitor role=Master interval=20 timeout=30 \ opmonitor role=Slave interval=30 timeout=30 primitive mystore Filesystem \ paramsdevice="/dev/drbd1" directory="/data" fstype=ext4 \ opstart timeout=60 interval=0 \ opstop timeout=60 interval=0 primitive myvip IPaddr \ paramsip=192.168.5.250 \ opmonitor interval=20 timeout=20 on-fail=restart ms ms_mysqldrbd mysqldrbd \ metamaster-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true order mysqld_after_mystone Mandatory:mystore mysqld colocation mysqld_with_mystone inf: mysqldmystore order mystone_after_ms_mysqldrbd Mandatory:ms_mysqldrbd:promote mystore:start colocation mystore_with_ms_mysqldrbd inf:mystore ms_mysqldrbd:Master colocation vip_with_ms_mysqldrbd inf:ms_mysqldrbd:Master myvip property cib-bootstrap-options: \ have-watchdog=false\ dc-version=1.1.16-12.el7_4.4-94ff4df\ cluster-infrastructure=corosync\ cluster-name=webcluster\ stonith-enabled=false\ no-quorum-policy=ignore property migration-limit=1
最後全部的工做都完成以後,咱們測試一下vip:
[root@cml1 ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 19G 6.8G 13G 36% /
devtmpfs devtmpfs 501M 0 501M 0% /dev
tmpfs tmpfs 512M 143M 370M 28% /dev/shm
tmpfs tmpfs 512M 20M 493M 4% /run
tmpfs tmpfs 512M 0 512M 0% /sys/fs/cgroup
/dev/sda1 xfs 521M 161M 361M 31% /boot
tmpfs tmpfs 103M 0 103M 0% /run/user/0
/dev/drbd1 ext4 11G 69M 9.9G 1% /data
[root@cml1 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDBServer
Copyright (c) 2000, 2017, Oracle, MariaDBCorporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' toclear the current input statement.
MariaDB [(none)]> show databases;
+---------------------+
| Database |
+---------------------+
| information_schema |
| cml |
| cml2 |
| cmltest |
| #mysql50#lost+found |
| mysql |
| performance_schema |
| test |
| testcml |
+---------------------+
9 rows in set (0.01 sec)
###如今操做是受權
MariaDB [(none)]> GRANT ALL ON *.* TO'root'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
###使用客戶端訪問vip:
[root@cml3 ~]# mysql -uroot -p123456 -h192.168.5.250
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.56-MariaDB MariaDBServer
Copyright (c) 2000, 2017, Oracle, MariaDBCorporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' toclear the current input statement.
MariaDB [(none)]> show databases;
+---------------------+
| Database |
+---------------------+
| information_schema |
| cml |
| cml2 |
| cmltest |
| #mysql50#lost+found |
| mysql |
| performance_schema |
| test |
| testcml |
+---------------------+
9 rows in set (0.00 sec)
###下面咱們把cml1設置爲standby,而後在cml2訪問:
crm(live)#node standby
crm(live)#status
Stack:corosync
CurrentDC: cml1(version 1.1.16-12.el7_4.2-94ff4df) - partition with quorum
Lastupdated: Sun Oct 22 14:54:14 2017
Lastchange: Sun Oct 22 14:54:02 2017 by root via crm_attribute on cml1
2 nodesconfigured
5resources configured
Node cml1:standby
Online:[ cml2 ]
Fulllist of resources:
Master/Slave Set: ms_mysqldrbd [mysqldrbd]
Masters: [ cml2 ]
Stopped: [ cml1 ]
mystore (ocf::heartbeat:Filesystem): Started cml2
mysqld (systemd:mariadb): Started cml2
myvip (ocf::heartbeat:IPaddr): Started cml2
###在cml2上面訪問(咱們發現vip已經漂移過來,數據也漂移了過來):
[root@cml2~]# mysql -uroot -p123456 -h 192.168.5.250
Welcometo the MariaDB monitor. Commands endwith ; or \g.
YourMariaDB connection id is 3
Serverversion: 5.5.56-MariaDB MariaDB Server
Copyright(c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB[(none)]> show databases;
+---------------------+
|Database |
+---------------------+
|information_schema |
|cml |
|cml2 |
|cmltest |
|#mysql50#lost+found |
|mysql |
|performance_schema |
|test |
|testcml |
+---------------------+
9 rowsin set (0.00 sec)