Haproxy的应用

多次代理

如上图所示,在 192.168.1.0/24 这个网段的客户端想要访问在 172.20.0.0/20 网段内的服务器,所有的通信又不想暴露在互联网上,因此可以在这两个网段内分别都放一台 Haproxy 服务器,并将两台 Haproxy 直连,10.94.0.7310.94.0.72 互通,然后做两次代理即可。

Haproxy1 应该监听在 192.168.1.105 上以供 192.168.1.0/24 网段内的主机访问,然后指定源地址 10.94.0.73 将请求指向后端服务器 Haproxy2 的 10.94.0.72

Haproxy1 的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
global
daemon
user nobody
group nobody
nbproc 1
maxconn 300000

defaults
maxconn 300000
timeout client 30s
timeout server 30s
timeout connect 8s
mode tcp
source 10.94.0.73

listen http
bind 192.168.1.105:80
server http 10.94.0.72:80

listen mysql
bind 192.168.1.105:3306
server mysql 10.94.0.72:3306

Haproxy2 应该监听在 10.94.0.72 以供 Haproxy1 的 10.94.0.73 来请求,然后指定源地址 172.20.0.254 将 Web 请求指向后端服务器 172.20.15.15:80,将 MySQL 请求指向后端服务器 172.20.11.154:3306

Haproxy2 的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
global
daemon
user nobody
group nobody
nbproc 1
maxconn 300000

defaults
maxconn 300000
timeout client 30s
timeout server 30s
timeout connect 8s
mode tcp
source 172.20.0.254

listen http
bind 10.94.0.72:80
server http 172.20.15.15:80

listen mysql
bind 10.94.0.72:3306
server mysql 172.20.11.154:3306

Web 负载均衡

如图所示,客户端访问 192.168.127.130 时,要把请求分发到 192.168.127.131:80192.168.127.132:80 这两台服务器上。这是一个最简单的负载均衡结构图,实现起来比较简单。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
global
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4096
user nobody
group nobody
daemon

# turn on stats unix socket
stats socket /var/lib/haproxy/stats

defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
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 3000

frontend web_proxy
bind 192.168.127.130:80
default_backend webservers

backend webservers
balance roundrobin
mode http
server web1 192.168.127.131:80 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3
server web2 192.168.127.132:80 maxconn 1024 weight 5 check inter 2000 rise 2 fall 3

配置说明

  • Global settings :这是第一部分的配置,全局配置;对Haproxy进程自身属性的设定
  • proxys:对代理的设定,通常有4部分组成
    • defaults:提供默认配置
    • frontend:前端配置
    • backend:后端配置
    • listen:从某种意义上讲可以认为是将 frontend 和 backend 整合到一块的

ACL 匹配

如图所示,要求
1、如果客户端的 IP 是 192.168.127.110,访问 192.168.127.130 时,要把请求分发到 Web Server3 上,即 192.168.127.133

2、客户端访问 192.168.127.130 时,要把请求分发到 192.168.127.131192.168.127.132192.168.127.133 这三台服务器上。同时还要求客户端每一次访问,都跳转到不同的服务器上。

3、如果客户端访问的不是 192.168.127.130 而是 192.168.127.129 时,要把请求全部分发到 Web Server1 上,即 192.168.127.131

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
maxconn 4096
uid 1005
gid 1005
daemon

defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000

frontend WebLB
bind *:80
acl from_110 src 192.168.127.110
acl to_129 hdr_beg(host) 192.168.127.129
acl to_130 hdr_beg(host) 192.168.127.130
use_backend from_to if from_110 to_130
use_backend 3round if to_130
default_backend backend_default

backend from_to
balance source
server web3 192.168.127.133:80 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

backend 3round
balance roundrobin
server web1 192.168.127.131:80 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3
server web2 192.168.127.132:80 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3
server web3 192.168.127.133:80 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

backend backend_default
server web1 192.168.127.131:80 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

状态监控

在其他的配置完成后,做一个全局配置区段,用来监控 Haproxy 服务器的状态。

1
2
3
4
5
6
7
8
9
10
listen Haproxy_Status                   # 关联前端和后端定义一个完整的代理          
mode http # 设置代理协议
bind 0.0.0.0:1080 # 绑定相应的端口
stats enable # 开启 Haproxy 统计状态
stats refresh 10s # 统计页面自动刷新时间间隔
stats uri /status # 访问的url
stats realm Haproxy\ Statistics # 统计页面认证时提示内容信息
stats auth admin:admin # 设置登录用户和密码
stats admin if TRUE # 如果认证通过,则就可以打开 status
stats hide-version # 隐藏代理服务器版本

配置好之后,重启服务访问 http://192.168.127.130:1080/status 即可看到状态信息。

记录日志

自己记录日志

1
local2.*                       /var/log/haproxy.log

交给日志服务器记录日志

配置 /etc/haproxy/haproxy.cfg

1
log         127.0.0.1 local2

开启 rsyslog 记录 haproxy 日志功能,编辑 /etc/rsyslog.conf 找到如下配置项并去掉配开头的的注释

1
2
3
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

添加如下内容

1
2
# Save haproxy log 
local2.* /var/log/haproxy.log

配置 /etc/sysconfig/rsyslog

1
2
3
4
5
# Options for rsyslogd
# Syslogd options are deprecated since rsyslog v3.
# If you want to use them, switch to compatibility mode 2 by "-c 2"
# See rsyslogd(8) for more details
SYSLOGD_OPTIONS="-r -m 0"

相关解释说明:

  • -r:  打开接受外来日志消息的功能,其监控514 UDP端口;
  • -x: 关闭自动解析对方日志服务器的FQDN信息,这能避免DNS不完整所带来的麻烦;
  • -m: 修改syslog的内部mark消息写入间隔时间(0为关闭),例如240为每隔240分钟写入一次”–MARK–”信息;
  • -h: 默认情况下,syslog不会发送从远端接受过来的消息到其他主机,而使用该选项,则把该开关打开,所有接受到的信息都可根据syslog.conf中定义的@主机转发过去.

重启服务查看日志

1
systemctl restart rsyslog haproxy
有钱任性,请我吃包辣条
0%