1. 程式人生 > >keepalived+haproxy 安裝 PXC高可用

keepalived+haproxy 安裝 PXC高可用

pxc節點:10.194.41.231    10.194.41.228   10.194.41.227
ha: 10.194.41.220 主    10.194.41.221 備      
vip:10.194.41.199

一.安裝keepalived
1.yum install keepalived

2.修改配置
主節點4上
vi /etc/keepalived/keepalived.conf
vrrp_script chk_haproxy {
        script "killall -0 haproxy"  # verify the pid existance
        interval 2          # check every 2 seconds
        weight -2          # add 2 points of prio if OK
   }
vrrp_instance PXC_3306 {
        interface eth0        # interface to monitor
        state MASTER
        virtual_router_id 51     # Assign one ID for this route
        priority 101         # 101 on master, 100 on backup
        nopreempt
        debug
        virtual_ipaddress {
        10.194.41.199/24 dev eth0 label eth0:0
        }
track_script {
        chk_haproxy
        }
        notify_master /etc/keepalived/scripts/start_haproxy.sh #表示當切換到master狀態時,要執行的指令碼
        notify_fault /etc/keepalived/scripts/stop_keepalived.sh #故障時執行的指令碼
        notify_stop  /etc/keepalived/scripts/stop_haproxy.sh #keepalived 停止執行前執行notify_stop指定的指令碼
 }
 
 注:備節點5上/etc/keepalived/keepalived.conf,做相同配置,只修改
     state BACKUP
     priority 90
     
     
3.各個執行指令碼
3.1  當切換到master狀態時,要執行的指令碼 
vi /etc/keepalived/scripts/start_haproxy.sh

#!/bin/bash    
sleep 5 
get=`ip addr |grep 10.194.41.199 |wc -l` echo $get >> /etc/keepalived/scripts/start_ha.log    
if [ $get -eq 1 ]
 then    
        echo "`date +%c` success to get vip" >> /etc/keepalived/scripts/start_ha.log     
        /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg      #根據實際haproxy修改
 else    
        echo "`date +%c` can not get vip" >> /etc/keepalived/scripts/start_ha.log 
fi


3.2   故障時執行的指令碼
vi /etc/keepalived/scripts/stop_keepalived.sh 
#!/bin/bash    
pid=`pidof keepalived` 
if [ $pid == "" ] 
then  
        echo "`date +%c` no keepalived process id" >> /etc/keepalived/scripts/stop_keep.log 
else  
        echo "`date +%c` will stop keepalived " >> /etc/keepalived/scripts/stop_keep.log   
        /etc/init.d/keepalived stop 
fi   


3.3  keepalived 停止執行前執行notify_stop指定的指令碼
vi /etc/keepalived/scripts/stop_haproxy.sh
#!/bin/bash
pid=`pidof haproxy`
echo "`date +%c` stop haproxy" >> /etc/keepalived/scripts/stop_ha.log
kill -9 $pid


二、安裝haproxy
wget http://www.haproxy.org/download/1.4/src/haproxy-1.4.27.tar.gz
tar xvfz haproxy-1.4.27.tar.gz -C /tmp/
cd /tmp/haproxy-1.4.27 
make TARGET=linux26 PREFIX=/usr/local/haproxy 
make install PREFIX=/usr/local/haproxy
cd /usr/local/haproxy/ 
mkdir conf logs 
vim conf/haproxy.cfg
global
 maxconn 51200
 #uid 99
 #gid 99
 chroot /usr/local/haproxy
 daemon
 #quiet
 nbproc 1
 pidfile  /usr/local/haproxy/logs/haproxy.pid

defaults
 mode tcp
 option redispatch
 option abortonclose
 timeout connect 5000s
 timeout client 50000s
 timeout server 50000s
 log 127.0.0.1 local0
 balance roundrobin

listen proxy
 bind 10.194.41.199:3307
 mode tcp
 option httpchk
 server db1 10.194.41.231:3306 weight 1 check port 9200 inter 12000 rise 3 fall 3
 server db2 10.194.41.228:3306 weight 1 check port 9200 inter 12000 rise 3 fall 3
 server db2 10.194.41.227:3306 weight 1 check port 9200 inter 12000 rise 3 fall 3

listen haproxy_stats
 mode http
 bind 10.194.41.220:8888
 option httplog
 stats refresh 5s
 stats uri /status
 stats realm Haproxy Manager
 stats auth admin:evis123
 
 
注: haproxy安裝和配置兩節點4,5一樣

4和5的核心引數修改/etc/sysctl.conf增加
net.ipv4.ip_nonlocal_bind=1
net.ipv4.ip_forward = 1

sysctl  -p  生效


三、資料庫節點
    安裝mysql健康狀態檢查指令碼,用於haproxy檢查, 每個節點都安裝
    
1)指令碼拷貝
# cp /usr/local/mysql/bin/clustercheck /usr/bin/
# cp /usr/local/mysql/xinetd.d/mysqlchk /etc/xinetd.d/
# cp /usr/local/mysql/bin/mysql /usr/bin/ 

    ps:clustercheck和指令碼都是預設值沒有修改


注:如不使用clustercheck中預設使用者名稱和密碼,將需要修改clustercheck指令碼,MYSQL_USERNAME和MYSQL_PASSWORD值
clustercheckuser
clustercheckpassword!
CREATE USER 'clustercheckuser'@'%' identified BY 'clustercheckpassword!'; 
GRANT ALL ON *.* TO 'clustercheckuser'@'%'; 
flush privileges;


2)更改/etc/services新增mysqlchk的服務埠號:
# echo 'mysqlchk 9200/tcp # mysqlchk' >> /etc/services

3)安裝xinetd服務,通過守護程序來管理mysql健康狀態檢查指令碼
# yum -y install xinetd
# /etc/init.d/xinetd restart
Stopping xinetd: [FAILED]
Starting xinetd: [ OK ]
# chkconfig --level 2345 xinetd on
# chkconfig --list |grep xinetd
xinetd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

  
安裝完成後,將xinetd服務加入開機自啟動:
    systemctl enable xinetd.service
將telnet服務加入開機自啟動:
    systemctl enable telnet.socket
最後,啟動以上兩個服務即可:
    systemctl start telnet.socket
    systemctl start xinetd(或service xinetd start)
    

測試檢測指令碼:
# clustercheck
HTTP/1.1 200 OK
Content-Type: text/plain
Connection: close
Content-Length: 40

Percona XtraDB Cluster Node is synced.

注:ps:要保證狀態為200,否則檢測不通過,可能是mysql服務不正常,或者環境不對致使haproxy無法使用mysql
haproxy如何偵測 MySQL Server 是否存活,靠著就是 9200 port,透過 Http check 方式,讓 HAProxy 知道 PXC 狀態

在mysql叢集的其他節點執行上面操作,保證各個節點返回狀態為200,如下:
  curl -I 10.194.41.231:9200
  curl -I 10.194.41.228:9200
  curl -I 10.194.41.227:9200



四、最後啟動測試
1.啟動keepalived 
  /etc/init.d/keepalived start
  
  1;systemctl daemon-reload  重新載入

2:systemctl enable keepalived.service  設定開機自動啟動

3:systemctl disable keepalived.service 取消開機自動啟動

4:systemctl start keepalived.service 啟動

5:systemctl stop keepalived.service停止
  
  
  
2.啟動haproxy
  /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
  
 http://10.194.41.220:8888/status