1. 程式人生 > >LVS+Keepalived+Mysql+主備數據庫架構

LVS+Keepalived+Mysql+主備數據庫架構

pos extra top k with 定義 compiler probe http 不一致

這是一個坑...磨了不少時間。見證自己功力有待提升。。。

架構圖

技術分享圖片

數據庫

1.安裝數據庫

這塊不難, 直接引用: mysql安裝

2.數據庫主備

這塊不難, 直接引用: mysql主備

虛擬VIP

重點來了!!

上腳本, 把這個腳本命名後仍進/etc/init.d, 然後授予權限755或者+x

SNS_VIP=192.168.158.110
/etc/rc.d/init.d/functions
case "$1" in
start)
       ifconfig eth0:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
       /sbin/route add -host $SNS_VIP dev eth0:0
       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
       sysctl -p >/dev/null 2>&1
       echo "RealServer Start OK"
       ;;
stop)
       ifconfig eth0:0 down
       route del $SNS_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 "RealServer Stoped"
       ;;
*)
       echo "Usage: $0 {start|stop}"
       exit 1
esac
exit 0

啟動這個腳本後會出現虛擬ip, 啟動方法service realserver start

查看有好幾種方式

第一種: ifconfig

技術分享圖片

第二種: ip addr

技術分享圖片

第三種: ifconfig eth0:0

技術分享圖片

好了, 虛擬ip也有了,記得兩臺mysql是一樣的操作。

LVS

重點!

1.依賴:

yum install -y libnl* popt*

rpm -ivh popt-static-1.13-7.el6.x86_64.rpm

第二個是後面發現缺少, 恰恰yum中也沒有的安裝而獨自下載rpm
包

2.查看是否已加載lvs模塊

modprobe -l |grep ipvs

3.正常的情況下是這樣的:

[root@localhost init.d]# modprobe -l |grep ipvs
kernel/net/netfilter/ipvs/ip_vs.ko
kernel/net/netfilter/ipvs/ip_vs_rr.ko
kernel/net/netfilter/ipvs/ip_vs_wrr.ko
kernel/net/netfilter/ipvs/ip_vs_lc.ko
kernel/net/netfilter/ipvs/ip_vs_wlc.ko
kernel/net/netfilter/ipvs/ip_vs_lblc.ko
kernel/net/netfilter/ipvs/ip_vs_lblcr.ko
kernel/net/netfilter/ipvs/ip_vs_dh.ko
kernel/net/netfilter/ipvs/ip_vs_sh.ko
kernel/net/netfilter/ipvs/ip_vs_sed.ko
kernel/net/netfilter/ipvs/ip_vs_nq.ko
kernel/net/netfilter/ipvs/ip_vs_ftp.ko
[root@localhost init.d]# 

4.安裝正包

tar -zxvf ipvsadm-1.26.tar.gz

make && make install

5.編譯安裝完成後測試:

ipvsadm -L -n
[root@localhost init.d]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

lvs大功告成!

Keepalived

重點!!

1.安裝依賴

yum install -y kernel-devel openssl openssl-devel

2.編譯安裝

新建安裝目錄

mkdir -p /usr/local/keepalived

編譯

./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-358.el6.x86_64/
Keepalived configuration
------------------------
Keepalived version       : 1.2.13
Compiler                 : gcc
Compiler flags           : -g -O2
Extra Lib                : -lssl -lcrypto -lcrypt 
Use IPVS Framework       : Yes
IPVS sync daemon support : Yes
IPVS use libnl           : No
fwmark socket support    : Yes
Use VRRP Framework       : Yes
Use VRRP VMAC            : Yes
SNMP support             : No
SHA1 support             : No
Use Debug flags          : No

註意: 這裏的2.6.32-358.el6.x86_64是您的內核, 不要照抄哦, 可能咋們內核不一致

安裝

make && make install

3.其他操作

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

chkconfig keepalived on

LVS+Keepalived配置

~~負載均衡主機~~

1.新建keepalived配置文件:

vim /etc/keepalived/keepalived.conf

2.keepalived.conf詳細:

! Configuration File for keepalived

global_defs {                           #全局標識模塊
   notification_email {         #定義郵件通知
     [email protected]                       #目標郵箱
   }
   notification_email_from [email protected] #發送郵箱
   smtp_server 127.0.0.1        #發送郵箱的smtp服務器
   smtp_connect_timeout 30      #smtp服務器連接超時時間
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER                        #本實例啟動狀態:MASTER/BACKUP
    interface eth0                      #監控的網絡接口
    virtual_router_id 51        #vrrp實例(同一個組主備服務器設置一樣)
    priority 100                        #優先級高的為master,不能超過255。(BACKUP可設置為50)
    advert_int 1                        #均衡器檢測間隔1秒(服務器設置都一樣)
    authentication {            #驗證類型及密碼(服務器設置都一樣)
        auth_type PASS          #認證方式,PASS或AH
        auth_pass 123456        #認證密碼
    }
    virtual_ipaddress {         #虛擬ip地址virtual_ipaddress,可以定義多個
        192.168.158.110
    }
}

virtual_server 192.168.158.110 3306 {   #定義虛擬服務器,與上面的virtual_server一樣
    delay_loop 3                        #健康檢查時間間隔,6秒
    lb_algo wrr                         #負載均衡調度算法:rr|wrr|lc|wlc|sh|dh|lblc
    lb_kind DR                          #負載均衡轉發規則:NAT|DR|TUN
    nat_mask 255.255.255.0
    #persistence_timeout 50     #回話保持時間50秒,動態服務建議開啟
    protocol TCP                        #轉發協議protocol,一般有tcp和udp兩種

        #後端真實服務器,有幾臺就設置幾個
    real_server 192.168.158.134 3306 {
        weight 1                        #權重越大負載分越大,0表示失效
        SSL_GET {                       #健康檢查方式:HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
    real_server 192.168.158.139 3306 {
        weight 1
        SSL_GET {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
} 

3.附上啟動腳本

#!/bin/sh
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /var/run/keepalived.pid
# config: /etc/keepalived/keepalived.conf
# chkconfig: - 21 79
# description: Start and stop Keepalived

# Source function library
. /etc/rc.d/init.d/functions

# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /etc/sysconfig/keepalived

RETVAL=0

prog="keepalived"

start() {
    echo -n $"Starting $prog: "
    daemon keepalived ${KEEPALIVED_OPTIONS}
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
}

stop() {
    echo -n $"Stopping $prog: "
    killproc keepalived
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
}

reload() {
    echo -n $"Reloading $prog: "
    killproc keepalived -1
    RETVAL=$?
    echo
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    reload)
        reload
        ;;
    restart)
        stop
        start
        ;;
    condrestart)
        if [ -f /var/lock/subsys/$prog ]; then
            stop
            start
        fi
        ;;
    status)
        status keepalived
        RETVAL=$?
        ;;
    *)
        echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"
        RETVAL=1
esac

exit $RETVAL

4.檢測keepalived

service keepalived start

service keepalived stop

service keepalived status

5.檢測lvs

ipvsadm

lvs數據參考:

[root@localhost init.d]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.158.110:mysql wrr
  -> 192.168.158.134:mysql        Route   1      0          0         
  -> 192.168.158.139:mysql        Route   1      0          0         

~~負載均衡備機~~

備機負載服務器與主負載服務器大致相同,只是在keepalived的配置文件中需要改以下兩處:

(1)將state由MASTER改為BACKUP

(2)將priority由100改為99

lvs+keepalived大功告成!!

驗證性測試

1.啟動mysql(兩臺)

service mysqld start

2.啟動vip

service realserver start

3.啟動keepalived(兩臺)

service keepalived start

4.使用本地ping vip

技術分享圖片

5.使用本地連接mysql(連接的是vip)

mysql.exe -u root -p"lizhenghua" -P 3306 -h 192.168.158.110

技術分享圖片

6.在從庫139上建立數據庫cheng(先斷開主從)

192.168.158.139

技術分享圖片

192.168.158.134

技術分享圖片

完成!!

LVS+Keepalived+Mysql+主備數據庫架構