![](https://img.haomeiwen.com/i44480/44ab02983c346cae.png)
HAProxy是什么?
HAProxy (High Availability Proxy)是一个TCP/HTTP
负载均衡器和代理服务器,允许web服务器跨多个端点请求。当单机并发量过大时,负载均衡是非常有用的。客户端将连接到HAProxy实例,而不是连接到处理所有请求的单个服务器,HAProxy实例将使用反向代理根据负载均衡算法将请求转发到一个可用的端点。
本文将描述用于负载均衡HTTP请求的HAProxy的安装和配置,该配置可适用于大多数负载均衡场景。该设置是从典型的生产设置简化而来的,它将使用一个HAProxy节点和两个web服务器节点,这两个web服务器节点将服务于从HAProxy节点转发的请求。
准备工作
-
本指南将尽可能使用
sudo
。完成我们的安全您的服务器指南的部分,创建一个标准的用户帐户,加强SSH访问和删除不必要的网络服务。 -
更新系统:
sudo apt-get update && sudo apt-get upgrade
-
本指南在示例配置中使用私有IP地址。请参考我们的Linux静态IP配置指南,以添加私有IP地址和内部网络节点。
本指南是为非根用户编写的。需要提升特权的命令以sudo作为前缀。如果您不熟悉sudo命令,请参阅用户和组指南。
安装
- Ubuntu 17.04:
sudo apt-get install haproxy
- Fedora 26:
sudo yum install haproxy
初始化配置
- 查看/etc/haproxy/haproxy.cfg,它是在安装过程中自动创建的。这个文件定义了一个没有任何负载均衡的标准设置:
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
# Default SSL material locations
ca-base /etc/ssl/certs
crt-base /etc/ssl/private
# Default ciphers to use on SSL-enabled listening sockets.
# For more information, see ciphers(1SSL). This list is from:
# https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
# An alternative list with additional directives can be obtained from
# https://mozilla.github.io/server-side-tls/ssl-config-generator/?server=haproxy
ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS
ssl-default-bind-options no-sslv3
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
全局部分定义系统级参数,如文件位置和执行HAProxy的用户和组。在大多数情况下,您不需要更改本节中的任何内容。用户haproxy和组haproxy都是在安装期间创建的。
default部分定义了与超时和错误相关的其他日志参数和选项。默认情况下,将记录正常消息和错误消息。
- 如果您希望禁用正常的操作日志,您可以在
option dontlognull
(disable logging of null connections)之后添加:
option dontlog-normal # disable logging of normal, successful connections
- 您还可以选择将错误日志放在单独的日志文件中:
option log-separate-errors
配置负载均衡
使用HAProxy配置负载平衡时,需要定义两种类型的节点:前端和后端。前端是HAProxy侦听连接的节点。后端节点是HAProxy转发请求的节点。第三种节点类型stats节点可用于监视负载均衡器和其他两个节点。
- 在文本编辑器中打开/etc/haproxy/haproxy.cfg,并为前端添加配置:
frontend haproxynode
bind *:80
mode http
default_backend backendnodes
在本文中,将203.0.113.2替换为前端节点的IP地址。192.168.1.3和192.168.1.4将作为后端节点的IP地址。
此配置块指定一个名为haproxynode的前端节点,它绑定到端口80上的所有网络接口。它将侦听HTTP连接(可以将TCP模式用于其他目的),并使用后端backendnode。
- 添加后端配置:
backend backendnodes
balance roundrobin
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
option httpchk HEAD / HTTP/1.1\r\nHost:localhost
server node1 192.168.1.3:8080 check
server node2 192.168.1.4:8080 check
这定义了backendnode并指定了几个配置选项:
- balance设置指定负载平衡策略。在本例中,使用了
[roundrobin]
(轮询调度)策略。这种策略依次使用每台服务器,但允许为每台服务器分配权重:权重较高的服务器使用得更频繁。其他策略包括static-rr
,它类似于roundrobin,但不允许动态调整权重;还有leastconn
,它将把请求转发给连接数最少的服务器。 -
forwardfor
选项确保转发的请求包含实际的客户端IP地址。 - 第一个
http-request
行允许转发的请求包含客户端HTTP请求的端口。第二添加了包含https的协议,前提是ssl_fc
(HAProxy系统变量)为true。如果连接第一次是通过SSL/TLS传输层建立的,则会出现这种情况。 -
Option httpchk
定义了健康检查,用于测试web服务器是否仍然对转发请求有效。如果服务器没有响应定义的请求,那么在它通过测试之前,将不会使用它来进行负载平衡。 -
server
行定义了实际的服务器节点及其IP地址,请求将会转发到这些节点。这里定义的服务器是node1和node2,它们都将使用您定义的健康检查。
- 将可选的stats节点添加到配置中:
listen stats
bind :32700
stats enable
stats uri /
stats hide-version
stats auth someuser:password
HAProxy stats节点将侦听端口32700上的连接,并配置为隐藏HAProxy版本,同时需要密码登录。用更安全的密码替换密码。此外,建议在生产中禁用stats登录
- 以下是修改后的完整配置文件:
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
# Default SSL material locations
ca-base /etc/ssl/certs
crt-base /etc/ssl/private
# Default ciphers to use on SSL-enabled listening sockets.
# For more information, see ciphers(1SSL). This list is from:
# https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
# An alternative list with additional directives can be obtained from
# https://mozilla.github.io/server-side-tls/ssl-config-generator/?server=haproxy
ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS
ssl-default-bind-options no-sslv3
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
frontend haproxynode
bind *:80
mode http
default_backend backendnodes
backend backendnodes
balance roundrobin
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
option httpchk HEAD / HTTP/1.1\r\nHost:localhost
server node1 192.168.1.3:8080 check
server node2 192.168.1.4:8080 check
listen stats
bind :32700
stats enable
stats uri /
stats hide-version
stats auth someuser:password
运行和监控
- 重新启动HAProxy服务,使新配置生效:
sudo service haproxy restart
现在,任何传入到HAProxy节点的IP地址203.0.113.2的请求都将被转发到一个内部网络节点,该节点的IP地址为192.168.1.3或192.168.1.4。这些后端节点将提供HTTP请求。如果在任何时候,这些节点中的任何一个都没有通过健康检查,那么在它们通过测试之前,将不会使用它们来服务任何请求。
为了查看统计数据并监视节点的健康状况,可以在指定端口的web浏览器中导航到前端节点的IP地址或域名,例如http://203.0.113.2:32700。这将显示统计信息,例如将请求转发到特定节点的次数,以及前端节点处理当前和以前会话的次数。
更多信息
有关此主题的更多信息,您可能希望参考以下参考资料。虽然提供这些资料是希望它们会有用,但请注意,我们不能保证对外载材料的准确性或及时性。
本文为翻译文章,原文地址:https://www.linode.com/docs/uptime/loadbalancing/how-to-use-haproxy-for-load-balancing/
参考
网友评论