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 trafficserver)
#perlbal
#pound
#squid
#varnish
以上程序都可以实现服务的向外拓展;haproxy特性
haproxy 当前版本为1.31.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 架构图
http://s1.运维网.com/images/20171213/1513155452982333.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超时时,使用该选项在服务器上关闭会话
#timeoutconnect:haproxy转发到后边upstream server 时等待的时长
#timeoutclient :客户端非活动状态的超时时长
#timeoutserver :haproxy和后边的服务器段保持一个会话,当后台服务器down掉后,haproxy等待的超时时间
#timeout-keep-alive:定义保持连接模式的超时时长
#timeout-check : 建立状态检测时间的超时时间
#maxconn :每一个server最大并发连接数 负载均衡调度方法
格式:balanceroundrobin| static-rr| leastconn | source | uri | uri_param | hdr() | rdp-cookie(name)调度方法解析
#roundrobin :属于加权轮询 (动态)支持服务器活动时修改其权重,服务器下线后重新上线时支持慢启动
#static-rr :属于加权轮询(静态)不支持服务器活动时修改,需要重启服务才能生效
# 老服务器重新上线上时,立刻会收到大批量的请求
#leastconn :支持动态修改权重,慢启动
#source :默认为(静态)方法,hash/ 源ip 取模算法,支持hash-type调整为动态
#uri :默认为(静态)方法,hash/weight 取模算法,支持hash-type来调整
#url-params:默认为(静态)方法,hash/wgith 算法,支持hash-type调整
#hdr ():默认为静态方法, 先对做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 [[]]
# capture request header len
# capture resopense header len
实例:在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_backendwebservers
#serivcehaproxy reload
(3)haproxy服务器测试
#tail /var/log/hawebsrv.log haproxy中的ACL
格式:acl
value: 支持整数或者整数范围
支持字符串
支持正则表达式
支持ip地址和网络地址ACL例子
# acl url_staticpath_beg/static /images/img /css
# acl url_staticpath_end.gif.png.jpg.css .js
# acl host_www hdr_beg(host) -iwww
# aclhost_statichdr_beg(host) -iimg. video. download. ftp.
# use_backend staticifhost_staticor host_www or url_static
# use_backend wwwifhost_www实现访问控制
http-request:7层过滤 (借助于定义好的acl实现)
tcp-request: 4层过滤 (借助于定义好的acl实现)haproxy 动静分离的实现
架构图
http://s1.运维网.com/images/20171213/1513155879458315.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
node1.linux.com
# 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
node2.linux.com
# servicehttpd start 2、安装配置haproxy
# yum install haproxy -y 安装haproxy
# vim /etc/haproxy/haproxy.cfg编辑配置文件自定义一个backend和frontend,注释原有的内容
frontendwebsrv *:80
default_backend webservers
backend webservers
balance roundrobin
server node1192.168.20.11:80check
server node2 192.168.20.12:80 check 3、 客户端测试
http://s1.运维网.com/images/20171213/1513156048924444.jpg
http://s1.运维网.com/images/20171213/1513156059765375.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
# servicehaproxy restart重启服务
(三)日志查看
#tail -f /var/log/haproxy.loghttp://s1.运维网.com/images/20171213/1513156135454100.jpg
接下来让我们来拓展haproxy的功能
5 、调度算法 uri的实现
1)后端服务器 server1 与 server2 同时创建多个页面
server1
#cd /var/www/html/
# for i in {1..10}; do echo "node1.test$i">test$i.html; done
server2 方法同 server1
2)更改haproxy.cfg的配置文件中的调度算法
balance uri
3)客户端测试
# http://172.16.13.2/test1.htmlhttp://s1.运维网.com/images/20171213/1513156232499236.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_backendwebservers
backendwebservers
cookie node insert nocache
balance roundrobin
servernode1192.168.20.11:80 checkcookie node1
servernode2192.168.20.12:80 checkcookie node2
2)客户端测试
#http://172.16.13.2/test1.htmlhttp://s1.运维网.com/images/20171213/1513156319731718.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路径 http://s1.运维网.com/images/20171213/1513156424943502.jpg
8、haproxy动静分离的实现
1)server2 服务器安装php
#yum -y install php php-mysql
提供php动态页面
#vim /var/www/html/index.php
node2.linux.com
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:80check maxconn 30000
backend dynamic
balance roundrobin
servernode2 192.168.20.12:80 check maxconn1000
# service haproxy restart
3) 客户端测试 动静分离 http://s1.运维网.com/images/20171213/1513156520531649.jpg
http://s1.运维网.com/images/20171213/1513156526899607.jpg
如图所示:静态页面由server1服务器显示,动态页面由server2 显示。
页:
[1]