MySQL主主+Keepalived+LVS高可用(二):單點寫入

jx_yu發表於2014-12-11

方案簡介

本方案採用keepalived中的sorry_server來實現寫入資料庫為單點的需求。本方案實現的功能是當網路有問題、mysql 有問題、 伺服器當機、 keepalived 服務停止後,伺服器能自動跳轉到備用機,當主伺服器服務啟動起來後會自動切換回來。

1)    安裝LVS

~]#rpm -ivh libnl-devel-1.1-14.el6.x86_64.rpm

~]#rpm -ivh popt-devel-1.13-7.el6.x86_64.rpm

~]#rpm -ivh popt-static-1.13-7.el6.x86_64.rpm

~]#wget

~]#tar -zxvf ipvsadm-1.26.tar.gz -C /opt/

~]#cd /opt/ipvsadm-1.26

~]#make && make install

2)    建立LVS指令碼

#Master A和B都執行此操作

~]# cat /etc/init.d/realserver

#!/bin/bash

VIP=192.168.56.111

. /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."                   

    ;;   

stop)   

    /sbin/ifconfig lo:0 down   

    /sbin/route del $VIP >/dev/null 2>&1   

    echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore   

    echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce   

    echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore   

    echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce   

    echo "LVS-DR real server stopped."

    ;;

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 to run yet."

    else

       echo "LVS-DR real server is running."

    fi

    exit 3

    ;;

*)   

    echo "Usage: $0 {start|stop|status}"

    exit 1   

esac                    

exit 0

3)    KeepAlived配置

Master A

~]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

! Configuration File for keepalived

 

global_defs {

   notification_email {

     jixiang.yu@trekiz.com

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id MySQL_HA

}

 

vrrp_instance MySQL-HA{

    state MASTER

    interface eth1

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.56.111

    }

}

 

virtual_server 192.168.56.111 3306{

    delay_loop 3

    lb_algo rr

    lb_kind DR

    persistence_timeout 20

    protocol TCP

    sorry_server 192.168.56.102 3306

    real_server 192.168.56.101 3306{

        weight 3

        TCP_CHECK { 

            connect_timeout 10

            nb_get_retry 3

            delay_before_retry 3

            connect_port 3306

        }

    }

}

Master B

~]# cat /etc/keepalived/keepalived.conf

global_defs {

   notification_email {

     jixiang.yu@trekiz.com

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id MySQL_HA

}

 

vrrp_instance MySQL-HA{

    state BACKUP

    interface eth1

    virtual_router_id 51

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.56.111

    }

}

 

virtual_server 192.168.56.111 3306{

    delay_loop 3

    lb_algo rr

    lb_kind DR

    persistence_timeout 20

    protocol TCP

    sorry_server 192.168.56.102 3306

    real_server 192.168.56.101 3306{

        weight 3

        TCP_CHECK { 

            connect_timeout 10

            nb_get_retry 3

            delay_before_retry 3

            connect_port 3306

        }

    }

}

4)    realserver配置

#A和B一樣

~]# cat /etc/init.d/realserver

#!/bin/bash

VIP=192.168.56.111

. /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."                    

    ;;   

stop)   

    /sbin/ifconfig lo:0 down   

    /sbin/route del $VIP >/dev/null 2>&1   

    echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore   

    echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce   

    echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore   

    echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce   

    echo "LVS-DR real server stopped."

    ;;

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 to run yet."

    else

       echo "LVS-DR real server is running."

    fi

    exit 3

    ;;

*)   

    echo "Usage: $0 {start|stop|status}"

    exit 1   

esac                     

exit 0

5)    啟動realserverKeepAlived服務

#A和B都分別啟動

~]# /etc/init.d/realserver start

LVS-DR real server starts successfully.

~]# /etc/init.d/keepalived start

Starting keepalived:

Master A~]# ipvsadm -ln

~]# ipvsadm -Ln

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  192.168.56.111:3306 rr persistent 20

  -> 192.168.56.101:3306          Local   3      0          0 

Master B~]# ipvsadm -ln

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  192.168.56.111:3306 rr persistent 20

  -> 192.168.56.101:3306          Route   3      0          0

並將 keepalived realserver 的啟動指令碼加入到 rc.local 自啟動中:
echo “/etc/rc.d/init.d/realserver.sh start” >> /etc/rc.local

echo “/etc/rc.d/init.d/keepalived start” >> /etc/rc.local

6)    Failover測試

1.       停止Master A上的mysql, MySQL自動切換到sorry_server,使用如下命令檢視:

~]# ipvsadm -Ln

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  192.168.56.111:3306 rr persistent 20

  -> 192.168.56.102:3306          Route   1      1          0    

2.       停止Master上的keepalivedVIP飄到backupMaster B)上,如:

3.       啟動Master A上的mysql, MySQL服務會切換到Master(101)上

4.       啟動Master A上的keepalived,VIP切換回Master(101)

5.       重啟Master上的mysql,先切換到backup102,再切換回101

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27000195/viewspace-1364714/,如需轉載,請註明出處,否則將追究法律責任。

相關文章