使用Keepalived设置负载平衡HAProxy群集
我们将使用两种主要技术来监视群集成员和群集服务的负载均衡器:Keepalived和HAProxy。
Keepalived使用LVS在主动和被动LVS路由器上执行负载平衡和故障转移任务,而HAProxy为TCP和HTTP应用程序执行负载平衡和高可用性服务。
软件
本文使用的软件:
- CentOS的6
- HAProxy 1.5.4
- Keepalived 1.2.13
网络和IP地址
我们的网络设置如下:
- 10.8.8.0/24-可以访问Internet的LAN。
我们将使用的虚拟机的主机名和角色:
- lvs-hap01-具有keepalived的活动HAProxy路由器,
- lvs-hap02-具有keepalived的备用HAProxy路由器,
- lvs-hap03/lvs-hap04-真实服务器,均运行带有SSL的预配置Apache Web服务器。
有关更多信息,请参见下面的架构。
将两个HAProxy负载均衡器部署为故障转移群集,以保护负载均衡器不中断。
多端口服务和防火墙标记
我们将使用多端口服务(HTTP和HTTPS),因此需要使用防火墙标记将不同但相关的协议捆绑在一起。
在两个路由器lvs-hap01和lvs-hap02上分配防火墙标记:
# iptables -t mangle -A PREROUTING -p tcp -d 10.8.8.40/32 -m multiport --dport 80,443 -j MARK --set-mark 80
其中10.8.8.40是我们的虚拟IP地址。
保存iptables规则,以便它们在重新启动后得以恢复:
# service iptables save
具有直接路由的HAProxy
我们将使用HAProxy进行用户直接路由。
构建使用直接路由的负载均衡器设置可提供更高的性能优势,因为直接路由允许真实服务器直接将数据包处理并路由到发出请求的用户,而不是将所有传出数据包通过LVS路由器传递。
直接路由通过将LVS路由器的工作委派给仅处理传入数据包来减少网络性能问题的可能性。
在lvs-hap01路由器上,允许虚拟冗余路由协议(VRRP)流量并保存防火墙规则:
# iptables -I INPUT -p vrrp -m comment --comment "VRRP" -j ACCEPT # service iptables save
为了使Keepalived服务将网络数据包正确转发到真实服务器,每个路由器节点必须在内核中启用IP转发:
# sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/' /etc/sysctl.conf # sysctl -p
HAProxy中的负载平衡还需要具有绑定到非本地IP地址的能力,这意味着未将其分配给本地系统上的设备。
这允许正在运行的负载平衡器实例绑定到非本地IP以进行故障转移。
# echo "net.ipv4.ip_nonlocal_bind=1" >>/etc/sysctl.conf # sysctl -p
将防火墙和sysctl配置复制到备份lvs-hap02路由器:
# scp /etc/sysconfig/iptables Hyman@theitroad:/etc/sysconfig/ # scp /etc/sysctl.conf Hyman@theitroad:/etc/
HAProxy设定
HAProxy在第7层(应用程序层)上执行负载平衡管理。
部署HAProxy进行基于HTTP的负载平衡是一种常见的用例。
安装
在两个路由器节点lvs-hap01和lvs-hap02上安装软件包:
# yum install -y haproxy
在启动时启用:
# chkconfig haproxy on
配置
我们将在主路由器节点lvs-hap01上配置HAProxy。
编辑之前备份配置文件:
# cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.org
我们的HAProxy'/etc/haproxy/haproxy.cfg'配置如下。
“ 10.8.8.40.pem”文件包含我们的自签名SSL证书以及私钥。
#-------------------------------------------------------------------- # Global settings #-------------------------------------------------------------------- global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4096 user haproxy group haproxy daemon ssl-default-bind-ciphers kEECDH+aRSA+AES:kRSA+AES:+AES256:RC4-SHA:!kEDH:!LOW:!EXP:!MD5:!aNULL:!eNULL #-------------------------------------------------------------------- # Common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block. #-------------------------------------------------------------------- defaults #mode tcp|http mode http log global option httplog option dontlognull option http-server-close # Since HAProxy works in reverse-proxy mode, the servers see its IP address as # their client address. This is sometimes annoying when the client's IP address # is expected in server logs. To solve this problem, the well-known HTTP header # "X-Forwarded-For" Jan be added by HAProxy to all requests sent to the server. # This header contains a value representing the client's IP address. option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 2048 #-------------------------------------------------------------------- # Main frontend which proxys to the backends. #-------------------------------------------------------------------- frontend lb-http bind 10.8.8.40:80 reqadd X-Forwarded-Proto:\ http default_backend http-nodes frontend lb-https bind 10.8.8.40:443 ssl crt /etc/ssl/certs/10.8.8.40.pem reqadd X-Forwarded-Proto:\ https default_backend http-nodes #-------------------------------------------------------------------- # Round robin balancing between the various backends. #-------------------------------------------------------------------- backend http-nodes balance roundrobin server lvs-hap03 10.8.8.43:80 check server lvs-hap04 10.8.8.44:80 check #-------------------------------------------------------------------- # Stats. #-------------------------------------------------------------------- listen stats 10.8.8.40:8080 mode http stats enable stats uri / #stats hide-version stats realm HAProxy\ Statistics stats auth admin:passwd
几种常用的HAProxy负载平衡算法如下:
roundrobin:循环轮流选择服务器。
这是默认算法。
minimumconn:选择连接数最少的服务器,建议用于更长的会话。
同一后端中的服务器也以循环方式旋转。
来源:这根据来源IP的散列来选择要使用的服务器,例如:用户的IP地址。
这是确保用户将连接到同一服务器的一种方法。
启动HAProxy服务:
# service haproxy start
将配置复制到备用lvs-hap02路由器:
# scp /etc/haproxy/haproxy.cfg Hyman@theitroad:/etc/haproxy/
不要忘记在备份节点上启动HAProxy服务。
安装keepalived
keepalived守护程序可在主动和被动LVS路由器上运行。
运行keepalived的两个路由器都使用虚拟冗余路由协议(VRRP)。
主用路由器定期发送VRRP通告报文,如果备用路由器没有收到这些通告报文,则选择新的主用路由器。
Keepalived在第4层(传输层)上执行故障转移,TCP在该层上进行基于连接的数据传输。
当真实服务器无法回复简单的超时TCP连接时,keepalived会检测到该服务器已发生故障,并将其从服务器池中删除。
安装
在两个路由器节点lvs-hap01和lvs-hap02上安装软件包:
# yum install -y keepalived
在启动时启用:
# chkconfig keepalived on
配置
编辑前请备份配置文件。
# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.org
主lvs-hap01节点的配置:
global_defs { notification_email { Hyman@theitroad } #notification_email_from Hyman@theitroad #smtp_server 127.0.0.1 #smtp_connect_timeout 30 } # Script used to check if HAProxy is running vrrp_script check_haproxy { script "killall -0 haproxy" interval 2 weight 2 } vrrp_instance LVS_HAP { #state MASTER|BACKUP state MASTER interface eth0 virtual_router_id 51 # 5 on master, 4 on backup # Note that priority is set in the VRRP router and defines the master priority 5 advert_int 1 authentication { #auth_type PASS|AH auth_type PASS auth_pass changeme } virtual_ipaddress { 10.8.8.40 } track_script { check_haproxy } }
备份lvs-hap02节点的配置:
global_defs { notification_email { #Hyman@theitroad } #notification_email_from Hyman@theitroad #smtp_server 127.0.0.1 #smtp_connect_timeout 30 } # Script used to check if HAProxy is running vrrp_script check_haproxy { script "killall -0 haproxy" interval 2 weight 2 } vrrp_instance LVS_HAP { state SLAVE interface eth0 virtual_router_id 51 # 5 on master, 4 on backup priority 4 advert_int 1 authentication { auth_type PASS auth_pass changeme } virtual_ipaddress { 10.8.8.40 } track_script { check_haproxy } }
启动Keeplived服务:
# /etc/init.d/keepalived start
在这一点上,我们应该看到添加到eth0接口的虚拟IP地址10.8.8.40:
# ip ad sh eth0 2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:ff:41:00 brd ff:ff:ff:ff:ff:ff inet 10.8.8.41/24 brd 10.10.1.255 scope global eth0 inet 10.8.8.40/32 scope global eth0 inet6 fe80::a00:27ff:feff:4100/64 scope link valid_lft forever preferred_lft forever
真实服务器
在每个真实服务器节点(lvs-hap03和lvs-hap04)上,为要用于真实服务器的VIP 10.8.8.40和协议组合运行以下命令:
# iptables -t nat -A PREROUTING -p tcp -d 10.8.8.40 -m multiport --dport 80,443 -j REDIRECT
上面的命令将使真实服务器处理发给VIP和端口的数据包。
确保重新启动后保存并还原了防火墙更改:
# service iptables save