Appearance
VIP + LVS-DR 案例
1. 环境准备
使用 VMware 搭建四台虚拟机(均采用 Debian 12 系统):
Director1
- IP:
10.10.0.11/24 - VIP
10.10.0.20
- IP:
Director2
- IP:
10.10.0.12/24 - VIP
10.10.0.20
- IP:
Real Server1
- IP:
10.10.0.13/24
- IP:
Real Server2
- IP:
10.10.0.14/24
- IP:
2. 配置步骤
2.1. Director
网卡配置:
Bash$ sudo nano /etc/network/interfacesDirector1:
Bashallow-hotplug ens33 iface ens33 inet static address 10.10.0.11 netmask 255.255.255.0 gateway 10.10.0.1 dns-nameservers 218.85.152.991
2
3
4
5
6Director2:
Bashallow-hotplug ens33 iface ens33 inet static address 10.10.0.12 netmask 255.255.255.0 gateway 10.10.0.1 dns-nameservers 218.85.152.991
2
3
4
5
6
安装
keepalived:Bash$ sudo apt update && sudo apt install keepalived -y配置
/etc/keepalived/keepalived.conf:Bash$ sudo touch /etc/keepalived/keepalived.confBash$ sudo nano /etc/keepalived/keepalived.confDirector1:
Nginxglobal_defs { router_id LVS_DR1 } vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 10.10.0.20 # VIP } } virtual_server 10.10.0.20 80 { # VIP delay_loop 6 lb_algo rr lb_kind DR protocol TCP real_server 10.10.0.13 80 { # Real Server 1 weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 10.10.0.14 80 { # Real Server 2 weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }Note:关于配置文件的详细说明可以参考这篇文章。
Director2:
diffglobal_defs { - router_id LVS_DR1 + router_id LVS_DR2 } vrrp_instance VI_1 { - state MASTER + state BACKUP interface ens33 virtual_router_id 51 - priority 100 + priority 50 advert_int 1 authentication { auth_type PASS ...这里有几个关键点:
global_defs.router_id:因保持每个实例唯一;- 不同实例中相同的
vrrp_instance组:- 名称建议保持一致(非强制);
- 建议只有一个
MASTER,其余为BACKUP; virtual_router_id应保持一致;priority各不相同,值越大优先级越高(通常MASTER的值最大);
- 同一实例中的
vrrp_instance组之间:- 名称应各不相同;
virtual_router_id应各不相同;
启动
keepalived:Bash$ sudo systemctl enable --now keepalived
2.2. Real Server
抑制 VIP 的 ARP 响应:
默认情况下,Linux 的 ARP 行为是 “宽松” 的(
arp_ignore = 0),任何接口收到 ARP 请求,只要目标 IP 在本机上,就会响应。这会导致 Real Server 意外响应 VIP 的 ARP 请求。Bash$ sudo tee -a /etc/sysctl.conf <<EOF net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 EOF通过上述配置:
net.ipv4.conf.all.arp_ignore = 1:所有接口只会对与自身绑定的 IP 地址响应 ARP 请求;net.ipv4.conf.all.arp_announce = 2:所有接口发送 ARP 请求时都会强制使用发送接口的地址;
Bash$ sudo sysctl -p网卡配置:
Bash$ sudo nano /etc/network/interfaces将 VIP 绑定到 Real Server 的回环接口上:
Bashallow-hotplug ens33 iface ens33 inet static address 10.10.0.13 netmask 255.255.255.0 gateway 10.10.0.1 dns-nameservers 218.85.152.99 # Bind the VIP to loopback network auto lo:0 iface lo:0 inet static address 10.10.0.20 # VIP netmask 255.255.255.2551
2
3
4
5
6
7
8
9
10
11
12Note:上述为 Real Server1 的网卡配置,Real Server2 的网卡配置仅需将
10.10.0.13改为10.10.0.14即可。重启网络服务或重启系统:
Bashsudo systemctl restart networking检查配置:
检查 VIP 绑定:
Bash$ ip addr show lo:0检查 ARP 参数:
Bash$ sudo sysctl net.ipv4.conf.all.arp_ignore net.ipv4.conf.all.arp_announce通过 Docker 启动 Nginx 实例:
Bash$ docker run --name nginx -d -p 80:80 -v nginx-html:/usr/share/nginx/html nginx分别修改 Real Server1 和 Real Server2 上的 Nginx 的页面,便于后续测试负载均衡效果:
Bash$ docker volume inspect nginx-htmlBash$ sudo nano /var/lib/docker/volumes/nginx-html/_data/index.html
3. 测试
测试 Real Server ARP 抑制:
在客户端上执行:
Bashsudo arping 10.10.0.20如果 Real Server 正确抑制了 ARP 响应,你应该看不到来自 Real Server 的 ARP 回复。而只有 LVS Director 响应 VIP 的 ARP 请求。
负载均衡测试:
在客户端上使用服务网络中的 VIP 进行多次请求,能够看到每次展示的页面在两个实例之间进行轮询变换:
Bash$ curl 10.10.0.20 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to A nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24高可用,自动故障转移测试:
- Director 高可用:任意停止其中一台 Director;
- Real Server 高可用:任意停止其中一个 Nginx 实例;
均不影响客户端正常访问服务。