环境

两个调度器dr1、dr2,两台真实机rs1、rs2。两台真实机安装httpd,并编辑主页内容用于验证

vip="192.168.132.250"
dr1="192.168.132.120"
dr2="192.168.132.121"
rs1="192.168.132.131"
rs2="192.168.132.134"

配置

#/bin/bash
vip="192.168.132.250"
dr1="192.168.132.120"
dr2="192.168.132.121"
rs1="192.168.132.131"
rs2="192.168.132.134"

#dr1配置keepalived
yum install -y keepalived
cat > /etc/keepalived/keepalived.conf <<EOF
global_defs {
}

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 {
        $vip/24
    }
}

virtual_server $vip 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    protocol TCP

    real_server $rs1 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server $rs2 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
EOF
systemctl restart keepalived

yum install -y ipvsadm sshpass
sed -i 35c"StrictHostKeyChecking no" /etc/ssh/ssh_config
setenforce 0
systemctl stop firewalld

#dr1复制keepalived文件到dr2
sshpass -p root ssh $dr2 "yum install -y keepalived"
sshpass -p root scp /etc/keepalived/keepalived.conf $dr2:/etc/keepalived/keepalived.conf

#dr2配置keepalived
dr2_cmd="
setenforce 0;
systemctl stop firewalld;
sed -i 's/MASTER/BACKUP/g' /etc/keepalived/keepalived.conf;
sed -i 's/100/90/g' /etc/keepalived/keepalived.conf;
systemctl restart keepalived;
"
sshpass -p root ssh $dr2 "$dr2_cmd"

#rs1,rs2配置lvs
cmd="
setenforce 0;
firewall-cmd --add-port=80/tcp;
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up;
route add -host $vip dev lo:0;
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;
service httpd restart
"
sshpass -p root ssh $rs1 "$cmd"
sshpass -p root ssh $rs2 "$cmd"
01-15 18:56