1. 程式人生 > >LVS+Keepalived+MysqlCluster搭建高可用的Mysql系統(實測可行)

LVS+Keepalived+MysqlCluster搭建高可用的Mysql系統(實測可行)

LVS+Keepalived+MysqlCluster搭建高可用的Mysql系統(實測可行)

環境

  • CentOS7.0
    -mysql-cluster-gpl-7.1.9a-linux-i686-glibc23.tar.gz
  • keepalived-1.4.2.tar.gz

方案

型別 ip
Management node 192.168.1.2
Data node 192.168.1.2
Data node 192.168.1.3
MASTER 192.168.1.11
BACKUP 192.168.1.13
BACKUP 192.168.1.14
SQL node(LVS節點) 192.168.1.3
SQL node(LVS節點) 192.168.1.4

安裝MysqlCluster

在此不再贅述,安裝詳情見我的上一篇文章MysqlCluster安裝實測可行

安裝keepalived

安裝必要的外掛(官網上要求的和一些必備的)
 - yum install curl gcc openssl-devel libnl3-devel net-snmp-devel
 - yum install -y libnfnetlink-devel(解決錯誤:configure: error: libnfnetlink headers missing)
 - yum install kernel-devel ipvsadm -y
安裝
  • 上傳檔案到/etc/local/,解壓
  • tar -zxvf keepalived-1.4.2.tar.gz
  • cd keepalived-1.4.2
  • ./configure –prefix=/usr/local/keepalived(prefix:指定安裝目錄)
  • make
  • make install
    -完成以上步驟 ,安裝就完成了,目錄結構如下:
    這裡寫圖片描述

配置MASTER和BACKUP

  • 在 /etc/keepalived建立keepalived.conf,檔案內容如下:
global_defs {
    router_id LVS_DEVEL
}
vrrp_instance VI_MYSQL_1 {
        state MASTER //(備機:BACKUP)
        interface ens33
        virtual_router_id 55
        priority 150 //(備機:100)
        advert_int 5
        authentication {
                auth_type PASS
                auth_pass 010203
        }
        virtual_ipaddress {
                192.168.1.200
        }
}
virtual_server 192.168.1.200 3306 {
        delay_loop 6
        lb_algo rr
        lb_kind DR
        persistence_timeout 6
        protocol TCP

        real_server 192.168.1.2 3306 {
                weight 1
                TCP_CHECK {
                        connect_timeout 3
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 3306
                }
        }
        real_server 192.168.1.3 3306 {
                weight 1
                TCP_CHECK {
                        connect_timeout 3
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 3306
                }
        }
        real_server 192.168.1.4 3306 {
                weight 1
                TCP_CHECK {
                        connect_timeout 3
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 3306
                }
        }CKUP
}
  • 備機使用scp命令從主機拷貝
/etc/keepalived/keepalived.conf 192.168.1.14:/etc/keepalived/`
  • 移動啟動相關的檔案
cp  /usr/local/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

配置LVS節點

(這一步至關重要,不然VIP無法跟LVS節點建立連線,keealived執行正常,但是client就是連不上VIP)


  • vi /etc/rc.d/init.d/realserver.sh 檔案內容如下:
    SNS_VIP=192.168.1.200設定VIP,如果你的VIP不是該ip,請修改
#!/bin/bash
# description: Config realserver lo and apply noarp
SNS_VIP=192.168.1.200

/etc/rc.d/init.d/functions

case "$1" in
start)
       ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
       /sbin/route add -host $SNS_VIP dev lo: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 lo: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
  • 賦予該執行許可權
1.chmod +x /etc/rc.d/init.d/functions
2.chmod +x /etc/rc.d/init.d/realserver.sh
  • 啟動
/etc/rc.d/init.d/realserver.sh start 
  • ifconfig檢視新增的虛擬ip
    這裡寫圖片描述

啟動keepalived

  • 啟動項在 cd /etc/init.d
  • ./keealived start
  • 檢視日誌檔案
    tail -f /var/log/message
    這裡寫圖片描述
  • ipvsadm
    這裡寫圖片描述

  • 連線VIP
    這裡寫圖片描述

  • ipvsadm -lc
    這裡寫圖片描述

測試

  • 當主機掛了,備機就會變成主機提供服務
  • 當主機恢復後,備機將從主機回到備機狀態
  • keepalived主機會監聽LVS節點,當有SQL(LVS)掛點或上線,主機會第一時間與其建立聯絡或者斷開連線

總結

配置LVS節點至關重要,務必配置。

LVS+keepalived+MysqlCluster實現了mysql的高可用性,高擴充套件。單MysqlCluster是無法完成這個任務的,keepalived實現了負載均衡,從而將高可用體現了出來。