haproxy

haproxy
haproxy 支持4层,7层负载均衡,反向代理,会话保持,大并发。
LVS 稳定,效率高,四层调度。不支持7层的内容分发或过滤。不支持会话保持。
nginx 支持七层调度,现在也有开发的新的模块来扩展调度相关的功能。在会话保持,内容分发过滤方面比haproxy相比要差
squid varnish nginx(apache) LVS haproxy
下图中haproxy用了两个网段(这里模拟内外网),实际时也可以只用一个网卡(只有内网网卡),公网IP在前端就可以了
--这里尽量用两个内网来做,防止桥接网络IP冲突
客户端(宿主机)172.16.2.9
|
| 172.16.2.8
haproxy
| 192.168.122.8
|
|
web1 web2
192.168.122.11 192.168.122.12
实验前准备: (centos7.3平台)
1,配置主机名和主机名互相绑定
# hostnamectl set-hostname --static 172.16.2.8
# vim /etc/hosts
192.168.122.8 172.16.2.8
192.168.122.11 web1.cluster.com
192.168.122.12 web2.cluster.com
2,静态ip
3,关闭iptables,selinux
4,时间同步
5,配置yum源 (haproxy服务器需要配置163源)
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
第一步:
1,客户端准备
客户端有firefox和elinks就可以了
2,后台web服务器准备 
web1上做:
# yum install httpd httpd-devel -y
# systemctl start httpd
# systemctl enable httpd
# echo web1 > /var/www/html/index.html
web2上做:
# yum install httpd* -y
# systemctl start httpd
# systemctl enable httpd
# echo web2 > /var/www/html/index.html
第二步:
在haproxy服务器上安装haproxy
# yum install haproxy
rpm -ql haproxy
/etc/haproxy
/etc/haproxy/haproxy.cfg
/etc/logrotate.d/haproxy
/etc/sysconfig/haproxy
/usr/bin/halog
/usr/bin/iprange
/usr/lib/systemd/system/haproxy.service
/usr/sbin/haproxy
/usr/sbin/haproxy-systemd-wrapper
/usr/share/doc/haproxy-1.5.18/haproxy-en.txt --参数文档
/etc/haproxy/haproxy.cfg --主配置文件
第三步:
配置haproxy
配置结构介绍:
global
全局配置参数(主要配置服务用户,pid,socket,进程,chroot等)
defaults
负载调度相关的全局配置 (调度模式,调度超时时间,调度算法,健康检查等等。配置在这个段,那么就默认对后面的listen,frontend,backend都生效)
frontend
处理前台接收的请求,可以在这个段配置acl进行七层调度等,指定调到对应的backend
backend
最终调度的realserver相关配置
listen (就是frontend和backend的综合体)
# grep -v '#' /etc/haproxy/haproxy.cfg --下面是我的配置示例(global,defaults都基本没有调整,优化相关参数请参考文档和实际生产环境做调整)
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode http
log global
option httplog
option dontlognull
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
listen 172.16.2.8 *:80
balance roundrobin
server web1.cluster.com 192.168.122.11:80
server web2.cluster.com 192.168.122.12:80
-------------------------------------------------------
把上面的listen配置段改成下面的frontend和backend,效果一样
frontend 172.16.2.8 *:80
default_backend webs
backend webs
balance roundrobin
server web1.cluster.com 192.168.122.11:80
server web2.cluster.com 192.168.122.12:80
--------------------------------------------------------
# systemctl start haproxy
# systemctl enable haproxy
第四步:
客户端 elinks 172.16.2.8 测试
结果为rr轮循web1,web2
====================================================
haproxy状态页面
--配置文件listen配置段加上stats的四行
listen 172.16.2.8 *:80
stats uri /haproxy-stats --指定访问的路径
stats realm Haproxy\ statistics --指定统计信息提示
stats auth li:li123 --需要验证的用户名和密码才能登录查看
stats hide-version --隐藏客户端访问统计页面时的haproxy版本号
balance roundrobin
server web1.cluster.com 192.168.122.11:80
server web2.cluster.com 192.168.122.12:80
# systemctl reload haproxy.service
--客户端使用下面的去访问
或者换成下面的配置,效果一样
frontend 172.16.2.8 *:80
default_backend servers
backend servers
stats uri /haproxy-stats
stats realm Haproxy\ statistics
stats auth li:li123
stats hide-version
balance roundrobin
server web1.cluster.com 192.168.122.11:80
server web2.cluster.com 192.168.122.12:80
==============================
关于haproxy日志
# vim /etc/rsyslog.conf
$ModLoad imudp
$UDPServerRun 514 --打开这两句的注释,表示udp协议514端口接收远程日志(haproxy日志做到127.0.0.1的本地,也要按远程日志做法来做)
local2.* /var/log/haproxy.log --加上这一句,表示local2日志设备的所有级别日志都会记录到后面的文件路径中(local2是和haproxy里的配置对应的)
# systemctl restart rsyslog.service
客户端访问后,在haproxy服务器上就可以cat /var/log/haproxy.log查看日志了
===============================
haproxy的健康检查功能和日志处理
--再加上下面的option 两句
listen 172.16.2.8 *:80
stats uri /haproxy-stats
stats realm Haproxy\ statistics
stats auth li:li123
stats hide-version
balance roundrobin
option forwardfor --日志forward,让后台web记录客户端的IP,而不是haproxy的IP
option httpchk HEAD /check.txt HTTP/1.1 --健康检查功能如果后台web服务器家目录中没有check.txt文件,则表示后台web挂掉;此版本要使用http的1.0版,1.1版还不支持
server web1.cluster.com 192.168.122.11:80 check inter 2000 rise 2 fall 5
server web2.cluster.com 192.168.122.12:80 check inter 2000 rise 2 fall 5 --加上检查的间隔2秒,rise 2是2次正确表示服务器可用;fall 5表示5次失败表示服务器不可用
# systemctl reload haproxy --刷新
客户端验证:有check.txt文件的则可以被调,没有的就不可以
日志问题1
后台web里每2秒都会有一句healthcheck的日志,想删除他,方法如下
方法一:
写一个脚本就如下两句,定时去执行一次就可以了
sed -i '/check.txt\ HTTP\/1.0/d' /var/log/httpd/access_log
kill -HUP `cat /var/run/httpd/httpd.pid`
方法二:
SetEnvIf Request_URI "^/check\.txt$" dontlog
CustomLog logs/access_log combined env=!dontlog
重启后端的web服务器
/etc/init.d/httpd reload
日志问题2:
查看后端web的access.log,客户端的正常访问日志的IP并不是实际客户端IP,而是haproxy的内网IP
解决方法如下:
方法一:
直接使用前端haproxy的日志
方法二:
后端apache日志处理 --为了让access.log显示客户端的IP,而不是haproxy调度器的IP
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b " combined --把原来的combined日志注释,再加上这一句
CustomLog logs/access_log combined
=====================================================
使用haproxy做动静分离或网站数据切分(七层调度)
例一:
frontend 172.16.2.8 *:80
# acl invalid_src src 172.16.2.9 --如果你要拒绝它访问,就把注释打开
# block if invalid_src
acl url_static path_end .html .png .jpg .css .js
use_backend static if url_static
default_backend dynamic
backend static
server web1.cluster.com 192.168.122.11:80 check inter 2000 rise 2 fall 5
backend dynamic
server web2.cluster.com 192.168.122.12:80 check inter 2000 rise 2 fall 5
例二:
frontend 172.16.2.8 *:80
acl url_static path_end .html .png .jpg .css .js
acl url_static path_beg /static /images /img
acl host_www hdr_beg(host) -i www
acl host_static hdr_beg(host) -i img. video. download.
use_backend static if host_static or host_www url_static
use_backend dynamic if host_www
backend static
server web1.cluster.com 192.168.122.11:80 check inter 2000 rise 2 fall 5
backend dynamic
server web2.cluster.com 192.168.122.12:80 check inter 2000 rise 2 fall 5
课后扩展;
上网查squid,varnish,ats,nginx,lvs,haproxy等软件的区别与优缺点?
posted @ 2018-06-19 22:21  Sky-wings  阅读(162)  评论(0编辑  收藏  举报