HAproxy-1.6.3 安装部署
反向代理优缺点:
haproxy反向代理
高性能的HTTP,TCP反向代理
nginx:优点:
1.web服务器,比较广泛
2.工作7层location设置比较复杂基于HTTP(url,cookies,agent)的负载均衡
3.性能强大,网络依赖小
4.安装配置简单
缺点:
1.健康检查单一,不能基于url
2.负载均衡算法少
3.不能动态管理
4.没有集群upstream的状态页面
haproxy 优点:
1.专门做反向代理负载均衡
2.负载均衡算法丰富=8种
3.性能大于nginx
4.支持动态管理,通过和haproxy的sock进行通信,可以进行管理
5.有比较丰富的dashboard页面
6.比较强大的7层功能
缺点:
1.配置没有nginx简单
----------------------------------------------------------------------------------
1.haproxy源码安装,提前上传haproxy软件
cd /usr/local/src/ tar -zxvf haproxy-1.6.3.tar.gz cd haproxy-1.6.3 make TARGET=linux26 ARCH=x86_64 PREFIX=/usr/local/haproxy-1.6.3 make install PREFIX=/usr/local/haproxy-1.6.3 ln -s /usr/local/haproxy-1.6.3/ /usr/local/haproxy
2.配置启动脚本
cp /usr/local/src/haproxy-1.6.3/examples/haproxy.init /etc/init.d/haproxy chmod +x /etc/init.d/haproxy useradd -r haproxy -s /sbin/nologin
3.配置环境变量
echo 'PATH="/usr/local/haproxy/sbin:$PATH"' >> /etc/profile source /etc/profile
4.haproxy配置文件
mkdir /etc/haproxy mkdir /var/lib/haproxy cd /etc/haproxy/ vim haproxy.cfg
5.启动脚本更改
vim /etc/init.d/haproxy 35 BIN=/usr/sbin/$BASENAME # 替换BIN=/usr/local/haproxy/sbin/$BASENAME
6.配置haproxy日志
[root@localhost haproxy-1.6.3]# vim /etc/rsyslog.conf #17,18行注释取消,#最后增加一行 16 # Provides TCP syslog reception 17 $ModLoad imtcp 18 $InputTCPServerRun 514 local3.* /var/log/haproxy.log [root@localhost haproxy-1.6.3]# /etc/init.d/rsyslog restart [root@localhost haproxy-1.6.3]# /etc/init.d/rsyslog restart
7.haprox.cfg配置文件
# 全局配置,日志,运行安装路径,
global
log 127.0.0.1 local3 info # 日志存储到127.0.0.1,端口是514,
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
#配置haproxy的sock文件,权限是600,等级是admin权限,超时2分钟
stats socket /var/lib/haproxy/haproxy.sock mode 660 level admin
stats timeout 2m
user haproxy
group haproxy
daemon
# 默认配置
defaults
log global
mode http
#option httplog # 访问日志关闭
option dontlognull # 不记录空链接,如监控链接
timeout connect 5000
timeout client 50000
timeout server 50000
timeout check 10000
maxconn 3000
# 状态监控页面
listen haproxy_status
# 绑定地址,每5s自动刷新,隐藏版本,状态访问页面,认证账号,密码,条件满足进入管理界面
bind 172.16.1.14:8888
stats enable
stats refresh 100s
stats hide-version
stats uri /haproxy-status
stats realm "HAProxy/ static"
stats auth admin:admin123
stats admin if TRUE
# 允许的网段,允许,拒绝
#acl allow src 192.168.12.0/24
#tcp-request content accept if allow
#tcp-request content reject
# 1.匹配到www.pinhui001.com域名,跳转到www_backend
frontend ph_web
bind 172.16.1.14:80
acl www hdr_end(host) pinhui001.com
#ACL规则定义的方式有hdr_reg(host)、hdr_dom(host)、hdr_beg(host)、url_sub、url_dir、path_beg、path_end等,-i表示不匹配大小写
acl www hdr_end(host) www.pinhui001.com
use_backend www_backend if www
# 2.匹配到目录static,images及jpg,png结尾的跳转到
frontend ph_static
bind 172.16.1.14:1802
acl url_static path_beg -i /static /images /stylesheets
#acl url_static path_end -i .jpg .gif .png .css .js
acl static_reg url_reg /*.(css|jpg|js|jpeg|gif)$
use_backend static_backend if url_static
# test
frontend test_web
bind 172.16.1.14:8899
acl test hdr_beg(host) -i test.pinhui001.cc
use_backend test_backend if test
backend test_backend
mode http
balance roundrobin
option forwardfor header X-REAL-IP
option httpchk GET /iisstart.htm HTTP/1.1\r\nHost:172.16.1.25:80
server web-node1 172.16.1.25:80 check inter 2000 rise 3 fall 3 weight 1
# 1.
backend www_backend
# 随机,2秒检测,2次成功认为服务可用,3次失败认为服务不可用,权重为1
# option httpchk GET /index.html
balance roundrobin
option forwardfor header X-REAL-IP
server web-node1 172.16.1.25:18201 check inter 2000 rise 3 fall 3 weight 1
server web-node3 192.168.2.16:80 check inter 2000 rise 3 fall 3 weight 1
# 2.
backend static_backend
balance roundrobin
option forwardfor header X-REAL-IP
# cookie中插入srv字串防止登录信息丢失
cookie srv insert nocache
server static01 172.16.1.110:80 check inter 2000 rise 2 fall 3 weight 1
server static02 172.16.1.111:80 check inter 2000 rise 2 fall 3 weight 1
8.动态管理haproxy
# 配置文件全局加入2行 vim /etc/haproxy/haproxy.cfg global stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin stats timeout 2m # 安装socker yum list | grep socat yum install -y socat # 查看支持的命令 [root@ha-node01 haproxy]# echo "help" | socat stdio /var/lib/haproxy/haproxy.sock [root@ha-node01 haproxy]# echo "show info" | socat stdio /var/lib/haproxy/haproxy.sock # 查看状态信息 # 关闭某台主机,开启 cho "disable server test_backend/web-node1" | socat stdio /var/lib/haproxy/haproxy.sock echo "enable server test_backend/web-node1" | socat stdio /var/lib/haproxy/haproxy.sock
9.haproxy性能调优
[root@ha-node01 haproxy]# cat /proc/sys/net/ipv4/ip_local_port_range # 端口范围调大 32768 61000 [root@ha-node01 haproxy]# cat /proc/sys/net/ipv4/tcp_tw_reuse # 设置1 1 [root@ha-node01 haproxy]# cat /proc/sys/net/ipv4/tcp_fin_timeout # 时间调短 30
参考文档:
http://cbonte.github.io/haproxy-dconv/configuration-1.6.html

浙公网安备 33010602011771号