首页 > 代码库 > centos搭建keepalived+ipvsadm实现高可用lvs分发

centos搭建keepalived+ipvsadm实现高可用lvs分发

这文档适合非常简单的不需要任何技术完全照着弄的拥有三台以上机器的情况。


虚ip192.168.200.16

keepalived主192.168.98.11

keepalived备192.168.98.98

realserver一192.168.99.131

realserver二192.168.98.11或192.168.98.98(当11存活时候,就是11当rs,死了就98当rs)


1、keepalived主192.168.98.11进行如下安装配置。

yum install -y wget ipvsadm vim

wget http://www.keepalived.org/software/keepalived-1.2.24.tar.gz

tar xf keepalived-1.2.24.tar.gz 

cd keepalived-1.2.24

./configure --prefix=/usr/local/keepalived

make && make install

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

mkdir /etc/keepalived

cd /usr/local/keepalived/etc/keepalived/

ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/

chkconfig --add keepalived

chkconfig keepalived on

vim /etc/keepalived/keepalived.conf 粘贴下面的。

vrrp_instance ha44 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 100

#advert_int广播间隔,这值影响master挂多久切换到backup,我实测设置2或者3的话切换间隔较短

    advert_int 2

    authentication {

        auth_type PASS

        auth_pass 1234

    }

    virtual_ipaddress {

        192.168.200.16

#可以自定义掩码、广播。

#        192.168.200.16/16 brd 192.168.255.255 dev eth0

    }

#可以自定义网关,不过我没这么用过。

#    virtual_routes {

#        192.168.200.0/16 via 192.168.98.1 dev eth0

#    }

}

#如果想用lvs的功能,就添加以下内容,keepalived会自动调用ipvsadm功能。

virtual_server 192.168.200.16 80 {

    delay_loop 3

    lb_algo rr

    lb_kind DR

    persistence_timeout 50

    protocol TCP


    real_server 192.168.98.11 80 {

        weight 20

        TCP_CHECK

        {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

    real_server 192.168.99.131 80 {

        weight 100

        TCP_CHECK

        {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

}

保存退出

/etc/init.d/keepalived start


2、keepalived备192.168.98.98进行如下安装配置。

yum install -y wget ipvsadm vim

wget http://www.keepalived.org/software/keepalived-1.2.24.tar.gz

tar xf keepalived-1.2.24.tar.gz 

cd keepalived-1.2.24

./configure --prefix=/usr/local/keepalived

make && make install

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

mkdir /etc/keepalived

cd /usr/local/keepalived/etc/keepalived/

ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/

chkconfig --add keepalived

vim /etc/keepalived/keepalived.conf 粘贴下面的。

vrrp_instance ha44 {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 20

#advert_int广播间隔,这值影响master挂多久切换到backup,我实测设置2或者3的话切换间隔较短

    advert_int 2

    authentication {

        auth_type PASS

        auth_pass 1234

    }

     virtual_ipaddress {

        192.168.200.16

#可以自定义掩码、广播。

#        192.168.200.16/16 brd 192.168.255.255 dev eth0

    }

#可以自定义网关,不过我没这么用过。

#    virtual_routes {

#        192.168.200.0/16 via 192.168.98.1 dev eth0

#    }

}

#如果想用lvs的功能,就添加以下内容,keepalived会自动调用ipvsadm功能。

virtual_server 192.168.200.16 80 {

    delay_loop 3

    lb_algo rr

    lb_kind DR

    persistence_timeout 50

    protocol TCP


    real_server 192.168.98.98 80 {

        weight 20

        TCP_CHECK

        {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

    real_server 192.168.99.131 80 {

        weight 100

        TCP_CHECK

        {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

}

保存退出

service keepalived start


3、realserver一192.168.99.131的配置。

yum install -y vim

vim /etc/init.d/lvsrs 粘贴如下内容。

#!/bin/sh

#chkconfig:345 61 61

#description:LVSRS

VIP=192.168.200.16


Usage ()

{

echo "Usage:`basename $0` (start|stop)"

exit 1

}


if [ $# -ne 1 ];then

Usage

fi


case $1 in

start)

echo "configuring vip for Real Server"

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 "1" >/proc/sys/net/ipv4/conf/eth0/arp_ignore

echo "2" >/proc/sys/net/ipv4/conf/eth0/arp_announce

/sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 up

/sbin/route add -host $VIP dev lo:0

if [ $? -eq 0 ]

then

sleep 5

echo "Real Server started."

else

echo "error occurred!"

fi

;;

stop)

/sbin/ifconfig lo:0 down

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 "0" >/proc/sys/net/ipv4/conf/eth0/arp_ignore

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

if [ $? -eq 0 ]

then

sleep 2

echo "Real Server has stopped."

else

echo ‘error occurred occurred! Maybe "lo:0" has stopped already.‘

fi

;;

*)

Usage

esac

保存退出

chmod +x /etc/init.d/lvsrs

chkconfig --add lvsrs

service lvsrs start


4、realserver二192.168.98.11或192.168.98.98,这俩由于已经配置了keepalived,由keepalived服务代管ipvsadm,所以它俩不需要再配置啥就已经自然成为了realserver了。


备注:

1、以上配置用于这仨机器都提供http服务的情况。

2、如果多台keepalived服务器需要提供更多服务,就复制添加更多virtual_server并修改端口即可。如果不配置virtual_server部分,keepalived就不代管lvs了,那么同一时刻只有一台机器工作而已,这台出了问题,vip才会飘到下一台上。vip在哪台,就是哪台工作,所以通常,lvs和keepalived是配合使用的,是需要配置virtual_server部分的。

3、若配置多个vrrp_instance,每个instance需要拥有独一无二的virtual_router_id,与其他instance相同是不行的。多台机器启动keepalived服务后,就是以virtual_router_id的值来进行配对儿的。

4、keepalived配置文件中的priority优先级的设置大有玄机!设置的不科学就会事与愿违!建议查看一下相关资料儿了解了以后再设置。 戳这儿就有一篇哦

centos搭建keepalived+ipvsadm实现高可用lvs分发