程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> Keepalived+HAProxy完成MySQL高可用負載平衡的設置裝備擺設

Keepalived+HAProxy完成MySQL高可用負載平衡的設置裝備擺設

編輯:MySQL綜合教程

Keepalived+HAProxy完成MySQL高可用負載平衡的設置裝備擺設。本站提示廣大學習愛好者:(Keepalived+HAProxy完成MySQL高可用負載平衡的設置裝備擺設)文章只能為提供參考,不一定能成為您想要的結果。以下是Keepalived+HAProxy完成MySQL高可用負載平衡的設置裝備擺設正文


 Keepalived

因為在臨盆情況應用了mysqlcluster,須要完成高可用負載平衡,這裡供給了keepalived+haproxy來完成.

      keepalived重要功效是完成真實機械的毛病隔離及負載平衡器間的掉敗切換.可在第3,4,5層交流.它經由過程VRRPv2(Virtual Router Redundancy Protocol) stack完成的.

      Layer3:Keepalived會按期向辦事器群中的辦事器.發送一個ICMP的數據包(既我們日常平凡用的Ping法式),假如發明某台辦事的IP地址沒有激活,Keepalived便申報這台辦事器掉效,並將它從辦事器群中剔除,這類情形的典范例子是某台辦事器被不法關機。Layer3的方法是以辦事器的IP地址能否有用作為辦事器任務正常與否的尺度。

     Layer4:重要以TCP端口的狀況來決議辦事器任務正常與否。如web server的辦事端口普通是80,假如Keepalived檢測到80端口沒有啟動,則Keepalived將把這台辦事器從辦事器群中剔除。

     Layer5:在收集上占用的帶寬也要年夜一些。Keepalived將依據用戶的設定檢討辦事器法式的運轉能否正常,假如與用戶的設定不符合,則Keepalived將把辦事器從辦事器群中剔除。

Software Design

2016228164756604.jpg (597×381)

keepalived啟動後會有單個過程

8352 ?    Ss   0:00 /usr/sbin/keepalived
8353 ?    S   0:00 \_ /usr/sbin/keepalived
8356 ?    S   0:01 \_ /usr/sbin/keepalived

父過程:內存治理,子過程治理等等

子過程:VRRP子過程

子過程:Healthchecking 子過程
實例

2台mysqlcluster 10.1.6.203 master  10.1.6.205 backup

vip 10.1.6.173

目標拜訪10.1.6.173 3366端口 分離輪詢經由過程haproxy轉發到10.1.6.203 3306 和10.1.6.205 3306

mysqlcluster搭建參照之前博客,這裡在2台機上裝置keepalived

[email protected]:~# apt-get install keepalived
[email protected]:~# cat /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 VI_1 {
    interface eth1        # 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.1.6.173
    }
 
    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指定的劇本 }

VRRPD設置裝備擺設包含三個類:

  • VRRP同步組(synchroization group)
  • VRRP實例(VRRP Instance)
  • VRRP劇本

這裡應用了 VRRP實例, VRRP劇本

留意設置裝備擺設選項:

stat:指定instance(Initial)的初始狀況,就是說在設置裝備擺設好後,這台辦事器的初始狀況就是這裡指定的,但這裡指定的不算,照樣得要經由過程競選經由過程優先級來肯定,裡假如這裡設置為master,但如若他的優先級不及別的一台,那末這台在發送公告時,會發送本身的優先級,別的一台發明優先級不如本身的高,那末他會就回搶占為master

  • interface:實例綁定的網卡,由於在設置裝備擺設虛擬IP的時刻必需是在已有的網卡上添加的
  • priority 101:設置本節點的優先級,優先級高的為master
  • debug:debug級別
  • nopreempt:設置為不搶占
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 劇本成果招致的優先級變革:2表現優先級+2;-2則表現優先級-2
}

然後在實例(vrrp_instance)外面援用,有點相似劇本外面的函數援用一樣:先界說,後援用函數名

    track_script {
        chk_haproxy
    }

留意:VRRP劇本(vrrp_script)和VRRP實例(vrrp_instance)屬於統一個級別

[email protected]:scripts# cat start_haproxy.sh 
#!/bin/bash
 
sleep 5
get=`ip addr |grep 10.1.6.173 |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/sbin/haproxy -f /etc/haproxy/haproxy.cfg
else
    echo "`date +%c` can not get vip" >> /etc/keepalived/scripts/start_ha.log
fi
[email protected]:scripts# cat 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
 
/etc/init.d/keepalived stop
 
[email protected]:scripts# cat stop_haproxy.sh 
#!/bin/bash
 
pid=`pidof haproxy`
echo "`date +%c` stop haproxy" >> /etc/keepalived/scripts/stop_ha.log
kill -9 $pid

同理設置裝備擺設10.1.6.205

[email protected]:~# cat /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 VI_1 {
  interface eth1        # interface to monitor
  state BACKUP
  virtual_router_id 51     # Assign one ID for this route
  priority 100         # 101 on master, 100 on backup
  virtual_ipaddress {
    10.1.6.173
  }
 
  track_script {
    chk_haproxy
  }
 
notify_master /etc/keepalived/scripts/start_haproxy.sh
notify_fault /etc/keepalived/scripts/stop_keepalived.sh
notify_stop /etc/keepalived/scripts/stop_haproxy.sh
 
}

 HAProxy

上面再引見下haproxy

       HAProxy是一款基於TCP(第四層)和HTTP(第七層)運用的署理軟件,它也可作為負載平衡器.可以支撐數以萬計的並發銜接.同時可以掩護辦事器不裸露到收集上,經由過程端口映照.它還自帶監控辦事器狀況的頁面.

      裝置haproxy

wget -O/tmp/haproxy-1.4.22.tar.gz http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz
tar xvfz /tmp/haproxy-1.4.22.tar.gz -C /tmp/
cd /tmp/haproxy-1.4.22
make TARGET=linux26
make install

      haproxy須要對每個mysqlcluster辦事器停止安康檢討

1.在2台主機分離設置裝備擺設haproxy.cfg

[email protected]:scripts# cat /etc/haproxy/haproxy.cfg 
global
    maxconn 51200 #默許最年夜銜接數 
    #uid 99
    #gid 99
    daemon    #今後台情勢運轉haproxy
    #quiet
    nbproc 1   #過程數目(可以設置多個過程進步機能) 
    pidfile /etc/haproxy/haproxy.pid #haproxy的pid寄存途徑,啟動過程的用戶必需有權限拜訪此文件 
 
defaults
    mode tcp      #所處置的種別 (#7層 http;4層tcp ) 
    option redispatch  #serverId對應的辦事器掛失落後,強迫定向到其他安康的辦事器 
    option abortonclose #當辦事器負載很高的時刻,主動停止失落以後隊列處置比擬久的銜接 
    timeout connect 5000s  #銜接超時
    timeout client 50000s #客戶端超時
    timeout server 50000s  #辦事器超時
    log 127.0.0.1 local0  #毛病日記記載
    balance roundrobin  #默許的負載平衡的方法,輪詢方法 
 
listen proxy
    bind 10.1.6.173:3366  #監聽端口 
    mode tcp        #http的7層形式
    option httpchk    #心跳檢測的文件
    server db1 10.1.6.203:3306 weight 1 check port 9222 inter 12000 rise 3 fall 3   #辦事器界說,check inter 12000是檢測心跳頻率 rise 3是3次准確以為辦事器可用, fall 3是3次掉敗以為辦事器弗成用,weight代表權重 
    server db2 10.1.6.205:3306 weight 1 check port 9222 inter 12000 rise 3 fall 3
 
listen haproxy_stats
    mode http
    bind 10.1.6.173:8888
    option httplog
    stats refresh 5s  
    stats uri /status #網站安康檢測URL,用來檢測HAProxy治理的網站能否可以用,正常前往200,不正常前往503 
    stats realm Haproxy Manager
    stats auth admin:p@a1SZs24 #賬號暗碼

[email protected]:~$ cat /etc/haproxy/haproxy.cfg 
global
    maxconn 51200
    #uid 99
    #gid 99
    daemon
    #quiet
    nbproc 1
    pidfile /etc/haproxy/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.1.6.173:3366
    mode tcp
    option httpchk
    server db1 10.1.6.203:3306 weight 1 check port 9222 inter 12000 rise 3 fall 3
    server db2 10.1.6.205:3306 weight 1 check port 9222 inter 12000 rise 3 fall 3
 
listen haproxy_stats
    mode http
    bind 10.1.6.173:8888
    option httplog
    stats refresh 5s  
    stats uri /status 
    stats realm Haproxy Manager
    stats auth admin:p@a1SZs24

2.裝置xinetd

[email protected]:~# apt-get install xinetd

3.在每一個節點添加xinetd辦事劇本和mysqlchk端標語

[email protected]:~# vim /etc/xinetd.d/mysqlchk 
# default: on
# description: mysqlchk
service mysqlchk         #須要在servive界說
{
    flags      = REUSE
    socket_type   = stream
    port      = 9222
    wait      = no
    user      = nobody
    server     = /opt/mysqlchk 
    log_on_failure += USERID
    disable     = no
    per_source   = UNLIMITED
    bind      = 10.1.6.173
}
 
[email protected]:~# vim /etc/services 
mysqlchk    9222/tcp            # mysqlchk

4.編寫mysqlchk監控辦事劇本

[email protected]:~# ls -l /opt/mysqlchk 
-rwxr--r-- 1 nobody root 1994 2013-09-17 11:27 /opt/mysqlchk
[email protected]:~# cat /opt/mysqlchk 
#!/bin/bash
#
# This script checks if a mysql server is healthy running on localhost. It will
# return:
# "HTTP/1.x 200 OK\r" (if mysql is running smoothly)
# - OR -
# "HTTP/1.x 500 Internal Server Error\r" (else)
#
# The purpose of this script is make haproxy capable of monitoring mysql properly
#
 
MYSQL_HOST="localhost"
MYSQL_SOCKET="/var/run/mysqld/mysqld.sock"
MYSQL_USERNAME="mysqlchkusr"   #該賬戶暗碼須要在mysql裡添加
MYSQL_PASSWORD="secret"
MYSQL_OPTS="-N -q -A"
TMP_FILE="/dev/shm/mysqlchk.$$.out"
ERR_FILE="/dev/shm/mysqlchk.$$.err"
FORCE_FAIL="/dev/shm/proxyoff"
MYSQL_BIN="/opt/mysqlcluster/mysql-cluster-gpl-7.2.6-linux2.6-x86_64/bin/mysql"
CHECK_QUERY="select 1"
 
preflight_check()
{
  for I in "$TMP_FILE" "$ERR_FILE"; do
    if [ -f "$I" ]; then
      if [ ! -w $I ]; then
        echo -e "HTTP/1.1 503 Service Unavailable\r\n"
        echo -e "Content-Type: Content-Type: text/plain\r\n"
        echo -e "\r\n"
        echo -e "Cannot write to $I\r\n"
        echo -e "\r\n"
        exit 1
      fi
    fi
  done
}
 
return_ok()
{
  echo -e "HTTP/1.1 200 OK\r\n"
  echo -e "Content-Type: text/html\r\n"
  echo -e "Content-Length: 43\r\n"
  echo -e "\r\n"
  echo -e "<html><body>MySQL is running.</body></html>\r\n"
  echo -e "\r\n"
  rm $ERR_FILE $TMP_FILE
  exit 0
}
return_fail()
{
  echo -e "HTTP/1.1 503 Service Unavailable\r\n"
  echo -e "Content-Type: text/html\r\n"
  echo -e "Content-Length: 42\r\n"
  echo -e "\r\n"
  echo -e "<html><body>MySQL is *down*.</body></html>\r\n"
  sed -e 's/\n$/\r\n/' $ERR_FILE
  echo -e "\r\n"
  rm $ERR_FILE $TMP_FILE
  exit 1
}
preflight_check
if [ -f "$FORCE_FAIL" ]; then
    echo "$FORCE_FAIL found" > $ERR_FILE
    return_fail;
fi
$MYSQL_BIN $MYSQL_OPTS --host=$MYSQL_HOST --socket=$MYSQL_SOCKET --user=$MYSQL_USERNAME --password=$MYSQL_PASSWORD -e "$CHECK_QUERY" > $TMP_FILE 2> $ERR_FILE
if [ $? -ne 0 ]; then
    return_fail;
fi
return_ok;

測試

2個節點開啟keepalived(主節點會取得vip,主動拉起haproxy),xinetd

[email protected]:~# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
  link/ether 00:26:b9:36:0f:81 brd ff:ff:ff:ff:ff:ff
  inet 211.151.105.186/26 brd 211.151.105.191 scope global eth0
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  link/ether 00:26:b9:36:0f:83 brd ff:ff:ff:ff:ff:ff
  inet 10.1.6.203/24 brd 10.1.6.255 scope global eth1
  inet 10.1.6.173/32 scope global eth1
4: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
  link/ether 00:26:b9:36:0f:85 brd ff:ff:ff:ff:ff:ff
5: eth3: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
  link/ether 00:26:b9:36:0f:87 brd ff:ff:ff:ff:ff:ff
[email protected]:~# netstat -tunlp | grep ha
tcp    0   0 10.1.6.173:3366   0.0.0.0:*        LISTEN   1042/haproxy  
tcp    0   0 10.1.6.203:8888   0.0.0.0:*        LISTEN   1042/haproxy  
udp    0   0 0.0.0.0:56562      0.0.0.0:*              1042/haproxy  
[email protected]:~# netstat -tunlp | grep xine
tcp    0   0 10.1.6.203:9222   0.0.0.0:*        LISTEN   30897/xinetd  
[email protected]:~# ps -ef | grep haproxy
root   1042   1 0 Sep17 ?    00:00:00 /usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg

測試:

經由過程vip10.1.6.173 3366拜訪cluster數據庫(留意賬戶dave權限須要加3個ip10.1.6.203,10.1.6.205,10.1.6.173)

[email protected]:mgm# mysql -udave -p -h 10.1.6.173 -P 3366
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1344316
Server version: 5.5.22-ndb-7.2.6-gpl-log MySQL Cluster Community Server (GPL)
 
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
 
mysql> show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema | 
| dave       | 
| test        | 
+--------------------+
3 rows in set (0.01 sec)
 
mysql>

手動分離使keepalive,haproxy,數據庫掛失落.vip10.1.6.173會主動漂到10.1.6.205從上,其實不影響vip的拜訪

經由過程vip,haproxy檢查各節點狀況

http://10.1.6.173:8888/status

2016228164844383.jpg (1158×481)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved