- 负载均衡(Load Balance,LB):一项反向代理技术。负载均衡将特定的业务(web服务、网络流量等)分担给指定的一个或多个后端特定的服务器或设备。
- 负载均衡类型
4层:LVS(Linux Virtual Server),HAproxy,Nginx
7层(osi参考模型)负载均衡:HAproxy,nginx。
lvs:ip和端口负载,效率高。无法做到动静分离(它无法区分动态与静态)。
动态:java,php等访问。
静态:html,图片等。
HAproxy,nginx根据URL种类区分动态与静态。
工作方式:HAproxy冒充用户,给服务器发请求。
HAproxy是一款高性能的负载均衡软件。
- 特点
- 支持tcp(4层)/http(7层)两种协议层的负载均衡,使得其负载均衡功能十分丰富。
- 支持8中左右的负载均衡算法,尤其在http模式时,有许多非常实在的负载均衡算法,适用各种需求。
- 基于事件驱动的连接处理模式和单进程处理模式(和Nginx类型)。
- 拥有一个功能出色的监控网页,实时了解系统当前的状态。
- 有强大的访问控制(ACL)支持
- 不支持的功能
- 正向代理–squid,nginx
- 缓存代理–varnish
- web服务–nginx、tengine、apache、php、tomcat
- UDP–目前不支持UDP协议
- 单机性能–相比LVS性能较差
(在集群中,服务器的时间必须一致)
HAproxy的配置
- HAproxy的安装
sudo apt-get install haproxy #安装haproxy
通过which haproxy
可以看到haproxy是安装在/usr/sbin/haproxy
下,HAProxy的配置文件路径为:/etc/haproxy/haproxy.cfg
使用sudo haproxy -f /etc/haproxy/haproxy.cfg
即可启动HAProxy
- 打开配置文件
cat /etc/haproxy/haproxy.cfg #后面是配置文件的路径。此路径很重要。
结果如下:
以下配置很多是用不到。但为了了解配置文件,把很多参数都列举了出来。
global #全局配置
log /dev/log local0#日志配置 haproxy支持将日志发给统一的机器。local0,local1是级别
maxconn n#最大连接数为n(此处没有)
nbproc n#开启haproxy进程数目(此处没有),默认为1
cpu-map 1 0#绑定haproxy进程至指定cpu,将第一个进程绑定在0号cpu
nbproc 1 #指定启动的haproxy进程的数目。只能用于守护进程模式的haproxy
pidfile /usr/local/haproxy/logs/haproxy.pid #指定pid文件生成目录
log /dev/log local1 notice
chroot /var/lib/haproxy # 改变当前工作目录,基于安全性的考虑
stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners #socket文件
stats timeout 30s
user haproxy#所属用户
group haproxy#所属组
daemon #以守护进程方式运行haproxy,常驻内存
# Default SSL material locations
ca-base /etc/ssl/certs
crt-base /etc/ssl/private
# Default ciphers to use on SSL-enabled listening sockets.
# For more information, see ciphers(1SSL). This list is from:
# https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
# An alternative list with additional directives can be obtained from
# https://mozilla.github.io/server-side-tls/ssl-config-generator/?server=haproxy
ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS
ssl-default-bind-options no-sslv3
defaults #listen,frontend,backend块进行设置如果块中没设置,则使用默认设置
log global#此行意思是log放在全局里面。放在全局设定的路径里
mode http #模式。http看url;tcp看ip和端口。
retries 3 #连接后端服务器最多失败次数
option httplog #启用记录HTTP请求、会话状态和计时器的功能
option dontlognull#启用该项,日志中将不会记录关于对后端服务器的检测情况。
timeout connect 5000#连接超时
timeout client 50000#客户端超时
timeout server 50000#服务器超时
timeout check 5s:检测超时
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
###########################################
frontend app01#前端(HAproxy前段是用户,后端是服务器),app0是任意起的名字。
bind *:8081 #绑定IP和端口
default_backend app01_backend#默认后端
option forwardfor #允许在发往服务器的请求首部中插入“X-Forwarded-For”首部。
option httpclose #每次请求完毕后主动关闭http通道
backend app01_backend
balance roundrobin #使用轮流派发的算法。使得请求轮流的发给此模块的server。(balance source 保存session值,支持static-rr,leastconn,first,uri等参数)
server web1 10.32.201.102:8080 #绑定服务器端ip和端口。
option redispatch#当使用了cookie时,haproxy将会将其请求的后端服务器的serverID插入到cookie中,以保证会话的SESSION持久性;而此时,如果后端的服务器宕掉了,但是客户端的cookie是不会刷新的,如果设置此参数,将会将客户的请求强制定向到另外一个后端server上,以保证服务的正常。
option abortonclose #丢弃由于客户端等待时间过长而关闭连接但仍在haproxy等待队列中的请求
cookie SERVERID#允许插入serverid到cookie中,serverid后面可以定义
#myslave02 172.18.74.87:80 cookie 2 weight 3 check inter 2000 rise 2 fall 3 #cookie 1表示serverid为1,check inter 1500 是检测心跳频率,rise 2是2次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重
listen stats
bind *:8083#监听端口
stats refresh 30s #统计页面自动刷新时间
og 127.0.0.1 local0 err #全局的日志配置,local0 是日志设备,err 表示日志级别。其中日志级别有err、 warning、info、debug 四种可选。这个配置表示使用 127.0.0.1 上的 rsyslog 服务中的local0 日志设备,记录日志等级为err。
stats refresh 30s #统计页面自动刷新时间
stats enable # 启用haproxy监控状态
stats uri /haproxy-status #统计页面url
stats realm welcome login\ Haproxy #统计页面密码框上提示文本
stats auth admin:admin123 #统计页面用户名和密码设置
stats hide-version #隐藏统计页面上HAProxy的版本信息
stats admin if TRUE #通过设置此选项,可以在监控页面上手工启用或禁用后端真实服务器,仅在haproxy1.4.9 以后版本有效
2.日志配置。
首先,查看是否已经默认配置好。执行以下命令
less -f /var/log/haproxy.log
3.改了配置文件之后,不重启服务,通常都是无效的。所以,改配置后一定一定要重启服务。
4.HAproxy监控页的配置
主要改配置文件中的listen模块。
stats enable # 启用haproxy监控状态
stats hide-version # 隐藏haproxy的版本号,非必须
stats uri /haproxy # 设置haproxy监控页的uri,可任意设置
stats auth admin:admin # 验证用户名密码
stats admin if TRUE # 此项是实现haproxy监控页的管理功能的。
访问方式,在浏览器输入以下命令:
http://10.32.204.79:8081/haproxy#配置了haproxy的机器IP,listen模块绑定的端口(注意注意)。uri设置的是之后的格式。