1. 环境说明

操作系统 IP地址 角色说明
CentOS7 dip:172.16.120.101 vip:172.16.1.100 lvs1+keepalived
CentOS7 dip:172.16.120.102 vip:172.16.1.100 lvs2+keepalived
CentOS7 dip:172.16.120.103 vip:172.16.1.100 real server1
CentOS7 dip:172.16.120.104 vip:172.16.1.100 real server2

2. RS配置

ifconfig lo:0 172.16.1.100 netmask 255.255.255.255 up echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce

3. LVS1配置

yum install -y ipvsadm keepalived vim /etc/keepalived/keepalived.conf

global_defs {
   router_id LVS_DEVEL
}
vrrp_instance http {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.1.100/24
    }
}

virtual_server 172.16.1.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.255.0
#persistence_timeout 50
    protocol TCP

    real_server 172.16.120.103 80 {
        weight 1
        TCP_CHECK {
              connect_timeout 3
              connect_port 80
            }
        }
    real_server 172.16.120.104 80 {
        weight 1
        TCP_CHECK {
              connect_timeout 3
              connect_port 80
            }
        }
}

4. LVS2配置

yum install -y ipvsadm keepalived vim /etc/keepalived/keepalived.conf

global_defs {
   router_id LVS_DEVEL
}
vrrp_instance apache {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 10
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.1.100/24
    }
}

virtual_server 172.16.1.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.255.0
#persistence_timeout 50
    protocol TCP

    real_server 172.16.120.103 80 {
        weight 1
        TCP_CHECK {
              connect_timeout 3
              connect_port 80
            }
        }
    real_server 172.16.120.104 80 {
        weight 1
        TCP_CHECK {
              connect_timeout 3
              connect_port 80
            }
        }
}

5. 启动服务

#两台LVS服务器都需要执行 systemctl start keepalived systemctl enable keepalived