如何在 Ubuntu 16.04 中安装配置HAProxy

HAProxy 是一个开源的高可用性负载平衡和代理服务工具,用于基于 TCP 和 HTTP 的网络应用程序。它易于使用,适合大容量网站,并且可以无缝集成到现有架构中。除了代理服务的主要功能之外,它还提供透明连接、服务器卸载、策略执行、限制连接。

在本文中,我们将解释如何在 Ubuntu 16.04 中设置 haproxy。

设置网络

我们将添加三个运行 Apache2 的 Web 服务器和一个 HAPROXY 服务器,它将代理我们网络中三个 Web 服务的所有请求/响应。

下面列出了我们网络中的 Web 服务器详细信息。

Server 1:    site1.local     10.0.1.116
Server 2:    site2.local     10.0.1.117
Server 3:    site3.local     10.0.1.119

HAProxy 服务器:

load-balancer.local      10.0.1.118

我们还将一个公共 IP 附加到 load-balancer.local 以便从公共域访问它(可选)。如果您不想从公共域访问它,那么您可以跳过附加公共 IP,假设您将访问网络中的代理服务器。

配置主机名

由于我们将通过名称(而不是 IPADDRESS)访问负载均衡器和 Web 服务器,因此我们将设置三个 Web 服务器和一个代理服务器的主机名。我们将设置三个 Web 服务器和代理服务器的主机名分别为 site1.local、site2.local、site3.local 和 load-balancer.local。

我们将在 /etc/hosts 和 /etc/hostname 中设置 10.0.1.116 的主机名作为 site1.local

root@site1:~# vi /etc/hosts
127.0.0.1 localhost
10.0.1.116 site1.local

root@site1:~# vi /etc/hostname
site1.local

重启网络

root@site1:~#  service networking restart

安装 apache 并启用它。

root@site1:~# apt-get install apache2

root@site1:~# service apache2 enable

root@site1:~# service apache2 start

为 site1.local 创建索引文件

    root@site1:~# vi /var/www/html/index.html

    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
    <h1> Hi, This is 10.0.1.116 </h1>
    </body>
    </html>

为 http 添加防火墙规则

# ufw allow 80/tcp
# ufw reload

现在检查您是否可以访问site1

root@site1:~# curl -I site1.local
HTTP/1.1 200 OK
Date: Sun, 14 Aug 2016 14:30:29 GMT
Server: Apache/2.4.18 (Ubuntu)
Last-Modified: Sun, 14 Aug 2016 03:50:35 GMT
ETag: "ec-53a0004a26c6d"
Accept-Ranges: bytes
Content-Length: 236
Vary: Accept-Encoding
Content-Type: text/html

或者

    root@site1:~#  curl  site1.local
    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
    <h1> Hi, This is 10.0.1.116 </h1>
    </body>
    </html>

对 site2 ( 10.0.1.117 ) 和 site3 ( 10.0.1.119 ) 重复步骤 2。

在代理服务器中添加所有三个 Web 服务器条目 (IPADDRESS HOSTNAME),除了它自己的主机名作为 load-balancer.local

root@load-balancer:~# vi  /etc/hosts
127.0.0.1 localhost
10.0.1.118 load-balancer.local
10.0.1.116 site1.local
10.0.1.117 site2.local
10.0.1.119 site3.local

设置代理服务器的主机名

root@load-balancer:~# vi /etc/hostname
load-balancer.local

安装HAProxy

在安装之前更新 Ubuntu。以 root 身份执行以下命令或使用 sudo 更新 ubuntu。

root@load-balancer:~# apt-get update

现在更新系统上的软件包。

root@load-balancer:~# apt-get upgrade

通过在终端中执行以下命令来安装它。

root@load-balancer:~# apt-get install haproxy

要将服务启用为守护程序,请编辑 /etc/defaults/haproxy 并添加以下行。
启用=1

root@load-balancer:~# vi /etc/defaults/haproxy

ENABLED=1
# Add extra flags here
#EXTRAOPTS="-de -m 16"

启动服务

root@load-balancer:~# service haproxy start

配置HAProxy

位于 /etc/haproxy/haproxy.cfg 的配置文件有两个部分 global 和 proxies,Global 部分设置进程范围的参数,proxy 部分由 defaults、listen、front-end 和 back-end 部分组成。

使用文本编辑器 vi 编辑配置文件

root@load-balancer:~# vi /etc/haproxy/haproxy.cfg

默认配置文件如下所示。保持全局和默认部分不变。

全局部分

global
log 127.0.0.1 local0
log 127.0.0.1 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:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH
+3DES:RSA+AESGCM: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 10.0.1.118:80
mode http
default_backend My_Web_Servers

添加后端 Web 服务器:

“后端”部分描述了一组服务器,代理将连接到这些服务器以转发传入连接。根据上述配置,负载均衡器现在正在侦听端口 80。现在定义它将发送请求的后端 Web 服务器。

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 site1.local  10.0.1.116:80
server site2.local  10.0.1.117:80
server site3.local  10.0.1.119:80

您可以在上面三行的末尾添加“check”参数来检查您的 Web 服务器的健康参数。

您可以根据需要使用其他平衡算法。

Leastconn
选择连接数最少的服务器——建议用于较长的会话。同一后端的服务器也以循环方式轮换。

Source
这会根据源 IP 的哈希值(即您的用户的 IP 地址)选择要使用的服务器。这是确保用户连接到同一服务器的一种方法。

启用统计信息(可选)

现在,如果您愿意,可以通过在配置文件中添加以下内容来启用统计信息。

listen stats
bind :9000
stats enable
stats hide-version
stats refresh 20s
stats show-node
stats uri /stats

重启HAProxy

由于您已完成代理服务器的所有必要配置,请在使用以下命令重新启动服务之前验证配置文件。

root@load-balancer:~# haproxy -c -f /etc/haproxy/haproxy.cfg

如果上述命令返回输出为“配置文件有效”,则重新启动 HAProxy 服务

root@load-balancer:~# service haproxy restart

现在检查状态 HAProxy 服务器。

root@load-balancer:~#  service haproxy status

测试HAProxy

在测试之前,请确保您可以从 load-balancer.local ping 网络服务器(site1.local、site2.local、site3.local)

root@load-balancer:~# ping site1.local
PING site1.local (10.0.1.116) 56(84) bytes of data.
64 bytes from site1.local (10.0.1.116): icmp_seq=1 ttl=64 time=0.906 ms
64 bytes from site1.local (10.0.1.116): icmp_seq=2 ttl=64 time=0.906 ms
64 bytes from site1.local (10.0.1.116): icmp_seq=3 ttl=64 time=0.648 ms

root@load-balancer:~# ping site2.local
PING site2.local (10.0.1.117) 56(84) bytes of data.
64 bytes from site2.local (10.0.1.117): icmp_seq=1 ttl=64 time=3.02 ms
64 bytes from site2.local (10.0.1.117): icmp_seq=2 ttl=64 time=0.687 ms
64 bytes from site2.local (10.0.1.117): icmp_seq=3 ttl=64 time=0.778 ms

root@load-balancer:~# ping site3.local
PING site3.local (10.0.1.119) 56(84) bytes of data.
64 bytes from site3.local (10.0.1.119): icmp_seq=1 ttl=64 time=0.830 ms
64 bytes from site3.local (10.0.1.119): icmp_seq=2 ttl=64 time=0.631 ms
64 bytes from site3.local (10.0.1.119): icmp_seq=3 ttl=64 time=0.771 ms

使用 CURL 测试 HAPROXY

要测试从终端执行以下脚本,您会发现负载均衡器正在以循环方式向三个 Web 服务器发送请求。

    root@load-balancer:~# while true; do curl http://10.0.1.118; sleep 1; done
    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
    <h1> Hi, This is 10.0.1.119 </h1>
    </body>
    </html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
    <h1> Hi, This is 10.0.1.116 </h1>
    </body>
    </html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
    <h1> Hi, This is 10.0.1.117 </h1>
    </body>
    </html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
    <h1> Hi, This is 10.0.1.119 </h1>
    </body>
    </html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
    <h1> Hi, This is 10.0.1.116 </h1>
    </body>
    </html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
    <h1> Hi, This is 10.0.1.117 </h1>
    </body>
    </html>

    ...............................

    ...............................

    ...............................

在浏览器中测试:

将您的浏览器指向 http://load-balancer.localhttp://Server-Public-IP,您将以循环方式从三个 Web 服务器获得响应,确认它按我们的预期运行。每次刷新时,代理服务器都会一个一个地向后端 Web 服务器发送请求。

您还可以访问配置文件最后部分中配置的 stats url 以确认端口已打开流量。只需导航到公共 IP 或端口 9000 上的 http://load-balancer.local

例如,导航到 http://load-balancer.local:9000/stats

HAPROXY 状态
您还可以使用 HATOP 从终端监控服务器的状态。HATOP 是一个第三方工具,它从负载均衡器创建的套接字文件中提取统计信息。通过从终端执行以下命令来安装 HATOP。

root@load-balancer:~# apt-get install hatop

通常在执行 HATOP 时,必须将 -s 参数与命令 sudo hatop -s /var/run/haproxy.sock 一起使用。为避免在调用 HATOP 时必须输入 -s 参数,您可以在 ~/.bashrc 文件中插入以下行:

导出 unix-socket=/var/run/haproxy.sock

HAProxy 的日志记录

编辑 /etc/rsyslog.d/haproxy.conf 并在其中添加以下行。

local0.* -/var/log/haproxy_0.log
local1.* -/var/log/haproxy_1.log

重启 rsyslog

root@load-balancer:~# service rsyslog restart

现在使用以下命令检查日志

root@load-balancer:~# tail -f /var/log/haproxy*.log

还要在配置文件的全局部分添加“调试”行以进行详细日志记录。

最后

使用 HAProxy,您可以提高 Web 应用程序的性能和可用性。本教程只是对负载平衡的介绍,尽管它能够做的比本教程中描述的要多得多。您可以通过在多个负载均衡器之间设置浮动 IP 来提高高可用性,以防止单个负载均衡器出现故障。

修复sed "command i expects \ followed by text"和“invalid command code”问题
如何在 Ubuntu下安装 Microsoft Edge浏览器
标签:

发表我的评论

电子邮件地址不会被公开。 必填项已用*标注

50 + 70 =

ajax-loader