MySQL+LVS+TUN高可用

LVS-VIP:192.168.159.250
LVS-server:192.168.159.91
mysql-server:192.168.151.150
####系统版本CentOS6.4###
####由于LVS DR模式基于同一网段,MySQL实例基于不同网段,所以选择TUN模式,本例由于是测试环境,所有只有一台realserver。

一、在lvs-server安装ipvsadm和keepalived

1.安装ipvsadm

1.1初始化环境

yum install libnl* libpopt* kernel-devel
###安装popt-static###
wget http://mirrors.163.com/centos/6/os/x86_64/Packages/popt-static-1.13-7.el6.x86_64.rpm
rpm -ivh popt-static-1.13-7.el6.x86_64.rpm
###环境初始化完毕###

1.2创建软连接

ln -s /usr/src/kernels/2.6.32-358.el6.x86_64/ /usr/src/linux

1.3安装ipvsadm

cd /usr/local/src/ipvsadm-1.26
make
make install

1.4测试ipvsadm

ipvsadm -v
#ipvsadm v1.26 2008/5/15 (compiled with popt and IPVS v1.2.1)

安装完成

2.安装keepalived

yum -y install popt-devel openssl openssl-devel

cd /usr/local/src/keepalived-1.2.13

./configure --prefix=/usr/local/src/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-358.el6.x86_64/

make && make install

cp /usr/local/src/keepalived-1.2.13/sbin/keepalived /usr/sbin/

cp /usr/local/src/keepalived-1.2.13/etc/sysconfig/keepalived /etc/sysconfig/

cp /usr/local/src/keepalived-1.2.13/etc/rc.d/init.d/keepalived /etc/init.d/

chkconfig --add keepalived

chkconfig keepalived on

二、整合keepalived和lvs

1.1添加keepalived.conf配置文件

! Configuration File for keepalived

vrrp_instance VIP_91
{
    state MASTER
    interface eth0
    virtual_router_id 91
    priority 100
    advert_int 2 
    smtp_alert

    authentication
    {
        auth_type PASS 
        auth_pass 123456
    }

    virtual_ipaddress
    { 
        192.168.159.250/24
    }


}
virtual_server 192.168.159.250 3358 {
    delay_loop 6
    lb_algo rr
    #lb_kind DR
    lb_kind TUN
    persistence_timeout 60
    protocol TCP
    real_server 192.168.151.150 3358 {
        weight 3
        TCP_CHECK {
          connect_timeout 10
          nb_get_retry 3
          delay_before_retry 3
          connect_port 3358
        }
    }
}

1.2lvs-server绑定vip到tunl0网卡

###vim /usr/locsl/src/ipvsdr.sh###
#!/bin/bash VIP=192.168.159.250 RIP1=192.168.151.150 . /etc/rc.d/init.d/functions case "$1" in start) echo "start LVS of DirectorServer" #Set the Virtual IP Address /sbin/ifconfig tunl0 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev tunl0 #Clear IPVS Table /sbin/ipvsadm -C #Set Lvs /sbin/ipvsadm -A -t $VIP:3358 -s rr /sbin/ipvsadm -a -t $VIP:3358 -r $RIP1:3358 -g #Run Lvs /sbin/ipvsadm ;; stop) echo "close LVS Directorserver" ifconfig tunl0 down /sbin/ipvsadm -C ;; *) echo "Usage: $0 {start|stop}" exit 1 esac
#####执行ipvsdr.sh脚本、启动keepalived#####
sh ipvsdr.sh

service keepalived restart

####测试vip可用性######

[root@xxxxxx keepalived]# ping 192.168.159.250 -c 4
PING 192.168.159.250 (192.168.159.250) 56(84) bytes of data.
64 bytes from 192.168.159.250: icmp_seq=1 ttl=64 time=10.6 ms
64 bytes from 192.168.159.250: icmp_seq=2 ttl=64 time=0.053 ms
64 bytes from 192.168.159.250: icmp_seq=3 ttl=64 time=0.038 ms
64 bytes from 192.168.159.250: icmp_seq=4 ttl=64 time=0.049 ms

--- 192.168.159.250 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 0.038/2.692/10.628/4.581 ms

1.3real-server绑定vip并禁止ARP广播响应

###vim realserver.sh###
#!/bin/bash
VIP=192.168.159.250
case $1 in
    start)
        ifconfig tunl0 up
        /sbin/ifconfig tunl0 $VIP broadcast $VIP netmask 255.255.255.255 up
        /sbin/route add -host $VIP dev tunl0
        echo "1" > /proc/sys/net/ipv4/conf/tunl0/arp_ignore
        echo "2" > /proc/sys/net/ipv4/conf/tunl0/arp_announce
        echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
            echo 0 > /proc/sys/net/ipv4/conf/tunl0/rp_filter
                echo 1 > /proc/sys/net/ipv4/conf/tunl0/forwarding
                echo 1 > /proc/sys/net/ipv4/ip_forward
        sysctl -p >/dev/null 2>&1
        echo "RealServer Start OK"
        ;;
    stop)
        ifconfig tunl0 down
        route del $VIP >/dev/null 2>&1
        echo "0" > /proc/sys/net/ipv4/conf/tunl0/arp_ignore
        echo "0" > /proc/sys/net/ipv4/conf/tunl0/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

最后,测试mysql连接状态,查看lvs调度信息

MySQL+LVS+TUN高可用_第1张图片

[root@xxxxxx keepalived]# 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.159.250:3358 rr persistent 60
  -> 192.168.151.150:3358         Tunnel  3      1          0   

至此搭建完毕。

 

你可能感兴趣的:(MySQL+LVS+TUN高可用)