LVS+keepalived

在A和B两个机器上安装lvs+keepalived

yum -y install lvs keepalived

LVS:

vi /etc/init.d/realserver.sh

用上面命令在A上建立realserver.sh文件,内容见下。

#!/bin/bash
#
# Script to start LVS DR real server.
# description: LVS DR real server
#
.  /etc/rc.d/init.d/functions
VIP=192.168.2.200 #修改你的VIP
host=`/bin/hostname`
case "$1" in
start)
       # Start LVS-DR real server on this machine.
        /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/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
        /sbin/route add -host $VIP dev lo:0
;;
stop)
        # Stop LVS-DR real server loopback device(s).
        /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
;;
status)
        # Status of LVS-DR real server.
        islothere=`/sbin/ifconfig lo:0 | grep $VIP`
        isrothere=`netstat -rn | grep "lo:0" | grep $VIP`
        if [ ! "$islothere" -o ! "isrothere" ];then
            # Either the route or the lo:0 device
            # not found.
            echo "LVS-DR real server Stopped."
        else
            echo "LVS-DR real server Running."
        fi
;;
*)
            # Invalid entry.
            echo "$0: Usage: $0 {start|status|stop}"
            exit 1
;;
esac

启动realserver

/etc/init.d/realserver.sh start

查看状态

/etc/init.d/realserver.sh status

正常会打印如下日志:

LVS-DR real server Running.

查看网卡:会看到绑定的虚拟地址。

lo:0      Link encap:Local Loopback
          inet addr:192.168.2.200  Mask:255.255.255.255
          UP LOOPBACK RUNNING  MTU:65536  Metric:1

同样对B机器做相同操作。

keepalived:

vi /etc/keepalived/keepalived.conf

内容根据自己需求填写,示例:

! Configuration File for keepalived

global_defs {
#   notification_email {
#     acassen@firewall.loc
#     failover@firewall.loc
#     sysadmin@firewall.loc
#   }
#   notification_email_from Alexandre.Cassen@firewall.loc
#   smtp_server 192.168.200.1
#   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.2.200
    }
}

virtual_server 192.168.2.200 80 {
    delay_loop 6   
    lb_algo rr   
    lb_kind DR   
    nat_mask 255.255.255.0   
    #persistence_timeout 50   
    protocol TCP
    real_server 192.168.2.212 80 {
        #配置realaserver
        weight 1   
        HTTP_GET { #监控配置   
            url {   
              path /   
              status_code 200   
            }   
            connect_timeout 2   
            nb_get_retry 3   
            delay_before_retry 1   
        }   
    }   
    real_server 192.168.2.222 80 {
        weight 1   
        HTTP_GET {   
            url {   
              path /   
              status_code 200   
            }   
            connect_timeout 2   
            nb_get_retry 3   
            delay_before_retry 1   
        }   
    }   
}

#virtual_server 192.168.200.100 443 {
#    delay_loop 6
#    lb_algo rr
#    lb_kind NAT
#    nat_mask 255.255.255.0
#    persistence_timeout 50
#    protocol TCP

#    real_server 192.168.201.100 443 {
#        weight 1
#        SSL_GET {
#            url {
#              path /
#              digest ff20ad2481f97b1754ef3e12ecd3a9cc
#            }
#            url {
#              path /mrtg/
#              digest 9b3a0c85a887a256d6939da88aabd8cd
#            }
#            connect_timeout 3
#            nb_get_retry 3
#            delay_before_retry 3
#        }
#    }
#}

#virtual_server 10.10.10.2 1358 {
#    delay_loop 6
#    lb_algo rr 
#    lb_kind NAT
#    persistence_timeout 50
#    protocol TCP

#    sorry_server 192.168.200.200 1358

#    real_server 192.168.200.2 1358 {
#        weight 1
#        HTTP_GET {
#            url { 
#              path /testurl/test.jsp
#              digest 640205b7b0fc66c1ea91c463fac6334d
#            }
#            url { 
#              path /testurl2/test.jsp
#              digest 640205b7b0fc66c1ea91c463fac6334d
#            }
#            url { 
#              path /testurl3/test.jsp
#              digest 640205b7b0fc66c1ea91c463fac6334d
#            }
#            connect_timeout 3
#            nb_get_retry 3
#            delay_before_retry 3
#        }
#    }

#    real_server 192.168.200.3 1358 {
#        weight 1
#        HTTP_GET {
#            url { 
#             path /testurl/test.jsp
#              digest 640205b7b0fc66c1ea91c463fac6334c
#            }
#            url { 
#              path /testurl2/test.jsp
#              digest 640205b7b0fc66c1ea91c463fac6334c
#            }
#            connect_timeout 3
#            nb_get_retry 3
#            delay_before_retry 3
#        }
#    }
#}

#virtual_server 10.10.10.3 1358 {
#    delay_loop 3
#    lb_algo rr 
#    lb_kind NAT
#    nat_mask 255.255.255.0
#    persistence_timeout 50
#    protocol TCP

#    real_server 192.168.200.4 1358 {
#        weight 1
#        HTTP_GET {
#            url { 
#              path /testurl/test.jsp
#              digest 640205b7b0fc66c1ea91c463fac6334d
#            }
#            url { 
#              path /testurl2/test.jsp
#              digest 640205b7b0fc66c1ea91c463fac6334d
#            }
#            url { 
#              path /testurl3/test.jsp
#              digest 640205b7b0fc66c1ea91c463fac6334d
#            }
#            connect_timeout 3
#            nb_get_retry 3
#            delay_before_retry 3
#        }
#    }

#    real_server 192.168.200.5 1358 {
#        weight 1
#        HTTP_GET {
#            url { 
#              path /testurl/test.jsp
#              digest 640205b7b0fc66c1ea91c463fac6334d
#            }
#            url { 
#              path /testurl2/test.jsp
#             digest 640205b7b0fc66c1ea91c463fac6334d
#            }
#            url { 
#              path /testurl3/test.jsp
#              digest 640205b7b0fc66c1ea91c463fac6334d
#            }
#            connect_timeout 3
#            nb_get_retry 3
#            delay_before_retry 3
#        }
#    }
#}

启动服务:

service keepalived start

查看服务状态:

ipvsadm -L -n

image

对B机器也是如此!

原文地址:https://www.cnblogs.com/huiy/p/8328589.html