keepalived实现LVS的高可用

发布时间 2023-10-08 09:45:56作者: 小糊涂90
#环境:centos8 
10.0.0.150 ka1
10.0.0.160 ka2
10.0.0.170 web1
10.0.0.180 web2
10.0.0.190 client

#ka1配置
[root@ka1 ~]#yum install -y httpd keepalived
[root@ka1 ~]#cat /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 ka1
   vrrp_mcast_group4 224.0.100.10
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.0.188/24 dev eth0 label eth0:1
    }
}

virtual_server 10.0.0.188 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP

    sorry_server 127.0.0.1 80

    real_server 10.0.0.170 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 1
            retry 3
            delay_before_retry 1
        }
    }


    real_server 10.0.0.180 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 1
            retry 3
            delay_before_retry 1
        }
    }

}

[root@ka1 ~]#echo sorry server>> /var/www/html/index.html
[root@ka1 ~]#systemctl enable --now httpd keepalived

#ka2配置
[root@ka2 ~]#yum install -y httpd keepalived
[root@ka2 ~]#cat /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 ka2
   vrrp_mcast_group4 224.0.100.10
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.0.188/24 dev eth0 label eth0:1
    }
}

virtual_server 10.0.0.188 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP

    sorry_server 127.0.0.1 80

    real_server 10.0.0.170 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 1
            retry 3
            delay_before_retry 1
        }
    }

    real_server 10.0.0.180 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 1
            retry 3
            delay_before_retry 1
        }
    }

}


[root@ka2 ~]#echo sorry server>> /var/www/html/index.html
[root@ka2 ~]#systemctl enable --now httpd keepalived


#web1配置
[root@web1 ~]#cat lvs_dr_rs.sh
#!/bin/bash

#================================================================
#   Copyright (C) 2022 IEucd Inc. All rights reserved.
#
#   文件名称:lvs_dr_rs.sh
#   创 建 者:TanLiang
#   创建日期:2022年08月25日
#   描    述:This is a test file
#
#================================================================

vip=10.0.0.188
mask='255.255.255.255'
dev=lo:1
rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null
service httpd start &> /dev/null && echo "The httpd Server is Ready!"
echo "<h1>`hostname`</h1>" > /var/www/html/index.html

case $1 in
start)
    echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
    ifconfig $dev $vip netmask $mask #broadcast $vip up
    #route add -host $vip dev $dev
    echo "The RS Server is Ready!"
    ;;
stop)
    ifconfig $dev down
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
    echo "The RS Server is Canceled!"
    ;;
*)
    echo "Usage: $(basename $0) start|stop"
    exit 1
    ;;
esac

[root@web1 ~]#bash lvs_dr_rs.sh start
The httpd Server is Ready!
The RS Server is Ready!
[root@web1 ~]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet 10.0.0.188/32 scope global lo:1
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:90:bc:7e brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.170/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:46:bf:71 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:46:bf:71 brd ff:ff:ff:ff:ff:ff
[root@web1 ~]#curl localhost
<h1>web1.tan.com</h1>


#web2配置
[root@web2 ~]#cat lvs_dr_rs.sh
#!/bin/bash

#================================================================
#   Copyright (C) 2022 IEucd Inc. All rights reserved.
#
#   文件名称:lvs_dr_rs.sh
#   创 建 者:TanLiang
#   创建日期:2022年08月25日
#   描    述:This is a test file
#
#================================================================

vip=10.0.0.188
mask='255.255.255.255'
dev=lo:1
rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null
service httpd start &> /dev/null && echo "The httpd Server is Ready!"
echo "<h1>`hostname`</h1>" > /var/www/html/index.html

case $1 in
start)
    echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
    ifconfig $dev $vip netmask $mask #broadcast $vip up
    #route add -host $vip dev $dev
    echo "The RS Server is Ready!"
    ;;
stop)
    ifconfig $dev down
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
    echo "The RS Server is Canceled!"
    ;;
*)
    echo "Usage: $(basename $0) start|stop"
    exit 1
    ;;
esac

[root@web2 ~]#bash lvs_dr_rs.sh start
The httpd Server is Ready!
The RS Server is Ready!
[root@web2 ~]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet 10.0.0.188/32 scope global lo:1
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:98:bc:23 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.180/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe98:bc23/64 scope link
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:46:bf:71 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:46:bf:71 brd ff:ff:ff:ff:ff:ff
[root@web2 ~]#curl localhost
<h1>web2.tan.com</h1>

#访问验证
[root@redis-node5 ~]#while :; do curl 10.0.0.188; sleep 1; done;
<h1>web1.tan.com</h1>
<h1>web2.tan.com</h1>
<h1>web1.tan.com</h1>
<h1>web2.tan.com</h1>
<h1>web1.tan.com</h1>
<h1>web2.tan.com</h1>
<h1>web1.tan.com</h1>
<h1>web2.tan.com</h1>
<h1>web1.tan.com</h1>
<h1>web2.tan.com</h1>
<h1>web1.tan.com</h1>
<h1>web2.tan.com</h1>
<h1>web1.tan.com</h1>
<h1>web2.tan.com</h1>
<h1>web1.tan.com</h1>

#故障测试
#停止keepalived的master节点,访问正常
[root@ka1 ~]#systemctl stop keepalived.service
[root@client ~]#while :; do curl 10.0.0.188; sleep 1; done;
<h1>web2.tan.com</h1>
<h1>web1.tan.com</h1>
<h1>web2.tan.com</h1>
<h1>web1.tan.com</h1>
<h1>web2.tan.com</h1>
<h1>web1.tan.com</h1>
<h1>web2.tan.com</h1>

#停掉web1后,有三次调度到web1丢失后,恢复正常访问web2
[root@web1 ~]#systemctl stop httpd
[root@client ~]#while :; do curl 10.0.0.188; sleep 1; done;
<h1>web1.tan.com</h1>
<h1>web2.tan.com</h1>
<h1>web1.tan.com</h1>
<h1>web2.tan.com</h1>
curl: (7) Failed to connect to 10.0.0.188 port 80: Connection refused
<h1>web2.tan.com</h1>
curl: (7) Failed to connect to 10.0.0.188 port 80: Connection refused
<h1>web2.tan.com</h1>
curl: (7) Failed to connect to 10.0.0.188 port 80: Connection refused
<h1>web2.tan.com</h1>
curl: (7) Failed to connect to 10.0.0.188 port 80: Connection refused
<h1>web2.tan.com</h1>
<h1>web2.tan.com</h1>
<h1>web2.tan.com</h1>
<h1>web2.tan.com</h1>

#重新启动web1后,恢复轮询
[root@web1 ~]#systemctl start  httpd
[root@client ~]#while :; do curl 10.0.0.188; sleep 1; done;
<h1>web2.tan.com</h1>
<h1>web2.tan.com</h1>
<h1>web2.tan.com</h1>
<h1>web1.tan.com</h1>
<h1>web2.tan.com</h1>
<h1>web1.tan.com</h1>

#依次停掉web1,web2,sorryserver上线
[root@web1 ~]#systemctl stop httpd
[root@web2 ~]#systemctl stop httpd
[root@client ~]#while :; do curl 10.0.0.188; sleep 1; done;
<h1>web2.tan.com</h1>
<h1>web1.tan.com</h1>
<h1>web2.tan.com</h1>
curl: (7) Failed to connect to 10.0.0.188 port 80: Connection refused
<h1>web2.tan.com</h1>
curl: (7) Failed to connect to 10.0.0.188 port 80: Connection refused
<h1>web2.tan.com</h1>
curl: (7) Failed to connect to 10.0.0.188 port 80: Connection refused
curl: (7) Failed to connect to 10.0.0.188 port 80: Connection refused
curl: (7) Failed to connect to 10.0.0.188 port 80: Connection refused
curl: (7) Failed to connect to 10.0.0.188 port 80: Connection refused
curl: (7) Failed to connect to 10.0.0.188 port 80: Connection refused
sorry server
sorry server
sorry server
sorry server
sorry server
sorry server