Haproxy 基础详解及动静分离配置

haproxy 介绍

 1 工作在ISO 七层 根据http协议(或者工作在ISO四层 根据tcp协议) 提供web服务的负载均衡调度器

负载均衡调度器分类
工作在四层:
#  lvs
工作在七层:
#  nginx   (web,http reverse proxy,cache)
#  haproxy (http reverse proxy,tcp proxy)
#     tcp:  实现MySQL的读写中读的负载均衡
# ats (apache traffic server) # perlbal # pound # squid # varnish 以上程序都可以实现服务的向外拓展;
 

haproxy特性

    haproxy 当前版本为1.3  1.4,下面我们介绍1.4版本的特性

# 客户端侧的长连接(client-side keep-alive)
# TCP加速(TCP speedups)
# 响应池(response buffering)
# RDP协议
# 基于源的粘性(source-based stickiness) # 更好的统计数据接口(a much better stats interfaces) # 更详细的健康状态检测机制(more verbose health checks) # 基于流量的健康评估机制(traffic-based health) # 支持HTTP认证 # 服务器管理命令行接口(server management from the CLI) # 基于ACL的持久性(ACL-based persistence) # 日志分析器
 

官网站点:haproxy.1wt.eu

 

haproxy 架构图

wKioL1Np5h_i9MFbAADhowCMXv8632.jpg

haproxy.cfg 配置文件详解

 安装  
 # yum inistall haproxy -y
 配置文件路径
 /etc/haproxy/haproxy.cfg
 
(1)配置由两部分组成
 #global settings: 对haproxy进程自身属性的设定----------全局设定段
 #proxies: 对代理的设定 -----------------代理设定段
  defaults
  frontend
  backend
  listen
  其中defaults为proxies提供默认属性,frontend接受客户端的请求,backend连接后端的上游服务器(类似于nginx的upstream),listen是特定的frontend与backend的组合
(2)定义一个完整的代理的方式:
  frontend
  backend
  listen
(3)defaults段分析
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
#  option   httpclose:使用短连接
#  option   redispath:使用cookie保持会话,如果后端的server宕机,则使用redispath 重定向另一个路径继续保持会话;
# option http-server-close :当keep-alive超时时,使用该选项在服务器上关闭会话 # timeout connect :haproxy转发到后边upstream server 时等待的时长 # timeout client :客户端非活动状态的超时时长 # timeout server : haproxy和后边的服务器段保持一个会话,当后台服务器down掉后,haproxy等待的超时时间 # timeout-keep-alive:定义保持连接模式的超时时长 # timeout-check : 建立状态检测时间的超时时间 # maxconn :每一个server最大并发连接数
 

负载均衡调度方法

格式:balance  roundrobin| static-rr| leastconn | source | uri | uri_param | hdr(<name>) | rdp-cookie(name)
 
调度方法解析
#roundrobin :属于加权轮询 (动态)  支持服务器活动时修改其权重,服务器下线后重新上线时支持慢启动
#static-rr :  属于加权轮询(静态)不支持服务器活动时修改,需要重启服务才能生效
#             老服务器重新上线上时,立刻会收到大批量的请求
#leastconn :支持动态修改权重,慢启动
#source :默认为(静态)方法,hash/ 源ip 取模算法,支持hash-type调整为动态 #uri :默认为(静态)方法,hash/weight 取模算法,支持hash-type来调整 #url-params:默认为(静态)方法,hash/wgith 算法,支持hash-type调整 #hdr (<name>):默认为静态方法, 先对<name>做hash计算然后 hash/weight 计算,支持hash-type调整
 
调度方法的使用总结
#1、调度众多的MySQL从服务器,用什么调度方法?
   leastconn
#2、调度web图片服务器组,用什么调度方法?
   roundrobin
#3、调度web图片服务器组,用什么调度方法?
   source 或者 cookie
#4、调度web缓存服务器组,用什么调度方法? uri hash-type: map-based (默认的静态的hash表) consistent(动态的一致性hash) ---------在后端的cache服务器上使用,否则会导致服务器的加入或者退出时 服务器群瘫痪
 

haproxy 的工作模式 (使用mode参数)

http :http协议  --------haproxy的价值体现于此
#      对应用层数据做深入分析,因此支持7层的过滤、处理、转换等机制;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
tcp  :haproxy在客户端和upstream server之间建立一个全双工的连接
#      不会对应用层协议做任何检查
#      SSL 、MySQL、SSL等都应该使用此模式;
#      默认模式!
 

指定haproxy日志

# log global : 使用全局配置中定义的日志服务器;
# log <address> <facility> [<level>  [<minlevel>]]
# capture request header <HEADER>  len <LENGTH>
# capture resopense header <HEADER>  len <LENGTH>
实例:在frontend中定义一个日志
(1)编辑rsyslog.conf #vim /etc/rsyslog.conf 在日志服务器上先定义一个日志 local3.* /var/log/hawebsrv.log #service rsyslog restart (2)编辑haproxy.cfg #vim /etc/haproxy/haproxy.cfg frontend websrv log 127.0.0.1 local3 bind *:80 default_backend webservers #serivce haproxy reload (3)haproxy服务器测试 #tail /var/log/hawebsrv.log
 

haproxy中的ACL

格式:acl <aclname>  <criterion> [flags] [oprator] <value> value: 支持整数或者整数范围 支持字符串 支持正则表达式 支持ip地址和网络地址
 
ACL例子
# acl url_static  path_beg  /static /images  /img /css                
# acl url_static  path_end  .gif  .png  .jpg  .css .js
# acl host_www    hdr_beg(host) -i  www         
# acl  host_static  hdr_beg(host) -i  img. video. download. ftp.  
# use_backend static if host_static or host_www or url_static # use_backend www if host_www
 
实现访问控制     
http-request:7层过滤   (借助于定义好的acl实现)
tcp-request: 4层过滤    (借助于定义好的acl实现)
 

haproxy 动静分离的实现

 

 架构图

wKioL1Np89Oik9d1AACXrXOJQ0o808.jpg

 

 1、 环境配置

haproxy服务器配置
 外网网卡
# ifconfig eth0 172.16.13.2/16 up
# route add default gw 172.16.0.1
 内网网卡
# ifconfig eth1 192.168.20.1/24 up

两台上游服务器配置

server1 配置
# ifconfig eth0 192.168.20.11/24
# route add default gw 192.168.20.1 提供页面 # vim /var/www/html/index.html <h1>node1.linux.com</h1> # service httpd start server2配置 # ifconfig eth0 192.168.20.12/24 up # route add default gw 192.168.20.1 提供页面 # vim /var/www/html/index.html <h1>node2.linux.com<h1> # service httpd start
 

2、  安装配置haproxy

# yum install haproxy -y   安装haproxy
# vim /etc/haproxy/haproxy.cfg  编辑配置文件自定义一个backend和frontend,注释原有的内容
frontend  websrv *:80        
  default_backend   webservers
backend   webservers
  balance   roundrobin
  server     node1  192.168.20.11:80  check
  server     node2   192.168.20.12:80   check
 

 3、 客户端测试

wKioL1Np9u2halzkAABm4VsD7kk102.jpg

wKioL1Np9vmQcAb_AABeMbu8jjs173.jpg

此时说明 haproxy服务器将客户的请求以roundrobin算法 反向代理给后端的服务器!

4、启用全局日志功能

(一)编辑rsyslog.conf
#vim  /etc/rsyslog.conf  开启如下行
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
# Provides TCP syslog reception $ModLoad imtcp $InputTCPServerRun 514 添加日志 local2.* /var/log/haproxy.log #service rsyslog restart 重启服务 (二)编辑haproxy.cfg # vim /etc/haproxy/haproxy.cfg 开启如下行 log 127.0.0.1 local2 # service haproxy restart 重启服务 (三)日志查看 #tail -f /var/log/haproxy.log
 

wKioL1Np-FvBb0ZpAAHBwD1gOvw836.jpg

 

接下来让我们来拓展haproxy的功能

 

5 、调度算法 uri的实现

1)后端服务器 server1 与 server2 同时创建多个页面
server1
#cd /var/www/html/
# for i in {1..10}; do echo "<h1>node1.test$i</h1>"  >  test$i.html; done
server2 方法同 server1
2)更改haproxy.cfg的配置文件中的调度算法
  balance uri
3)客户端测试 # http://172.16.13.2/test1.html
 

wKiom1Np-0KQ9ElkAABm9L9x-ZY858.jpg

基于172.16.13.2/test1.html 该uri,haproxy服务器反向代理至后台服务器至同一台服务器server2

 

  6、基于cookie实现会话绑定

1)编辑haproxy.cfg配置文件
#vim  /etc/haproxy/haproxy.cfg  内容如下
frontend websrv
   bind *:80
   default_backend  webservers
backend  webservers
   cookie node insert nocache
   balance roundrobin
   server  node1  192.168.20.11:80 check  cookie node1
   server  node2  192.168.20.12:80 check  cookie node2
2)客户端测试
#http://172.16.13.2/test1.html
 

wKiom1Np_WqxQWcTAAI6ilXivxo518.jpg

上图可见,基于cookie实现了客户端的请求与后端服务器server2的会话绑定。

 

7、haproxy管理界面---stats enable

# vim /etc/haproxy/haproxy.cfg 增加一个listen段,如下所示
listen statspage   
bind *:8009    -------侦听端口   
stats enable   -------开启stats   
stats hide-version -----隐藏版本   
stats auth admin:admin ----登录验证信息   
stats admin if TRUE    ----实现在管理界面上对所有backend服务器管理   
stats uri /admin?stats ----登录的uri路径
 

wKiom1Np_4yADwYaAAedNz5EvyU391.jpg

 

8、haproxy动静分离的实现

1)server2 服务器安装php
#yum -y install php php-mysql
提供php动态页面
#vim /var/www/html/index.php
<h1>node2.linux.com</h1> <?php phpinfo(); ?> 2)重新配置haproxy配置文件 #vim /etc/haproxy/haproxy.cfg 定义frontend 和 backend frontend websrvs bind *:80 acl url_static path_beg -i /static /images /javascript /stylesheets acl url_static path_end -i .jpg .gif .png .css .js .html acl host_static hdr_beg(host) -i img. video. download. ftp. imags. videos. acl url_php path_end -i .php use_backend static if url_static or host_static use_backend dynamic if url_php default_backend dynamic backend static balance roundrobin server node1 192.168.20.11:80 check maxconn 30000 backend dynamic balance roundrobin server node2 192.168.20.12:80 check maxconn 1000 # service haproxy restart 3) 客户端测试 动静分离
 

1.jpg

2.jpg

 如图所示:静态页面由server1服务器显示,动态页面由server2 显示。  

 

原文链接:https://blog.51cto.com/dengaosky/2050317

转载于:https://www.cnblogs.com/soymilk2019/p/11057413.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值