III (十七) haproxy(2)
haproxy是一个开源的、高性能的基于tcp和http应用代理的HA的、LB服务软件,它支持双机热备、HA、LB、虚拟主机、图形界面查看状态信息等功能,其配置简单、维护方便,而且后端RS的health check功能有很好的支持(相当于keepalived的health check),当它代理的后端RS server故障时,haproxy会自动将该server移除,当故障的RS server恢复后,还会自动加入进来继续提供服务;
与nginx比,haproxy没有web server功能,只能用于代理,特别适用于高负载、访问量大,需要会话保持(LVS的-p和nginx的iphash会导致负载不均;haproxy会在会话中插入cookie用以保持会话)及七层应用代理的业务;
haproxy运行在当下普通的server上,不需优化就可支持数以万计的并发连接,并且它的运行模式使得它可以很简单、轻松、安全的整合到各种已有的网站架构中,同时它的代理模式可使所有应用服务器不暴露到公网上(也即它背后的node server不需公网IP);
frontend(可用acl规则匹配,让运维管理人员根据任意HTTP请求头内容做规则匹配,然后把请求定向到相关的backend);
backend(定义多个RS server pools,frontend把请求转过来交给此处定义的RS server处理);
通过frontend和backend可很容易的实现各种七层应用代理功能;
haproxy支持两种主要代理模式:
基于4层的tcp协议族应用代理(haproxy仅在client和server-side之间进行流量转发,如邮件服务、内部协议通信server、MySQL、https等);
基于7层的http应用代理(haproxy会分析应用层协议,通过允许、拒绝、交换、增加、修改、删除request请求或response回应指定内容来控制协议);
注:haproxy软件采用的是类NAT模式(与LVS的NAT模式本质不同),数据包来去都会经过haproxy,因此在流量特别大时(尤其门户网站),其效率不如LVS的DR模式,在一般的中小型公司(千万级PV以下)建议使用haproxy做LB,对于负责运维的管理人员来说配置简单、快速、维护方便,出问题好排查(运维使用软件的原则:简单、易用、高效);
proxy-1-1(eth0:10.96.20.113,eth1:172.16.10.113)
proxy-1-2(eth0:10.96.20.114,eth1:172.16.10.114)
[root@proxy-1-1 ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.5(Santiago)
2.6.32-642.3.1.el6.x86_64 x86_64
[root@proxy-1-1 ~]# vim /etc/sysctl.conf
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route =0
error:"net.bridge.bridge-nf-call-ip6tables" is an unknown key
error: "net.bridge.bridge-nf-call-iptables"is an unknown key
error:"net.bridge.bridge-nf-call-arptables" is an unknown key
[root@proxy-1-1 haproxy]# elinks -dump http://172.16.10.118
[root@proxy-1-1 haproxy]# elinks -dump http://172.16.10.119
[root@proxy-1-1 ~]# yum -y install haproxy
[root@proxy-1-1 ~]# rpm -ql haproxy
/usr/share/doc/haproxy-1.5.4/CHANGELOG
[root@proxy-1-1 ~]# id haproxy
uid=188(haproxy) gid=188(haproxy)groups=188(haproxy)
[root@proxy-1-1 ~]# haproxy -h #(-sf可用于平滑重启)
HA-Proxy version 1.5.4 2014/09/02
Copyright 2000-2014 Willy Tarreau<w@1wt.eu>
Usage : haproxy [-f <cfgfile>]* [ -vdVD ] [ -n <maxconn> ] [ -N <maxpconn> ]
[ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ]
-v displays version ; -vv shows known build options.
-d enters debug mode ; -db only disables background mode.
-dM[<byte>] poisons memory with <byte> (defaults to 0x50)
-V enters verbose mode (disables quiet mode)
-D goes daemon ; -C changes to<dir> before loading files.
-q quiet mode : don't display messages
-c check mode : only check config filesand exit
-n sets the maximum total # of connections (2000)
-m limits the usable amount of memory (in MB)
-N sets the default, per-proxy maximum # of connections (2000)
-L set local peer name (default to hostname)
-p writes pids of all children to this file
-de disables epoll() usage even when available
-dp disables poll() usage even when available
-dS disables splice usage (broken on old kernels)
-dV disables SSL verify on servers side
-sf/-st [pid ]* finishes/terminates oldpids. Must be last arguments.
[root@proxy-1-1 ~]# cd /etc/haproxy/
[root@proxy-1-1 haproxy]# mv haproxy.cfg haproxy.cfg.ori
[root@proxy-1-1 haproxy]# egrep -v "#|^$" haproxy.cfg.ori > haproxy.cfg
[root@proxy-1-1 haproxy]# vim haproxy.cfg
log 127.0.0.1:514local0 info #(info会记录访问日志,生产中不要用,要用warning或error)
maxconn 204800 #(定义每个haproxy进程的最大连接数,每个连接包括client和server-side,所以单个tcp会话最大数目是该值的两倍)
stats socket /var/lib/haproxy/stats
#nbproc 8 #(设置haproxy启动时的进程数,最好与cpu数相同)
mode http #(语法:mode http|tcp|health)
option forwardfor except 127.0.0.0/8
timeout connect 10s #(设置成功连接到一台server的最长等待时间,默认单位ms可省,若非默认要跟单位,旧版本为contimeout)
timeout client 1m #(设置连接client发送数据时的成功连接最长等待时间,旧版本为clitimeout)
timeout server 1m #(设置服务端回应client数据发送的最长等待时间,旧版为srvtimeout)
#option httpchk HEAD /check.html HTTP/1.0
server web1 172.16.10.118:80 check
server web2 172.16.10.119:80 check
[root@proxy-1-1 haproxy]# vim /etc/rsyslog.conf
[root@proxy-1-1 haproxy]# vim /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS="-c2 -m 0 -r -x"
[root@proxy-1-1 haproxy]# service rsyslog restart
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
[root@proxy-1-1 haproxy]# lsof -i :514
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsyslogd 6526 root 3u IPv4 81287 0t0 UDP *:syslog
rsyslogd 6526 root 4u IPv6 81288 0t0 UDP *:syslog
[root@proxy-1-1 haproxy]# ps aux | grepsyslog
root 6526 0.0 0.7 267752 1616 ? Sl 22:00 0:00 /sbin/rsyslogd -i /var/run/syslogd.pid -c 2 -m 0 -r -x
root 6535 0.0 0.3 103264 836 pts/0 S+ 22:00 0:00 grep syslog
[root@proxy-1-1 haproxy]# haproxy -c -f haproxy.cfg
[root@proxy-1-1 haproxy]# service haproxy start
[root@proxy-1-1 haproxy]# chkconfig --list rsyslog
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@proxy-1-1 haproxy]# chkconfig haproxy on
[root@proxy-1-1 haproxy]# chkconfig --list haproxy
haproxy 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@proxy-1-1 haproxy]# netstat -tnulp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6501/haproxy
[root@proxy-1-1 haproxy]# ps aux | grep haproxy
root 6551 0.0 0.3 103264 828 pts/0 S+ 22:01 0:00 grep haproxy
[root@proxy-1-1 haproxy]# tail /var/log/haproxy.log
#for i in {1..10} ; do curl 10.96.20.113;sleep 2; done #(单独找一台主机测试当前算法)
server web1 172.16.10.118:80 check port 80 inter 5000 fall 5
check port 80(表示对80port进行health check,可简写为check)
2、基于ip的http uri方式的health check:
option httpchk HEAD /check.html HTTP/1.0
语法:option httpchk METH URI VER
URI(站点目录下有check.html,此文件由开发提供)
option httpchk HEAD /index.jsp HTTP/1.1\r\nHost:test.51cto.com
注:测试时需要在RS上配置基于域名的vhost,并创建index.jsp测试页面,此文件只要存在haproxy能检测到则显示正常,适用于更精细的基于具体业务的检测需求
option smtpchk -> sends "HELO localhost"
option smtpchk EHLO mail.mydomain.com -> sends this ESMTP greeting
option mysql-check [user USERNAME]
server <name> <address>[:port] [settings ...]
inter <delay>(interval间隔,表示每5秒检查一次)
rise <count>(检查后端RS共2次若正常,则将其加入进来)
若结尾无inter 2000 fall 3,则按默认每2s检查一次共3次;
nginx中的upstream段中也有类似的配置;max_fails=2,最大尝试失败次数,默认为1,0表示禁止失败尝试;fail_timeout=2;
server web7 172.16.10.7:80 check port 80 inter 5000 fall 5 backup
server web8 172.16.10.8:80 check port 80 inter 5000 fall 5 backup
基于域名中的指定字串做7层跳转(在frontend段中使用acl做更精细的转发):
acl <aclname> <criterion> [flags] [operator] [<value>] ...
acl www_domain hdr(host)-i www.test.com
acl blog_domain hdr(host)-i blog.test.com
acl www_domain hdr_beg(host) -i www.
acl blog_domain hdr_beg(host) -i blog.
[root@proxy-1-1 haproxy]# vim /etc/hosts
[root@proxy-1-1 haproxy]# elinks -dump www.test.com
[root@proxy-1-1 haproxy]# elinks -dump blog.test.com
[root@proxy-1-1 haproxy]# elinks -dump 172.16.10.118
[root@proxy-1-1 haproxy]# elinks -dump 172.16.10.119
[root@proxy-1-1 haproxy]# vim haproxy.cfg
stats socket /var/lib/haproxy/stats
option forwardfor except 127.0.0.0/8
acl www_domain hdr(host)-i www.test.com
acl blog_domain hdr(host)-i blog.test.com
use_backend blogif blog_domain
server web1 172.16.10.118:80 check port 80 inter 1000 fall 1
server web1 172.16.10.119:80 check port 80 inter 1000 fall 1
[root@proxy-1-1 haproxy]# haproxy -c -f haproxy.cfg
[root@proxy-1-1 haproxy]# service haproxy restart
在win的HOSTS文件加入:10.96.20.113 www.test.com blog.test.com
acl static_test path_beg /nginx/
acl dynamic_test path_beg /php/
注:例如在访问www.test.com/nginx/时交至后端nginxpools处理;访问www.test.com/php/时交至后端phppools处理
[root@proxy-1-1 haproxy]# vim haproxy.cfg
acl static_test path_beg /nginx/
acl dynamic_test path_beg /php/
use_backend www if static_test
use_backend blog if dynamic_test
server web1 172.16.10.118:80 check port 80 inter 1000 fall 1
server web1 172.16.10.118:80 check port 80 inter 1000 fall 1
[root@proxy-1-1 haproxy]# haproxy -c -f haproxy.cfg
[root@proxy-1-1 haproxy]# service haproxy restart
acl pic_test path_end .gif .png .jpg .css .js
acl pic_test path_end \.(gif|png|jpg|css?.*|js?.*)$
[root@proxy-1-1 haproxy]# vim haproxy.cfg
acl pic_test path_end .gif .png .jpg .css .js
acl static_test path_beg /nginx/
acl dynamic_test path_beg /php/
use_backend www if static_test
use_backend blog if dynamic_test
server web1 172.16.10.118:80 check port 80 inter 1000 fall 1
server web1 172.16.10.118:80 check port 80 inter 1000 fall 1
[root@proxy-1-1 haproxy]# haproxy -c -f haproxy.cfg
[root@proxy-1-1 haproxy]# service haproxy restart
acl iphone_users hdr_sub(user-agent) -i iphone
redirect prefix http://3g-iphone.test.com if iphone_users
acl android_users hdr_sub(user-agent) -i android
redirect prefix http://3g-android.test.com if android_users
注:本地要设置跳转后的地址页面;nginx中"$http_user_agent";httpd中\"%{user-agent}i\";
[root@proxy-1-1 haproxy]# vim haproxy.cfg
acl iphone_users hdr_sub(user-agent) -i iphone
redirect prefix http://3g-iphone.test.com if iphone_users
acl android_users hdr_sub(user-agent) -i android
redirect prefix http://3g-android.test.com if android_users
acl pic_test path_end \.(gif|png|jpg|css?.*|js?.*)$
acl static_test path_beg /nginx/
acl dynamic_test path_beg /php/
use_backend www if static_test
use_backend blog if dynamic_test
server web1 172.16.10.118:80 check port 80 inter 1000 fall 1
server web1 172.16.10.118:80 check port 80 inter 1000 fall 1
acl valid_ip src 10.96.20.0/24
http-request deny if ! valid_ip
错误页面优雅显示(不支持404,status code 404 not handled,error relocation will be ignored):
errorfile 400 /etc/haproxy/errorfiles/400badreq.http
errorfile 408 /dev/null # workaround Chrome pre-connect bug
errorfile 403 /etc/haproxy/errorfiles/403forbid.http
errorfile 503 /etc/haproxy/errorfiles/503sorry.http
balance <algorithm> [ <arguments> ]
algorithm有:roundrobin,static-rr,leastconn,first,source,uri,url_param,hdr(<name>),rdp-cookie(<name>)
[root@proxy-1-1 haproxy]# vim haproxy.cfg
[root@proxy-1-1 haproxy]# service haproxy restart
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common
CustomLog logs/access_log common
[root@localhost ~]# service httpd restart
"-" - - [15/Aug/2016:01:22:46 -0700] "HEAD /check.html HTTP/1.0" 200 -
"10.96.20.89" - - [15/Aug/2016:01:22:47 -0700] "GET / HTTP/1.1" 200 4
"10.96.20.89" - - [15/Aug/2016:01:22:48 -0700] "GET / HTTP/1.1" 200 4
"-" - - [15/Aug/2016:01:22:51 -0700] "HEAD /check.html HTTP/1.0" 200 -
主备模式(heartbeat仅负责VIP飘移,两台server上haproxy配置相同,listen段绑定VIP,事先把haproxy启动);
[root@proxy-1-1 haproxy]# vim /etc/ha.d/haresources
proxy-1-1 IPaddr::10.96.20.8/24/eth0
[root@proxy-1-1 haproxy]# vim haproxy.cfg
主主模式(heartbeat仅负责两VIP飘移,两台server上haproxy配置相同,有两段listen配置,分别绑定两个VIP,事先把haproxy启动);
[root@proxy-1-1 haproxy]# vim /etc/ha.d/haresources
proxy-1-1 IPaddr::10.96.20.8/24/eth0
proxy-1-2 IPaddr::10.96.20.9/24/eth0
[root@proxy-1-1 haproxy]# vim haproxy.cfg
200-OK(standard response for successful HTTP requests),服务成功返回网页;
代码、软件在LB器(LVS、nginx、haproxy)层面上实现平滑上线:
LVS(通过命令ipvsadm -d -t|-u SERVICE_ADDRESS -r SERVER_ADDRESS;也可通过替换配置文件,但LVS没有平滑重启的方式);
nginx和haproxy(替换配置文件,#service nginx|haproxy reload达到平滑重启);
DB更新(改配置时>SET GLOBAL ……;同时改配置文件);
主从,仅在server物理故障时恢复,若手动drop误删则无法恢复,解决办法:备机开binlog保持1天以上,并按天做备份;
案例(baidu,一主多从,其中一个从不提供服务,专门做备份,半同步插件或drbd做到实时同步,事务提交)
全量(drbd;通过程序实现双写,提交数据时写到两个存储上;分布式存储nosql、MySQL、mongodb同步机制做存储;分布式架构方案);
3、程序、各配置文件都要放到SVN里,再向外发布,办公室SVN-->IDC测试-->IDC正式,关键要维护好SVN
注:需求分析(对每个项目都要事先写好备份规划,根据需求讨论最多可承受丢多长时间的数据;数据库、存储备份、测试数据)
集群(LB(LVS、nginx、haproxy、BIG-IP、NetScaler)、HA(keepalived、heartbeat))
监控(运维级别,业务级别(按产品线监控、流量、负载、访问、请求、错误日志、接口)
注:QPS每秒请求量、RT反馈时间,三个层面监控(基础监控-->应用监控-->业务层面监控)
本文转自 chaijowin 51CTO博客,原文链接:http://blog.51cto.com/jowin/1840986,如需转载请自行联系原作者
III (十七) haproxy(2)相关推荐
- 第十七届全国大学生完全车模组竞速赛方案 - 草案
简 介: 本文给出了 第十七届全国大学智能汽车竞赛竞速比赛 中完全模型组的比赛细则. 关键词: 全国大学生智能车,完全模型 (讨论稿件) #mermaid-svg-w1kxRuRv6qtJdINV { ...
- 数据科学和人工智能技术笔记 十七、聚类
十七.聚类 作者:Chris Albon 译者:飞龙 协议:CC BY-NC-SA 4.0 凝聚聚类 # 加载库 from sklearn import datasets from sklearn.p ...
- “统信杯” 第十七届黑龙江省大学生程序设计竞赛
"统信杯" 第十七届黑龙江省大学生程序设计竞赛 前言 题目 A. Bookshelf Filling C. Tree Division D. Collision Detector ...
- 2020第十七届华为杯数模C题——P300脑电信号数据预处理算法
脑电信号数据预处理 这两天的数学建模选的C题,目标是要处理脑电P300信号的数据并进行相关预测任务.该题重点是数据预处理,因此根据最后实验的结果,分享相关的预处理方法以及源代码.长话短说,给出任务 ...
- java人员安排表_Java实现 LeetCode 732 我的日程安排表 III(暴力 || 二叉树)
732. 我的日程安排表 III 实现一个 MyCalendar 类来存放你的日程安排,你可以一直添加新的日程安排. MyCalendar 有一个 book(int start, int end)方法 ...
- 算法套路十七——买卖股票问题:状态机 DP
算法套路十七--买卖股票问题:状态机 DP 状态机DP是一种将动态规划方法应用于有限状态机(Finite State Machine)的问题求解方法. 状态机DP(State Machine DP)是 ...
- PyTorch的十七个损失函数
20220113 选损失函数的标准:能使得真实值和预测值越相近的时候总损失越小 20220303 机器学习大牛是如何选择回归损失函数的? MSE,MAE,huber loss 20210925 交叉熵 ...
- 2021年大数据Hadoop(二十七):YARN运行流程
全网最详细的Hadoop文章系列,强烈建议收藏加关注! 后面更新文章都会列出历史文章目录,帮助大家回顾知识重点. 目录 本系列历史文章 前言 Yarn运行流程 本系列历史文章 2021年大数据Hado ...
- Haproxy Nginx cluster构建
-----client---------haproxy-------nginx1---------nginx2------ 192.168.1.250 192.168.1.1 192.168.1.10 ...
最新文章
- 并发 IO多路复用 select 非asyncio
- English Spoken Math
- Delphi字符串函数Delete
- Python高效画饼图
- Hessian 原理分析
- [转载]Spring配置文件详解一:
- 百度BCH云服务器(免费版)SSL证书 - 部署篇
- 在ecshop商品详情页显示供货商
- web元件库/常用web组件/常用表单/导航栏/边框/图标/日期时间选择器/评分组件/穿梭框/输入框/步骤条/计数器/输入框/Axure原型/axure元件库/rp原型/交互控件/五星评分器/导航框架
- 使用FFmpeg类库实现YUV视频序列编码为视频
- PostgreSQL 递归查询 (转)
- 联发科被动“卡位”内地集成电路市场 剑指老对手展讯
- AWGN信道中BPSK调制的定点仿真性能_Simulink实现
- 管家婆 打开经营历程 Date exceeds maximum of 19-12-31报错解决
- 最全的Excel-sumif函数多条件求和案例汇总
- Postman代理设置
- Hadoop 集群在WebUI界面不能下载文件
- 关于 Kubernetes中JobCronJob的一些笔记
- 【中塘镇】助力儿童,健康成长---创意超轻黏土手工制作主题活动
- 人力外派和猎头的区别是什么?哪个行业更赚钱?