我使用haproxy来负载平衡多个虚拟机(Web服务)。 每个虚拟机都有多个IP地址。
在haproxy上设置这个最佳实践是什么? 现在我的configuration是虚拟机的每个IP在haproxy上都有一个条目“listen”与相应的两个真实服务器。
我可以问是否可以修改多个“听”,只有两个后端服务器。 因为所有“侦听IP”只被转发到同一个虚拟机。
global chroot /var/lib/haproxy pidfile /var/run/haproxy.pid stats socket /var/run/haproxy.stat mode 666 maxconn 4096 user haproxy group haproxy daemon defaults mode http log global log 127.0.0.1 local0 notice option dontlognull option redispatch timeout connect 10000 # default 10 second time out if a backend is not foun timeout client 300000 timeout server 300000 maxconn 60000 retries 3 listen stats bind xx.xxx.xxx.xxx:8888 stats uri / stats realm Haproxy\ Statistics stats auth user:pass stats refresh 20 listen server 66.xxx.xxx.36:80 mode http balance roundrobin cookie SERVERID insert nocache indirect option http-server-close option forwardfor option httplog server server1 66.xxx.xxx.66:80 cookie sv1 check inter 5s rise 2 fall 5 server server2 66.xxx.xxx.68:80 cookie sv2 check inter 5s rise 2 fall 5 listen app 66.xxx.xxx.36:80 mode http balance roundrobin cookie SERVERID insert nocache indirect option http-server-close option forwardfor option httplog server app1 66.xxx.xxx.66:80 cookie ap1 check inter 5s rise 2 fall 5 server app2 66.xxx.xxx.68:80 cookie ap2 check inter 5s rise 2 fall 5
有两件事你可以做。 首先,您可以在“听”或“前端”部分添加尽可能多的“绑定”行。 所以如果唯一改变的就是IP,那么这是最好的做法。 其次,如果您有任何理由对每个IP应用不同的处理(例如:不同的ACL或重写规则),则应该使用“前端”+“后端”而不是“监听”。 “监听”部分恰恰是一个前端加一个后端,都在同一部分。 通过拥有多个前端,您可以定义每个IP地址所需的处理types,并使它们全部使用相同的后端(使用“default_backend”规则)。 而所有的服务器将只被放置在这个单一的后端,具有相同的cookie名称,相同的LBalgorithm,相同的健康检查等…
希望这可以帮助,威利