spell 发表于 2019-1-1 09:40:27

centos7.2下安装haproxy1.7

  centos7.2下haproxy1.7的使用与配置
  haproxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。
  1.下载haproxy1.7
http://pkgs.fedoraproject.org/repo/pkgs/haproxy/
  2.安装haproxy
查看内核版本

  uname -r

  解压haproxy,并安装

  tar xf haproxy-1.7.2.tar.gz
cd haproxy-1.7.2
make TARGET=linux2628 PREFIX=/data/haproxy
make install PREFIX=/data/haproxy

  安装成功后,查看版本

  /data/haproxy/sbin/haproxy -v

  复制haproxy文件到/usr/sbin下
因为下面的haproxy.init启动脚本默认会去/usr/sbin下找,当然你也可以修改,不过比较麻烦。

  cp /data/haproxy/sbin/haproxy /usr/sbin/

  复制haproxy脚本,到/etc/init.d下

  cp ./examples/haproxy.init /etc/init.d/haproxy

  赋予权限

  chmod 755 /etc/init.d/haproxy

  创建系统账号

  useradd -r haproxy

  创建配置文件

  mkdir /etc/haproxy
vi /etc/haproxy/haproxy.cfg

  #全局配置
global
#设置日志
log 127.0.0.1 local3 info
chroot /data/haproxy
#用户与用户组
user haproxy
group haproxy
#守护进程启动
daemon
#最大连接数
maxconn 4000
#默认配置
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
#前端配置,http_front名称可自定义
frontend http_front
bind *:80
#haproxy的状态管理页面,通过/haproxy?stats来访问
stats uri /haproxy?stats
default_backend http_back
#后端配置,http_back名称可自定义
backend http_back
#负载均衡方式
#source 根据请求源IP
#static-rr 根据权重
#leastconn 最少连接者先处理
#uri 根据请求的uri
#url_param 根据请求的url参数
#rdp-cookie 据据cookie(name)来锁定并哈希每一次请求
#hdr(name) 根据HTTP请求头来锁定每一次HTTP请求
#roundrobin 轮询方式
balance roundrobin
#设置健康检查页面
option httpchk GET /index.html
#传递客户端真实IP
option forwardfor header X-Forwarded-For

inter 2000 健康检查时间间隔2秒

# rise 3 检测多少次才认为是正常的
# fall 3 失败多少次才认为是不可用的
# weight 30 权重
server node1 192.168.1.222:8080 check inter 2000 rise 3 fall 3 weight 30
server node2 192.168.1.222:8082 check inter 2000 rise 3 fall 3 weight 30
  打开rsyslog配置

  vi /etc/rsyslog.conf

  去掉下面两行前面的#号

  $ModLoad imudp
$UDPServerRun 514

  并添加下面一行

  local3.* /var/log/haproxy.log

  重启rsyslog

  systemctl restart rsyslog

  启动haproxy

  service haproxy start

  3、haproxy的acl规则

frontend http_front
bind *:80
stats uri /haproxy?stats
#创建一个acl,is_http_back2是acl的名称,可自定义,用于判断主机名是否为      www.back2.com
acl is_http_back2 hdr_end(host) www.back2.com
#通过正则判断主机名中是否为bbs.back.com或forum.back.com
acl is_host_bbs hdr_reg(host) -i ^(bbs.back.com|forum.back.com)
#判断ua是否为android
acl is_ua_android hdr_reg(User-Agent) -i android
#判断主机名开头是否为img.或css.或js.
acl is_host_static hdr_beg(host) -i img. css. js.
#判断url路径中是否有/bbs
acl is_path_bbs path_beg -i /bbs
#判断url文件结尾
acl is_php path_end -i .php
#通过正则判断url中结尾以
acl is_static_file url_reg -i /*.(css|jpg|png|jpeg|gif)$
#效果同上
acl is_static_file2 path_end -i .css .jpg .png .jpeg .gif
#如果主机名是www.back2.com那么就使用后端http_back2
use_backend http_back2 if is_http_back2
#默认使用的后端
default_backend http_back
backend http_back
balance roundrobin
option httpchk GET /index.html
option forwardfor header X-Forwarded-For
server node1 192.168.1.222:8080 check inter 2000 rise 3 fall 3 weight 30
backend http_back2
balance roundrobin
option httpchk GET /index.html
option forwardfor header X-Forwarded-For
server node2 192.168.1.222:8082 check inter 2000 rise 3 fall 3 weight 30


cn539 发表于 2019-1-1 10:01:06

学习学习。
页: [1]
查看完整版本: centos7.2下安装haproxy1.7