(第十八周作業(yè))
(part1? 的鏈接為? http://www.reibang.com/p/4c1bf1daabaf)
3、采用corosync?v2+pacemaker實(shí)現(xiàn)mariadb的高可用
需要3臺(tái)虛擬機(jī)柠傍,ABC三臺(tái)虛擬機(jī)麸俘,IP分別是192.168.184.136、192.168.184.137惧笛、192.168.184.138
1)ABC三臺(tái)服務(wù)器做時(shí)間同步从媚,可以使用NPT或chronyc,三臺(tái)都執(zhí)行以下命令?
yum install chrony -y
systemctl start chronyd
systemctl enable chronyd
2)設(shè)置主機(jī)名和配置hosts文件患整、建立ssh免密鑰登陸
A機(jī):hostnamectl??--static?set-hostname? nodea.magedu.com
B機(jī):hostnamectl??--static?set-hostname? nodeb.magedu.com
C機(jī):hostnamectl??--static?set-hostname? nodec.magedu.com
修改hosts文件
A機(jī):vi? /etc/hosts
127.0.0.1? ?nodea.magedu.com? nodea
192.168.184.136? ?nodea.magedu.com? nodea
192.168.184.137? ?nodeb.magedu.com? nodeb
192.168.184.138? ?nodec.magedu.com? nodec
B機(jī)和C機(jī)也類似修改拜效。
A機(jī):
#?ssh-keygen
# ssh-copy-id -i /root/.ssh/id_rsa root@nodeb
# ssh-copy-id -i /root/.ssh/id_rsa root@nodec
然后ssh?到另外兩臺(tái),第一次要輸入yes
B機(jī)和C機(jī)也類似操作各谚。
3)ABC機(jī)安裝mariadb拂檩,?pacemaker??corosync?
#?yum?-y?install?mariadb-server
#?systemctl?enable?mariadb.service
# yum -y install pacemaker?corosync?
# systemctl?start?corosync.service?pacemaker.service
4)ABC機(jī)安裝nfs
#?yum?-y?install?nfs-utils
#?mkdir?/mysql
# chown?mysql.mysql?/mysql
在A機(jī)執(zhí)行:
配置NFS目錄
#?vi? ?/etc/exports
/mysql??192.168.184.0/24(rw,no_root_squash)
在BC機(jī)執(zhí)行:
# showmount?-e?192.168.184.136
#?mount?-t?nfs?192.168.184.136:/mysql? ? /mysql
5)在ABC機(jī)配置數(shù)據(jù)庫(kù)
#?vim?/etc/my.cnf
datadir=/mysql
skip_name_resolve=on
innodb_file_per_table=on
啟動(dòng)mysql數(shù)據(jù)庫(kù)
6)在A機(jī)配置corosync.conf文件
# vi??/etc/corosync/corosync.conf
totem {? ?#定義心跳信息傳遞信息
????version: 2? ??#定義corosync版本
????crypto_cipher: aes128
????crypto_hash: sha1
????secauth: on? ?#是否需要安全認(rèn)證
????interface {
????????ringnumber: 0? ?#起始號(hào)
????????bindnetaddr: 192.168.184.0? ??#綁定在哪個(gè)網(wǎng)絡(luò)地址
????????mcastaddr: 239.255.1.1? ??#組播地址,為了與另一個(gè)節(jié)點(diǎn)傳遞心跳信息
????????mcastport: 5405? ??#組播地址端口號(hào)
????????ttl: 1
????}
}
nodelist {
????node {
????????ring0_addr: 192.168.184.136
????????nodeid: 1
????}
????node {
????????ring0_addr: 192.168.184.137
????????nodeid: 2
????}
????node {
????????ring0_addr: 192.168.184138
????????nodeid: 3
????}
}
logging {? ??#定義日志功能
????fileline: off
????to_stderr: no? ? ?#是否將錯(cuò)誤日志輸出到終端
????to_logfile: yes? ? ?#是否啟用專門的日志文件
? ? logfile: /var/log/cluster/corosync.log? ?#日志文件存放位置
????to_syslog:?no? ?#是否將日志記錄到linux默認(rèn)日志文件中
????debug: off? ? ?#是否開(kāi)啟debug日志信息
????timestamp: on? ??#是否開(kāi)啟日志記錄時(shí)間戳
????logger_subsys {
????????subsys: QUORUM
????????debug: off
????}
}
quorum {
????provider: corosync_votequorum
}
7)在A機(jī)創(chuàng)建一個(gè)authkey文件嘲碧,
# cd /dev/
# mv random random.bak
# ln -sv urandom random
# corosync-keygen
# rm -rf random
# mv random.bak random
# cd /etc/corosync/
復(fù)制到BC機(jī)上
# scp corosync.conf authkey?nodeb:/etc/corosync/
# scp corosync.conf authkey nodec:/etc/corosync/
8)在ABC機(jī)上啟動(dòng)corosync和pacemaker服務(wù)。
# systemctl? restart corosync.service
# systemctl? restart pacemaker.service
9)在A機(jī)上安裝crmsh父阻,完成資源配置
在https://github.com/ClusterLabs/crmsh地址下載源碼愈涩,解壓后,用以下命令進(jìn)行編譯安裝:
# ./autogen.sh
#./configure
#make && make install
安裝好后加矛,進(jìn)行配置
# crm configure
crm(live)configure# property stonith-enabled=false??
rm(live)configure# primitive mysqlip ocf:heartbeat:IPaddr params ip=’192.168.184.100’ op start timeout=60s op stop timeout=60s op monitor interval=20s timeout=40s
crm(live)configure# primitive mysqlstore ocf:heartbeat:Filesystem params device=’192.168.184.136:/data’ directory=’/data’ fstype=’nfs’ op start timeout=60s op stop timeout=60s op monitor interval=20s timeout=40s
crm(live)configure# primitive mysqlserver lsb:mysqld params op start timeout=60s op stop timeout=60s op monitor interval=20s timeout=40s
crm(live)configure# group mysqlservice mysqlip mysqlstore mysqlserver
crm(live)configure# order mysqlip_before_mysqlstore Mandatory: mysqlip mysqlstore
crm(live)configure# order mysqlstore_before_mysqlserver Mandatory: mysqlstore mysqlserver
crm(live)configure# verify
crm(live)configure# commit
crm(live)configure# bye
10)查看節(jié)點(diǎn)狀態(tài)
#crm status
4履婉、采用Corosync+Pacemaker+nfs實(shí)現(xiàn)http高可用
使用3臺(tái)虛擬機(jī):
A機(jī):主機(jī)名nodeA? ?IP地址:192.168.184.150? ? 安裝corosync+pacemaker+httpd??
B機(jī):主機(jī)名nodeB? ?IP地址:192.168.184.151? ?安裝corosync+pacemaker+httpd
C機(jī):主機(jī)名nfsShare? IP地址:192.168.184.152? ??安裝nfs,提供磁盤共享
1)設(shè)置主機(jī)名和hosts文件
A機(jī):hostnamectl --static set-hostname nodeA
B機(jī):hostnamectl --static set-hostname nodeB
C機(jī):hostnamectl --static set-hostname nfsShare
在A機(jī)和B機(jī)修改/etc/hosts文件
# vi? /etc/hosts
增加以下兩行內(nèi)容斟览,這樣可以用主機(jī)名進(jìn)行解析通信毁腿。
192.168.184.150 nodeA
192.168.184.151? nodeB
2)使用ssh密鑰實(shí)現(xiàn)A機(jī)和B機(jī)互相通信。
A機(jī):
#?ssh-keygen
# ssh-copy-id -i /root/.ssh/id_rsa root@nodeA
B機(jī):
#?ssh-keygen
# ssh-copy-id -i /root/.ssh/id_rsa root@nodeB
然后ssh?到另外一臺(tái),第一次要輸入yes已烤,把密鑰指紋保存一下鸠窗。
3)設(shè)置ntp和crontab做時(shí)間同步
A機(jī)和B機(jī)上都要執(zhí)行
# yum install ntp -y
# crontab -e?
增加一個(gè)NTP同步任務(wù)
*/3? *? *? *? *? ?/usr/sbin/ntpdate??ntp1.aliyun.com? ?&> /dev/null
4)設(shè)置NFS
C機(jī):
# yum install??nfs-utils -y
# mkdir??/web/htdocs
#?echo "<h1>this page is NFS?share?Page</h1>"? >>? /web/htdocs/index.html?
# vi??/etc/exports
/web/htdocs? 192.168.184.0/24(rw)
重啟nfs
#?service? nfs? start
5)在A機(jī)和B機(jī)上安裝corosync、pacemaker和httpd
# yum install??corosync??pacemaker? ?httpd -y?
6)在A機(jī)上創(chuàng)建corosync.conf配置文件
vi? /etc/corosync/corosync.conf
compatibility: whitetank? ??#是否兼容舊版本的corosync
totem {
????version:2
????secauth: on
????threads:0? ?#啟動(dòng)多少個(gè)線程處理心跳信息
????interface {
????????ringnumber:0
????????bindnetaddr:192.168.184.0?
????????mcastaddr:?239.255.1.1?
????????mcastport:5405?
????????ttl:1
????}
}
logging {?
????fileline: off
????to_stderr: no
????to_logfile: yes
????to_syslog: no?
????logfile: /var/log/cluster/corosync.log?
????debug: off?
????timestamp: on?
????logger_subsys {
????????subsys: AMF
????????debug: off
????}
}
amf {
????mode: disabled
}
service{? ? ? ? #設(shè)定使用pacemaker服務(wù)
????ver:0
????name: pacemaker
}
aisexec{? ? ? ? ?#定義運(yùn)行時(shí)使用的用戶和組
????user: root
????group: root
}
# scp? ? corosync.conf? ? ?root@nodeB:/etc/corosync/
7)在A機(jī)上創(chuàng)建authkey文件再?gòu)?fù)制到B機(jī)
# cd /dev/
# mv random random.bak
# ln -sv urandom random
# corosync-keygen
# rm -rf random
# mv random.bak random
# cd /etc/corosync/
# scp? ?authkey? ?root@nodeB:/etc/corosync/
8)重啟corosync胯究、pacemaker和httpd服務(wù)
# systemctl? restart corosync.service
# systemctl? restart pacemaker.service
#?systemctl? restart?httpd.service
9)?在A機(jī)上安裝crmsh
#cd? /tmp
#? ?wget? http://www.rpmfind.net/linux/epel/6/x86_64/Packages/p/pssh-2.3.1-5.el6.noarch.rpm
#? ?wget??ftp://ftp.pbone.net/mirror/ftp5.gwdg.de/pub/opensuse/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-6/x86_64/crmsh-1.2.6-0.rc2.2.1.x86_64.rpm
#yum? install? ?pssh-2.3.1-5.el6.noarch.rpm
#yum install? ? ?crmsh-1.2.6-0.rc2.2.1.x86_64.rpm
10)在A機(jī)上執(zhí)行
#crm?
crm(live)# configure
crm(live)configure# property stonith-enabled=false
crm(live)configure# property no-quorum-policy=ignore
crm(live)configure# verify
crm(live)configure# commit
定義資源,包括webip稍计,webserver,webstore
crm(live)configure# primitive webip ocf:IPaddr params ip=192.168.184.200 op monitor interval="30s" timeout="20s"
crm(live)configure# primitive webserver lsb:httpd op monitor interval="30s" timeout="20s"
crm(live)configure# primitive webstore ocf:Filesystem params device="192.168.184.152:/web/htdocs" directory="/var/www/html" fstype="nfs" op monitor interval="60s" timeout="40s" op start timeout="60s" interval="0" op stop timeout="60s" interval="0"
crm(live)configure# verify
定義組和順序約束
crm(live)configure# group webservice webip webstore webserver
crm(live)configure# order webip_before_webstore_before_webserver inf:webip webstore webserver
crm(live)configure# verify
crm(live)configure# commit
11)在A機(jī)上執(zhí)行裕循,查看狀態(tài)
#?crm
crm(live)# status?
12)使用瀏覽器查看網(wǎng)站
http://192.168.184.200
13)驗(yàn)證臣嚣,把nodeA下線,查看狀態(tài)剥哑,再上線
在A機(jī)上執(zhí)行
#?crm node standby? ? #把nodeA下線
#?crm status?查看狀態(tài)硅则,發(fā)現(xiàn)資源已轉(zhuǎn)移至node2
再查看網(wǎng)頁(yè)http://192.168.184.200發(fā)現(xiàn)仍可正常訪問(wèn)
#? crm node online? ? ?#把nodeA上線