HAProxy介紹
HAProxy是高性能TCP(第四層)/HTTP(第七層)反向代理負載均衡服務器。(The Reliable, High Performance TCP/HTTP Load Balancer)
HAProxy安裝部署
查看列表
$ yum list | grep haproxy

yum安裝
$ yum -y install haproxy

查看詳細信息
$ rpm -qi haproxy

查看幫助
[root@testHaproxy ~]# haproxy --help
HA-Proxy version 1.5.18 2016/05/10
Copyright 2000-2016 Willy Tarreau <willy@haproxy.org>
Usage : haproxy [-f <cfgfile>]* [ -vdVD ] [ -n <maxconn> ] [ -N <maxpconn> ]
[ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ]
-v displays version ; -vv shows known build options.
-d enters debug mode ; -db only disables background mode.
-dM[<byte>] poisons memory with <byte> (defaults to 0x50)
-V enters verbose mode (disables quiet mode)
-D goes daemon ; -C changes to <dir> before loading files.
-q quiet mode : don't display messages
-c check mode : only check config files and exit
-n sets the maximum total # of connections (2000)
-m limits the usable amount of memory (in MB)
-N sets the default, per-proxy maximum # of connections (2000)
-L set local peer name (default to hostname)
-p writes pids of all children to this file
-de disables epoll() usage even when available
-dp disables poll() usage even when available
-dS disables splice usage (broken on old kernels)
-dG disables getaddrinfo() usage
-dV disables SSL verify on servers side
-sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.
修改配置文件
$ vim /etc/haproxy/haproxy.cfg
指定配置文件
$ haproxy -f /etc/haproxy/haproxy.cfg -c
啟動haproxy
$ service haproxy start
查看狀態
$ service haproxy status

$ /bin/systemctl status haproxy.service

HAProxy配置文件
HAProxy配置文件主要由全局設定和代理設定兩部分組成,包含5個域:global、default、frontend、backend、listen。
global
# 全局配置,定義haproxy進程的工作特性和全局配置
global
log 127.0.0.1 local2
chroot /var/lib/haproxy #chroot運行的路徑
pidfile /var/run/haproxy.pid #haproxy pid的存放位置
maxconn 65536 #最大連接數
nbproc 10
ulimit-n 200000
user haproxy #haproxy的運行用戶
group haproxy #haproxy的運行用戶的所屬組
daemon #守護進程的方式在后台工作
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
全局配置,通常是一些進程級別的配置,與操作系統相關。
default
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http #默認使用的七層協議,也可以是tcp四層協議,如果配置為health,則表示健康檢查,返回ok
log global
option tcplog #詳細記錄tcp日志
option redispatch
option dontlognull #不記錄健康檢查的日志信息
option forwardfor
retries 3 #重試次數為3次,失敗3次以后則表示服務不可用
timeout http-request 5s #http請求超時時間,客戶端建立連接5s但不請求數據的時候,關閉客戶端連接
timeout queue 10s #等待最大時間,表示等待最大時長為10s
timeout connect 10s #連接超時時間,表示客戶端請求轉發至服務器所等待的時長為10s
timeout client 30m #客戶端超時時間,表示客戶端非活躍狀態的時間為30min
timeout server 30m #服務器超時時間,表示客戶端與服務器建立連接后,等待服務器的超時時間為30min
timeout http-keep-alive 10s #持久連接超時時間,表示保持連接的超時時長為10s
timeout check 10s #心跳檢測超時時間,表示健康狀態監測時的超時時間為10s
默認參數配置,主要是涉及的公共配置,在defaults中一次性添加。frontend、backend、listen未配置時,都可以默認defaults中的參數配置。若配置了,會覆蓋。
frontend & backend
frontend test
bind *:8082
default_backend test
option httplog
acl user-core path_beg /test/v1/user/
use_backend user-core_server if user-core
# test
backend test
mode http
balance roundrobin
server node1 10.xxx.xxx.1:7000 check port 7000 inter 5000 rise 5 fall 5
server node2 10.xxx.xxx.2:7000 check port 7000 inter 5000 rise 5 fall 5
# user-core_server
backend user-core_server
mode http
balance roundrobin
server node1 10.xxx.xxx.1:7001 check port 7001 inter 5000 rise 5 fall 5
server node2 10.xxx.xxx.2:7001 check port 7001 inter 5000 rise 5 fall 5 backup
frontend haproxy_statis_front
bind *:8081
mode http
default_backend statis_haproxy
backend statis_haproxy
mode http
balance roundrobin
stats uri /haproxy/stats
stats auth haproxy:zkK_HH@zz
stats refresh 30s
stats show-node
stats show-legends
stats hide-version
frontend可以看作是前端接收請求的部分,內部指定后端;
backend可以看作是后端服務接收請求的部分;
frontend中acl規則詳解
1)語法:acl 自定義的acl名稱 acl方法 -i [匹配的路徑或文件]
其中:
acl:是一個關鍵字,表示定義ACL規則的開始,后面跟自定義acl名稱;-i:忽略大小寫,后面跟匹配的路徑或者文件的正則表達式;
2)語法:use_backend backend實例名稱 if acl規則
use_backend:后面跟backend實例名;if:后面跟acl規則名稱,表示如果滿足acl規則,則請求backend實例;
3)示例:
acl user-core path_beg /test/v1/user/
use_backend user-core_server if user-core
表示符合http://ip:port/test/v1/user會轉發user-core_server實例服務;
listen
listen admin_stats
bind *:8080 #監聽端口
mode http
option httplog
log global
stats enable #統計接口啟用開關
maxconn 10
stats refresh 30s #頁面刷新時長
stats uri /haproxy?stats #haproxy ui訪問后綴
stats realm haproxy #認證時的realm,作為提示用的
stats auth admin:admin #認證用戶名和密碼
stats hide-version #隱藏HAProxy版本號
stats admin if TRUE #管理界面只有認證通過后才能在ui上進行管理
listen是`frontend和backend的組合,haproxy的監控ui可以通過這個進行配置。
haproxy頁面
通過上述的listen配置后,重啟haproxy。
$ service haproxy restart
通過`http://ip:8080/haproxy?stats訪問

輸入認證信息:用戶名:admin,密碼:admin

