Mysql雙主熱備+LVS+Keepalived高可用操作記錄

散盡浮華發表於2016-06-08

 

MySQL複製能夠保證資料的冗餘的同時可以做讀寫分離來分擔系統壓力,如果是主主複製還可以很好的避免主節點的單點故障。然而MySQL主主複製存在一些問題無法滿足我們的實際需要:未提供統一訪問入口來實現負載均衡,如果其中master宕掉的話需要手動切換到另外一個master,而不能自動進行切換。前面介紹了Mysql+Keepalived雙主熱備高可用方案記錄,那篇文件裡沒有使用到LVS(實現負載均衡),而下面要介紹的就是如何通過Keepalived+LVS方式來是實現MySQL的高可用性,利用LVS實現MySQL的讀寫負載均衡,Keepalived避免節點出現單點故障,同時解決以上問題。

Keepalived是一個基於VRRP(虛擬路由冗餘協議)可用來實現服務高可用性的軟體方案,避免出現單點故障。Keepalived一般用來實現輕量級高可用性,且不需要共享儲存,一般用於兩個節點之間,常見有LVS+Keepalived、Nginx+Keepalived組合。

LVS(Linux Virtual Server)是一個高可用性虛擬的伺服器叢集系統。本專案在1998年5月由章文嵩博士成立,是中國國內最早出現的自由軟體專案之一。LVS主要用於多伺服器的負載均衡,作用於網路層。LVS構建的伺服器叢集系統中,前端的負載均衡層被稱為Director Server;後端提供服務的伺服器組層被稱為Real Server。通過下圖可以大致瞭解LVS的基礎架構。

LVS有三種工作模式,分別是:
1)DR(Direct Routing 直接路由)模式。 DR可以支援相當多的Real Server,但需要保證Director Server(分發器)虛擬網路卡與物理網路卡在同一網段,並且後端Real Server的vip要建立在本地迴環口lo裝置上,這樣做是為了防止ip衝突;DR模式的好處是進站流量經過Director Server,出站流量不經過Director Server,減輕了Director Server的負載壓力。
2)NAT(Network Address Translation 網路地址轉換)模式。NAT擴充套件性有限,無法支援更多的Real Server,因為所有的請求包和應答包都需要Director Server進行解析再生,這樣就很影響效率。
3)TUN(Tunneling IP隧道)模式。TUN模式能夠支援更多的Real Server,但需要所有伺服器支援IP隧道協議;

LVS負載均衡有10中排程演算法,分別是:rr(輪詢)、wrr(加權輪詢)、lc、wlc、lblc、lblcr、dh、sh、sed、nq

以下詳細記錄了Mysql在主主同步環境下,利用LVS實現Mysql的讀寫負責均衡以及使用Keepalived心跳測試避免節點出現單點故障,實現故障轉移的高可用
1)高可用方案的環境準備

LVS_Master:           182.148.15.237
LVS_Backup:           182.148.15.236
MySQL1 Real Server:   182.148.15.233
MySQL2 Real Server:   182.148.15.238
VIP:                  182.148.15.239
OS:                    CentOS 6.8

溫馨提示:LVS_Master和LVS_Backup充當的是Director Server分發器的角色。
這裡的LVS採用DR模式,即"進站流量經過Director Server分發器,出站流量不經過分發器",這就要求:
1)LVS_Master和LVS_Backup需要將VIP繫結在其正常提供服務的網路卡上(這裡指182.48.115.0網段所在的網路卡),netmask和對於網路卡裝置一致。
2)後端的Real Server要在本地迴環口lo上繫結vip(防止ip衝突)

2)環境部署記錄如下
a)Mysql主主熱備環境部署
MySQL1 Real Server1和MySQL2 Real Server的主主熱備可以參考Mysql+Keepalived雙主熱備高可用操作記錄中對應部分。

b)Keepalived安裝
LVS_Master和LVS_Backup的keepalived安裝,也可以參考Mysql+Keepalived雙主熱備高可用操作記錄中對應部分。

c)LVS安裝

LVS_Master和LVS_Backup兩臺機器安裝步驟一樣
 
首先開啟兩臺機器的ip_forward轉發功能
[root@LVS_Master ~]# echo "1" > /proc/sys/net/ipv4/ip_forward

先下載ipvsadm
[root@LVS_Master ~]# cd /usr/local/src/
[root@LVS_Master src]# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
 
需要安裝以下軟體包
[root@LVS_Master src]# yum install -y libnl* popt*
 
檢視是否載入lvs模組
[root@LVS_Master src]# modprobe -l |grep ipvs
 
解壓安裝
[root@LVS_Master src]# ln -s /usr/src/kernels/2.6.32-431.5.1.el6.x86_64/ /usr/src/linux
[root@LVS_Master src]# tar -zxvf ipvsadm-1.26.tar.gz
[root@LVS_Master src]# cd ipvsadm-1.26
[root@LVS_Master ipvsadm-1.26]# make && make install
 
LVS安裝完成,檢視當前LVS叢集
[root@LVS_Master ipvsadm-1.26]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

d)編寫LVS啟動指令碼/etc/init.d/realserver

1)在MySQL1 Real Server伺服器上的操作
[root@MySQL1 ~]# vim /etc/init.d/realserver
#!/bin/sh
VIP=182.148.15.239
. /etc/rc.d/init.d/functions

case "$1" in
# 禁用本地的ARP請求、繫結本地迴環地址
start)
    /sbin/ifconfig lo down
    /sbin/ifconfig lo up
    echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
    echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
    echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
    echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
    /sbin/sysctl -p >/dev/null 2>&1
    /sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 up #在迴環地址上繫結VIP,設定掩碼,與Direct Server(自身)上的IP保持通訊
    /sbin/route add -host $VIP dev lo:0
    echo "LVS-DR real server starts successfully.\n"
    ;;
stop)
    /sbin/ifconfig lo:0 down
    /sbin/route del $VIP >/dev/null 2>&1
    echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
    echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
    echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
    echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "LVS-DR real server stopped.\n"
    ;;
status)
    isLoOn=`/sbin/ifconfig lo:0 | grep "$VIP"`
    isRoOn=`/bin/netstat -rn | grep "$VIP"`
    if [ "$isLoON" == "" -a "$isRoOn" == "" ]; then
        echo "LVS-DR real server has run yet."
    else
        echo "LVS-DR real server is running."
    fi
    exit 3
    ;;
*)
    echo "Usage: $0 {start|stop|status}"
    exit 1
esac
exit 0

將lvs指令碼加入開機自啟動
[root@MySQL1 ~]# chmod +x /etc/init.d/realserver
[root@MySQL1 ~]# echo "/etc/init.d/realserver" >> /etc/rc.d/rc.local

啟動LVS指令碼
[root@MySQL1 ~]# service realserver start
LVS-DR real server starts successfully.\n

檢視MySQL1 Real Server伺服器,發現VIP已經成功繫結到本地迴環口lo上了
[root@MySQL1 ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 52:54:00:D1:27:75  
          inet addr:182.148.15.233  Bcast:182.148.15.255  Mask:255.255.255.224
          inet6 addr: fe80::5054:ff:fed1:2775/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:598406 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12050 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:78790653 (75.1 MiB)  TX bytes:33151764 (31.6 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:483 errors:0 dropped:0 overruns:0 frame:0
          TX packets:483 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:55807 (54.4 KiB)  TX bytes:55807 (54.4 KiB)

lo:0      Link encap:Local Loopback  
          inet addr:182.148.15.239  Mask:255.255.255.255
          UP LOOPBACK RUNNING  MTU:65536  Metric:1


2)在MySQL2 Real Server伺服器上的操作
[root@MySQL2 ~]# vim /etc/init.d/realserver       //這個指令碼在後端Real Server上都是一樣的內容
#!/bin/sh
VIP=182.148.15.239
. /etc/rc.d/init.d/functions

case "$1" in

start)
    /sbin/ifconfig lo down
    /sbin/ifconfig lo up
    echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
    echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
    echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
    echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
    /sbin/sysctl -p >/dev/null 2>&1
    /sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 up 
    /sbin/route add -host $VIP dev lo:0
    echo "LVS-DR real server starts successfully.\n"
    ;;
stop)
    /sbin/ifconfig lo:0 down
    /sbin/route del $VIP >/dev/null 2>&1
    echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
    echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
    echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
    echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "LVS-DR real server stopped.\n"
    ;;
status)
    isLoOn=`/sbin/ifconfig lo:0 | grep "$VIP"`
    isRoOn=`/bin/netstat -rn | grep "$VIP"`
    if [ "$isLoON" == "" -a "$isRoOn" == "" ]; then
        echo "LVS-DR real server has run yet."
    else
        echo "LVS-DR real server is running."
    fi
    exit 3
    ;;
*)
    echo "Usage: $0 {start|stop|status}"
    exit 1
esac
exit 0


將lvs指令碼加入開機自啟動並啟動lvs指令碼
[root@MySQL2 ~]# chmod +x /etc/init.d/realserver
[root@MySQL2 ~]# echo "/etc/init.d/realserver" >> /etc/rc.d/rc.local
[root@MySQL2 ~]# service realserver start
LVS-DR real server starts successfully.\n


[root@MySQL2 ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 52:54:00:3B:33:8F  
          inet addr:182.148.15.238  Bcast:182.148.15.255  Mask:255.255.255.224
          inet6 addr: fe80::5054:ff:fe3b:338f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:135305 errors:0 dropped:0 overruns:0 frame:0
          TX packets:11256 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:17338566 (16.5 MiB)  TX bytes:892363 (871.4 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

lo:0      Link encap:Local Loopback  
          inet addr:182.148.15.239  Mask:255.255.255.255
          UP LOOPBACK RUNNING  MTU:65536  Metric:1

e)配置iptables防火牆

1)後端兩臺機器MySQL1 Real Server和MySQL2 Real Server要在iptables裡開放3306埠
[root@MySQL1 ~]# vim /etc/sysconfig/iptables
......
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

[root@MySQL1 ~]# /etc/init.d/iptables restart

2)LVS_Master和LVS_Backup兩臺機器要在iptables開放VRRP組播地址的相關規則。
   注意:這個一定要設定!!!否則會出現故障時的VIP資源漂移錯亂問題!

[root@LVS_Master ~]# vim /etc/sysconfig/iptables       //兩臺LVS機器都要設定
.......
-A INPUT -s 182.148.15.0/24 -d 224.0.0.18 -j ACCEPT        //允許組播地址通訊
-A INPUT -s 182.148.15.0/24 -p vrrp -j ACCEPT              //允許VRRP(虛擬路由器冗餘協)通訊
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT 

[root@LVS_Master ~]# /etc/init.d/iptables restart

3)接著配置LVS+Keepalived配置

1)LVS_Master上的操作
[root@LVS_Master ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id LVS_Master
}

vrrp_instance VI_1 {
    state MASTER               #指定instance初始狀態,實際根據優先順序決定.backup節點不一樣
    interface eth0             #虛擬IP所在網
    virtual_router_id 51       #VRID,相同VRID為一個組,決定多播MAC地址
    priority 100               #優先順序,另一臺改為90.backup節點不一樣
    advert_int 1               #檢查間隔
    authentication {
        auth_type PASS         #認證方式,可以是pass或ha
        auth_pass 1111         #認證密碼
    }
    virtual_ipaddress {
        182.148.15.239         #VIP
    }
}

virtual_server 182.148.15.239 3306 {
    delay_loop 6               #服務輪詢的時間間隔
    lb_algo wrr                #加權輪詢排程,LVS排程演算法 rr|wrr|lc|wlc|lblc|sh|sh
    lb_kind DR                 #LVS叢集模式 NAT|DR|TUN,其中DR模式要求負載均衡器網路卡必須有一塊與物理網路卡在同一個網段
    #nat_mask 255.255.255.0
    persistence_timeout 50     #會話保持時間
    protocol TCP              #健康檢查協議

    ## Real Server設定,3306就是MySQL連線埠
    real_server 182.148.15.233 3306 {
        weight 3  ##權重
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
    real_server 182.148.15.238 3306 {
        weight 3
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}

啟動keepalived
[root@LVS_Master ~]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]

注意此時網路卡的變化,可以看到虛擬網路卡已經分配到了realserver上。
此時檢視LVS叢集狀態,可以看到叢集下有兩個Real Server,排程演算法,權重等資訊。ActiveConn代表當前Real Server的活躍連線數。
[root@LVS_Master ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  182.148.15.239:3306 wrr persistent 50
  -> 182.148.15.233:3306          Route   3      1          0         
  -> 182.148.15.238:3306          Route   3      0          0  

2)LVS_Backup上的操作
[root@LVS_Backup ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id LVS_Backup
}

vrrp_instance VI_1 {
    state BACKUP              
    interface eth0             
    virtual_router_id 51       
    priority 90               
    advert_int 1              
    authentication {
        auth_type PASS         
        auth_pass 1111         
    }
    virtual_ipaddress {
        182.148.15.239         
    }
}

virtual_server 182.148.15.239 3306 {
    delay_loop 6              
    lb_algo wrr               
    lb_kind DR                 

    persistence_timeout 50     
    protocol TCP             

    real_server 182.148.15.233 3306 {
        weight 3  
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
    real_server 182.148.15.238 3306 {
        weight 3
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}


啟動keepalived
[root@LVS_Backup ~]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]

[root@LVS_Backup ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  182.148.15.239:3306 wrr persistent 50
  -> 182.148.15.233:3306          Route   3      0          0         
  -> 182.148.15.238:3306          Route   3      0          0  

此時LVS+Keepalived+MySQL主主複製已經搭建完成。

4)最後進行測試驗證

1)先進行功能性驗證
a)關閉MySQL2 Real Server伺服器上的mysql
[root@MySQL2 ~]# /etc/init.d/mysql stop
Shutting down MySQL.. SUCCESS!
 
在LVS_Master檢視/var/log/messages中關於keepalived日誌,LVS_Master檢測到了MySQL2 Real Server伺服器當機,同時LVS叢集自動剔除了故障節點
[root@LVS_Master ~]# tail -f /var/log/messages
.......
Apr 26 15:22:19 test3-237 Keepalived_healthcheckers[4606]: TCP connection to [182.148.15.238]:3306 failed.
Apr 26 15:22:19 test3-237 Keepalived_vrrp[4608]: Sending gratuitous ARP on eth0 for 182.148.15.239
.......
 
[root@LVS_Master ~]# ipvsadm -ln         //LVS已經將MySQL2 Real Server剔除
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  182.148.15.239:3306 wrr persistent 50
  -> 182.148.15.233:3306          Route   3      1          0
 
從新啟動MySQL2 Real Server後自動將故障節點自動加入LVS叢集
[root@LVS_Master ~]# tail -f /var/log/messages
.......
Apr 26 15:23:49 test3-237 Keepalived_healthcheckers[4606]: TCP connection to [182.148.15.238]:3306 success.
Apr 26 15:23:49 test3-237 Keepalived_healthcheckers[4606]: Adding service [182.148.15.238]:3306 to VS [182.148.15.239]:3306
.......
 
[root@LVS_Master ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  182.148.15.239:3306 wrr persistent 50
  -> 182.148.15.233:3306          Route   3      1          0        
  -> 182.148.15.238:3306          Route   3      1          0
 
 
b)關閉LVS_Master上的Keepalived(模擬當機操作),檢視LVS_Master上的日誌,可以看到Keepalived移出了LVS1上的VIP
[root@LVS_Master ~]# /etc/init.d/keepalived stop
Stopping keepalived:                                       [  OK  ]
 
[root@LVS_Master ~]# tail -f /var/log/messages
........
Apr 26 15:29:38 test3-237 Keepalived[4976]: Stopping
Apr 26 15:29:38 test3-237 Keepalived_vrrp[4979]: VRRP_Instance(VI_1) sent 0 priority
Apr 26 15:29:38 test3-237 Keepalived_vrrp[4979]: VRRP_Instance(VI_1) removing protocol VIPs.
Apr 26 15:29:38 test3-237 Keepalived_healthcheckers[4977]: Removing service [182.148.15.233]:3306 from VS [182.148.15.239]:3306
Apr 26 15:29:38 test3-237 Keepalived_healthcheckers[4977]: Removing service [182.148.15.238]:3306 from VS [182.148.15.239]:3306
Apr 26 15:29:38 test3-237 Keepalived_healthcheckers[4977]: Stopped
Apr 26 15:29:38 test3-237 kernel: IPVS: __ip_vs_del_service: enter
Apr 26 15:29:39 test3-237 Keepalived_vrrp[4979]: Stopped
Apr 26 15:29:39 test3-237 Keepalived[4976]: Stopped Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
 
[root@LVS_Master ~]# ip addr    //發現VIP資源已經不在本機了
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:68:dc:b6 brd ff:ff:ff:ff:ff:ff
    inet 182.148.15.237/27 brd 182.148.15.255 scope global eth0
    inet 182.148.15.239/27 brd 182.148.15.255 scope global secondary eth0:0
    inet6 fe80::5054:ff:fe68:dcb6/64 scope link
       valid_lft forever preferred_lft forever
 
 
同時檢視LVS_Backup上日誌,可以看到LVS_Backup成為了Master,並接管了VIP
[root@LVS_Backup ~]# tail -f /var/log/messages
.....
Apr 26 15:26:41 test4-236 Keepalived_vrrp[4711]: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 26 15:26:42 test4-236 Keepalived_vrrp[4711]: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 26 15:26:42 test4-236 Keepalived_vrrp[4711]: VRRP_Instance(VI_1) setting protocol VIPs.
Apr 26 15:26:42 test4-236 Keepalived_vrrp[4711]: Sending gratuitous ARP on eth0 for 182.148.15.239
Apr 26 15:26:42 test4-236 Keepalived_vrrp[4711]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 182.148.15.239
 
[root@LVS_Backup ~]# ip addr         //發現VIP資源已經轉移到LVS_Backup機器上了
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:90:ac:0f brd ff:ff:ff:ff:ff:ff
    inet 182.148.15.236/27 brd 182.148.15.255 scope global eth0
    inet 182.148.15.239/32 scope global eth0
    inet6 fe80::5054:ff:fe90:ac0f/64 scope link
       valid_lft forever preferred_lft forever
 
在LVS_Backup上檢視LVS叢集狀態,一切正常。
[root@LVS_Backup ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  182.148.15.239:3306 wrr persistent 50
  -> 182.148.15.233:3306          Route   3      0          0        
  -> 182.148.15.238:3306          Route   3      1          1
 
 
接著恢復LVS_Master上的Keepalived,發現VIP資源又會重新從LVS_Backup上轉移回來。即LVS_Master重新接管服務。
[root@LVS_Master ~]# tail -f /var/log/messages
.......
Apr 26 15:37:14 test3-237 Keepalived_vrrp[5263]: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 26 15:37:15 test3-237 Keepalived_vrrp[5263]: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 26 15:37:15 test3-237 Keepalived_vrrp[5263]: VRRP_Instance(VI_1) setting protocol VIPs.
Apr 26 15:37:15 test3-237 Keepalived_vrrp[5263]: Sending gratuitous ARP on eth0 for 182.148.15.239
Apr 26 15:37:15 test3-237 Keepalived_vrrp[5263]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 182.148.15.239
 
[root@LVS_Master ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:68:dc:b6 brd ff:ff:ff:ff:ff:ff
    inet 182.148.15.237/27 brd 182.148.15.255 scope global eth0
    inet 182.148.15.239/32 scope global eth0
    inet 182.148.15.239/27 brd 182.148.15.255 scope global secondary eth0:0
    inet6 fe80::5054:ff:fe68:dcb6/64 scope link
       valid_lft forever preferred_lft forever
 
-----------------------------------------------------------------------------------------
2)接著進行mysql主主熱備的高可用
在MySQL1 Real Server和MySQL2 Real Server兩臺機器的mysql裡授權,使遠端客戶機能正常連線。
 
mysql> grant all on *.* to test@'%' identified by "123456";
Query OK, 0 rows affected (0.03 sec)
 
mysql> flush privileges;
 
經測試發現,對於上面的功能驗證,不管是LVS_Master的keepalived關閉(當機)還是後端MySQL2 Real Serve的mysql關閉,在遠端客戶機上都能正常連線mysql(通過VIP進行連線)(LVS_Master的keepalived關閉時,如若遠端客戶機在連線mysql中,可以斷開重新連線一次即可)
[root@bastion-IDC ~]# mysql -h182.148.15.239 -utest -p123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 856
Server version: 5.6.34-log Source distribution
 
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> select * from huanqiu.haha;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | wangshibo |
|  2 | guohuihui |
| 22 | huihui    |
| 23 | bobo      |
+----+-----------+
4 rows in set (0.00 sec)

總結幾點:
1)MySQL主主複製是叢集的基礎,組成Server Array,其中每個節點作為Real Server。
2)LVS伺服器提供了負載均衡的作用,將使用者請求分發到Real Server,一臺Real Server故障並不會影響整個叢集的。
3)Keepalived搭建主備LVS伺服器,避免了LVS伺服器的單點故障,出現故障時可以自動切換到正常的節點。
4)VRRP虛擬IP地址和介面實際IP必須在同一個網段內,VRRP兩個實際介面不在同一網段,是不能形成主備關係的。因為:

VRRP的形式是組播(多播),這個和廣播基本一個原理,只不過縮小了範圍,讓幾個伺服器能收到,眾所周知廣播必須在2層裡面實現,出3層就出了廣播域了。
VRRP中的ip切換是靠arp欺騙實現的,否則vip從主切到備得斷多少機器,arp欺騙也是沒法在出了3層以外的2個機器直接做的。

相關文章