Lvs-DR+Keepalived的解决方案

参考

https://www.cnblogs.com/liaojiafa/p/6087276.html

windows real server设置相关,参考下面文章

http://www.loadbalancer.org/blog/direct-server-return-on-windows-2008-using-loopback-adpter/

四台主机在一个局域网内,LVS_master的ip 为192.168.1.196,lvs-backup:192.168.1.197,两台web服务器的ip为192.168.1.200,192.168.1.201,虚拟IP为192.168.1.198从keepalived中配置文件中可以看出。

实验环境为vmware虚拟出4台linux主机网卡全为桥接模式。系统版本为:CentOS Linux release 7.3.1611 (Core) 

【1】安装ipvsadm和keepalived
 

【2】配置主从LVS服务器

a,开启路由转发功能

b,LVS_master keepalived配置文件

[root@localhost keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_MASTER
   vrrp_skip_check_adv_addr
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

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


virtual_server 192.168.1.198 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
#    persistence_timeout 50
    protocol TCP

    real_server 192.168.1.200 80 {
        weight 1
        TCP_CHECK {

            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }

    real_server 192.168.1.201 80 {
        weight 1
        TCP_CHECK {

            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

c,LVS_backup 配置文件

[root@localhost ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_BACK
   vrrp_skip_check_adv_addr
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.198
        }
}


virtual_server 192.168.1.198 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
#    persistence_timeout 50
    protocol TCP

    real_server 192.168.1.200 80 {
        weight 1
        TCP_CHECK {

            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }

    real_server 192.168.1.201 80 {
        weight 1
        TCP_CHECK {

            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

d,在两台LVS上运行keepalived服务

e,配置两台WEB服务

分别在两台服务器上做如下操作:

    编辑sysctl.conf文件

[root@localhost ~]# cat /etc/sysctl.conf 
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.conf.ens33.arp_ignore = 1
net.ipv4.conf.ens33.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

重启

[root@localhost ~]# sysctl -p
net.ipv4.conf.ens33.arp_ignore = 1
net.ipv4.conf.ens33.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

配置lo接口ip

ifconfig lo 192.168.1.198 netmask 255.255.255.255 broadcast 192.168.1.198 up

增加一条路由

 route add -host 192.168.1.198 dev lo

建议不要忘记在rc.local里面写入增加VIP的添加命令,省去开机后人工配置的麻烦:

[root@localhost ~]# cat /etc/rc.local 
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
ifconfig lo 192.168.1.198 netmask 255.255.255.255 broadcast 192.168.1.198 up
route add -host 192.168.1.198 dev lo

猜你喜欢

转载自blog.csdn.net/tjjingpan/article/details/81205922