如何在Ubuntu 16.04中设置HAProxy

时间:2020-03-05 15:32:43  来源:igfitidea点击:

HAProxy是针对基于TCP和HTTP的网络应用程序的开源高可用性负载平衡和代理服务工具。
它易于使用,适用于大量并将其无缝集成到现有体系结构中。
除了代理服务的主要功能外,它还提供透明的连接,服务器的卸载,策略的实施,限制连接。
在本文中,我们解释了如何在Ubuntu 16.04中设置haproxy。

1.设置网络

我们将添加三个运行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的假设,前提是我们将访问网络中的代理服务器。

2.配置主机名

由于我们要通过负载均衡器和Web服务器的名称(而不是IPADDRESS)来访问它们,因此我们将设置三台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 2015 14:30:29 GMT
Server: Apache/2.4.18 (Ubuntu)
Last-Modified: Sun, 14 Aug 2015 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

3.安装HAProxy

在安装它之前,请先更新Ubuntu。
以超级用户身份或者使用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

4.配置HAProxy

配置文件位于/etc/haproxy/haproxy.cfg中,有两部分,全局和代理,全局部分设置进程范围的参数,代理部分由默认部分,侦听,前端和后端部分组成。

使用文本编辑器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服务器的运行状况参数。

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

莱斯特康
选择连接数最少的服务器-建议用于更长的会话。
同一后端中的服务器也以循环方式旋转。

来源
这基于源IP的哈希选择要使用的服务器,例如:用户的IP地址。
这是确保用户将连接到同一服务器的一种方法。

启用统计信息(可选)

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

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

5.重新启动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

6.测试HAProxy

在测试之前,请确保可以从load-balancer.local对Web服务器(site1.local,site2.local,site3.local)执行ping操作。

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.local或者http://Server-Public-IP,我们将以循环方式从三台Web服务器获得响应,以确认其运行正常。
每次刷新时,代理服务器都会将请求一一发送到后端Web服务器。

我们还可以访问配置文件最后一部分中配置的统计信息网址,以确认是否已打开端口以进行通信。
只需在端口9000上导航到Public IP或者http://load-balancer.local。

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

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

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

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

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

7. 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

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