url_param,# 根据请求的URl参数'balance url_param' requires an URL parameter name做哈希
hdr(name) # 根据HTTP请求头来锁定每一次HTTP请求
rdp-cookie(name) # 根据据cookie(name)来锁定并哈希每一次TCP请求
1.2 Haproxy 安装
官网:https://github.com/haproxy/haproxy/releases
历史版本:
on 25 Nov 2016
v1.8-dev0 …
0e658fb zip tar.gz
on 25 Nov 2016
v1.7.0 …
e59fcdd zip tar.gz
on 10 Nov 2016
v1.7-dev6 …
d5d890b zip tar.gz
yum install gcc gcc-c++ -y
基于域名的健康检查
option httpchk HEAD /index.html HTTP/1.1\r\nHOST:\www.a.com
option httpchk GET /index.html HTTP/1.1\r\nHOST:\www.a.com
server web1 10.16.0.9:8085 cookie 1 weight 5 check inter 2000 rise 2 fall 3
server web2 10.16.0.10:8085 cookie 2 weight 3 check inter 2000 rise 2 fall 3 backup
backend img.abc.com
mode http
option httpchk /index.php
balance roundrobin
server img01 192.168.137.101:80 check inter 2000 fall 3
server img02 192.168.137.102:80 check inter 2000 fall 3
参数详解:
check port 22可以简写成check 默认取前面端口
# inter 5000 fall 5 每5秒检查一次,总共检查5次 不添加默认为间隔2秒,共3次
# -rise 2 恢复前检查2次OK,加入提供服务
# weight 权重
# maxconn 2048 最大并发量
# cookie 1表示serverid为1,check inter 1500 是检测心跳频率 backup 备用节点,不提供服务,当所有节点宕机才提供服务,一般是生产中某台提供其它服务的服务器能做,应急
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" " forward
1.9.4 Haproxy 高可用参数backup
场景1: 当所有节点都失效时,自动启动1台备机 另1台备机仍然闲置
server web1 10.16.0.9:80 cookie 1 weight 5 check port 80 inter 2000 rise 2 fall 3
server web2 10.16.0.10:80 cookie 2 weight 3 check port 80 inter 2000 rise 2 fall 3
server web3 10.16.0.11:80 cookie 1 weight 5 check port 80 inter 2000 rise 2 fall 3 backup
server web4 10.16.0.12:80 cookie 1 weight 5 check port 80 inter 2000 rise 2 fall 3 backup
场景2:当所有节点失效时,自动启动所有备机,需要添加option allbackup参数生效
option allbackup
server web1 10.16.0.9:80 cookie 1 weight 5 check port 80 inter 2000 rise 2 fall 3
server web2 10.16.0.10:80 cookie 2 weight 3 check port 80 inter 2000 rise 2 fall 3
server web3 10.16.0.11:80 cookie 1 weight 5 check port 80 inter 2000 rise 2 fall 3 backup
server web4 10.16.0.12:80 cookie 1 weight 5 check port 80 inter 2000 rise 2 fall 3 backup
场景3:当1个节点失效时,就自动启动1个备节点
mode http
balance roundrobin
server server2 10.204.3.23:80 check port 80 inter 5000 fall 5
backend nginx_php
mode http
balance roundrobin
server server2 10.204.3.24:80 check port 80 inter 5000 fall 5
【实例讲解】
当用户输入的路径是/images/时,就跳转到后端nginx_img池中定义的服务器
当用户输入的路径是/php/时,就跳转到后端nginx_php池中定义的服务器
当用户输入的不包含上面路径时,默认走nginx_php
【环境搭建】
mode http
balance roundrobin
server server2 10.204.3.23:80 check port 80 inter 5000 fall 5
backend nginx_php
mode http
balance roundrobin
server server2 10.204.3.24:80 check port 80 inter 5000 fall 5
实验环境和上面一样,两台server建立相同的文件,通过识别扩展名自动分配后端节点
1.10.4 基于user_agent客户端做跳转
在日志中,记录中用户的IP和客户端类型,可以在日志中找到对应的客户端类型做匹配
acl iphone_user hdr_sub(user-agent) -i iphone
redirect prefix http://iphone.baidu.com if iphone_user #redirect方式跳转
use_backupend iphone if iphone_user # 跳转到后端backend池
测试实例:
acl img hdr_sub(user-agent) -i QQBrowser
redirect prefix http://www.qq.com if img
acl php hdr_sub(user-agent) -i Chrome
redirect prefix http://www.运维网.com if php
acl windows hdr_sub(user-agent) -i windows
use_backend 3-23 if windows
1.10.5 基于IP和端口控制过滤
acl invalid_src src 0.0.0.0/7 224.0.0.0/3
acl invalid_src src_port 0:1023
acl local_dst hdr(host) -i localhost
block if invalid_src || local_dst
acl invalid_src src 192.168.1.0/24 # 网段
acl invalid_src src_port 0:1024
acl local_dst hdr(host) –i localhost
block if !invalid_src # 拒绝不是来自上面规则的
实例: 针对IP做跳转
acl badguy1 src 10.204.3.17
use_backend 3-24 if badguy1
acl badguy src 10.204.1.245
use_backend 3-23 if badguy
1.11 Haproxy+Keepalived+nginx 架构
1.11.1 系统环境:
系统版本:CentOS6.0 x86_64
HAProxy版本:1.4.21
Keepalived版本:1.2.1
Nginx版本:1.2.2
MASTER_IP:192.168.0.130
BACKUP_IP:192.168.0.131
VIP:192.168.0.133
WEB_1:192.168.0.134
WEB_2:192.168.0.135
1.11.2 HAProxy安装:
haproxy.cfg内容如下:
######### haproxy.cfg ######################
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
maxconn 4096
uid 99
gid 99
daemon
defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
frontend http-in
bind *:80
acl is_www.wugk1.com hdr_end(host) -i wugk1.com
acl is_www.wugk2.com hdr_end(host) -i wugk2.com
use_backend www.wugk1.com if is_www.wugk1.com
use_backend www.wugk2.com if is_www.wugk2.com
default_backend www.wugk1.com
backend www.wugk1.com
balance roundrobin
cookie SERVERID insert nocache indirect
option httpchk HEAD /index.html HTTP/1.0
option httpclose
option forwardfor
server Server1 192.168.33.11:80 cookie Server1
backend www.wugk2.com
balance roundrobin
cookie SERVERID insert nocache indirect
option httpchk HEAD /index.html HTTP/1.0
option httpclose
option forwardfor
server Server1 192.168.33.11:81 cookie Server1
#########################################################
然后启动haproxy,如下执行启动命令:
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
错误提示:
[WARNING] 217/202150 (2857) : Proxy 'chinaapp.sinaapp.com': in multi-process mode, stats will be limited to process assigned to the current request.
会提示如上信息,nbproc进程如果设置为1则不会提示,如果想去掉这个提示可以修改编译文件即可。 在源码配置src/cfgparse.c找到如下行
if (nbproc > 1) {
if (curproxy->uri_auth) {
Warning("Proxy '%s': in multi-process mode, stats will be limited to process assigned to the current request.\n",
Warning("Proxy '%s': in multi-process mode, stats will be limited to the process assigned to the current request.\n",
调整nbproc > 1数值即可。