如何在Ubuntu 18.04和16.04上安装HAProxy负载平衡器
HAProxy是一个非常快速和可靠的解决方案,高可用性,负载平衡,它支持TCP和基于http的应用程序。
目前,最大化网站的在线运行时间对于高流量网站来说是非常重要的。
这在单服务器设置中是不可能的。
然后,我们需要一些高可用性环境,可以轻松地管理单服务器故障。
这篇文章你在Ubuntu, Debian和LinuxMint上设置HAProxy负载平衡环境。
这将配置一个 4层负载均衡。
它将基于IP地址和端口号平衡负载并将请求传输到不同的2台服务器。
服务器情况
下面是我们的网络服务器。
有3个web服务器使用Apache2运行并监听端口80,还有一个HAProxy服务器。
Web 服务器: Server 1: web1.example.com 192.168.1.101 Server 2: web2.example.com 192.168.1.102 Server 3: web3.example.com 192.168.1.103 HAProxy 服务器: HAProxy: haproxy 192.168.1.12
步骤1 -安装HAProxy
以特权用户的身份SSH到HAProxy服务器,并使用以下命令安装HAProxy。
sudo add-apt-repository ppa:vbernat/haproxy-1.8 sudo apt-get update sudo apt-get install haproxy
步骤2 -配置HAProxy负载平衡
现在编辑默认配置文件 ' /etc/haproxy/haproxy.cfg '和启动配置。
sudo vi /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/
ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256::RSA+AES:RSA+3DES:!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将在192.168.1.12的80端口上监听。
frontend Local_Server
bind 192.168.1.12:80
mode http
default_backend My_Web_Servers
添加后端Web服务器:
按照上面的配置,haproxy现在正在监听端口80。
现在定义HAProxy发送请求的后端web服务器。
backend nodes
mode http
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.1rnHost:localhost
server web1.example.com 192.168.1.101:80
server web2.example.com 192.168.1.102:80
server web3.example.com 192.168.1.103:80
启用Stats(可选)
现在,以启用Haproxy统计,添加以下配置在Haproxy配置文件。
listen stats *:1936
stats enable
stats hide-version
stats refresh 30s
stats show-node
stats auth username:password
stats uri /stats
步骤3 -最终HAProxy配置文件
最终的配置文件如下所示:
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/
ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256::RSA+AES:RSA+3DES:!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 Local_Server
bind 192.168.1.12:80
mode http
default_backend My_Web_Servers
backend My_Web_Servers
mode http
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.1rnHost:localhost
server web1.example.com 192.168.1.101:80
server web2.example.com 192.168.1.102:80
server web3.example.com 192.168.1.103:80
listen stats *:1936
stats enable
stats hide-version
stats refresh 30s
stats show-node
stats auth username:password
stats uri /stats
步骤4 -重启HAProxy
现在你已经对HAProxy服务器做了所有必要的修改。
现在,在使用以下命令重新启动服务之前,验证配置文件。
haproxy -c -f /etc/haproxy/haproxy.cfg
如果以上命令返回的输出作为配置文件是有效的,那么重新启动HAProxy服务
sudo service haproxy restart
步骤5 -验证HAProxy设置
现在我们有了一个完整功能的HAProxy设置。
在每个web服务器节点上,创建一个index.html演示页面来显示服务器的主机名,因此我们可以轻松地区分服务器的web页面。
现在在web浏览器中访问IP 192.168.1.12(如上配置)上的端口80并不停刷新。
我们将看到HAProxy正在一个一个地向后端服务器发送请求(按照轮询算法)。
每次刷新,HAProxy将发送请求一个后端服务器。

