HAproxy
1、 无负载平衡
没有负载平衡的简单Web应用程序环境可能
2、4层负载平衡
将网络流量负载,平衡到多个服务器的最简单方法,是使用第4层(传输层)负载平衡。以这种方式进行负载均衡将根据IP范围和端口转发用户流量(即,如果请求进入Free classifieds - yourdomain.com,则流量将转发到处理yourdomain.com的所有请求的后端。端口80)。
3、 7层负载平衡
层负载平衡是更复杂的负载均衡网络流量的方法是使用第7层(应用层)负载均衡。使用第7层允许负载均衡器根据用户请求的内容将请求转发到不同的后端服务器。这种负载平衡模式允许您在同一域和端口下运行多个Web应用程序服务器。
7层负载均衡、应用程序负载均衡、URL负载均衡、动静分离技术
免费、快速并且可靠
是一款高性能的负载均衡软件。 因为其专注于负载均衡这一些事情, 因此与nginx比起来在负载均衡这件事情上做更好,更专业。
- 用户群体
-
包括 GitHub、Bitbucket 、Stack Overflow 、Reddit、Tumblr、Twitter 和 Tuenti 在内的知名网站,及亚马逊网络服务系统都使用了HAProxy。
-
- HAProxy Session亲缘性
-
haproxy负载均衡保持客户端和服务器Session亲缘性的三种方式
-
1 用户IP 识别
-
haproxy 将用户IP经过hash计算后 指定到固定的真实服务器上(类似于nginx 的IP hash 指令)
-
配置指令 balance source
-
-
2 cookie 识别
-
haproxy 将WEB服务端发送给客户端的cookie中插入(或添加前缀)haproxy定义的后端的服务器COOKIE ID。
-
配置指令例举 cookie SESSION_COOKIE insert indirect nocache
-
部分浏览器,可以观察到用户的请求头的cookie里 有类似" Cookie jsessionid=0bc588656ca05ecf7588c65f9be214f5; SESSION_COOKIE=app1" SESSION_COOKIE=app1就是haproxy添加的内容3
-
-
3 session 识别
-
haproxy 将后端服务器产生的session和后端服务器标识存在haproxy中的一张表里。客户端请求时先查询这张表。
-
配置指令例举 appsession JSESSIONID len 64 timeout 5h request-learn
-
动静分离示例
frontend webservs
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 url_php path_end -i .php
acl host_static hdr_beg(host) -i img. imgs. video. videos. ftp. image. download.
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.1.111:80 check maxconn 3000
server node2 192.168.1.112:80 check maxconn 3000
backend dynamic
balance roundrobin
server node3 192.168.1.113:80 check maxconn 3000
server node4 192.168.1.113:80 check maxconn 3000
特点
-
支持tcp / http 两种协议层的负载均衡,使得其负载均衡功能非常丰富。
-
支持8种左右的负载均衡算法,尤其是在http模式时,有许多非常实在的负载均衡算法,适用各种需求。
-
性能非常优秀,基于事件驱动的链接处理模式及单进程处理模式(和Nginx类似)让其性能卓越。
-
处理模式
-
单进程处理模式
-
所有客户端连接全部都由同一个服务进程来处理,目标就是等待连接,来一个分配一个,主要消耗cpu,
-
-
多线程处理模式
-
多线程模式消耗内存,会限制并发而且多线程需要进程间通信,也会消耗相当多的cpu资源
-
-
-
-
拥有一个功能出色的监控页面,实时了解系统的当前状况。
-
功能强大的ACL支持,给用户极大的方便。
示例1
-
环境
-
4台linux,做好域名解析
-
一定关闭防火墙、selinux、配置yum源
-
Client
-
192.168.229.11/24 (真实机或虚拟机均可做客户端)
-
-
HAproxy
-
192.168.229.12/24
-
-
web1
-
192.168.229.13/24
-
-
web2
-
192.168.229.14/24
-
-
-
步骤
-
0.全部的服务器完成时间统一
-
#ntpdate 时间服务器ip
-
ntpdate ntp.aliyun.com
-
或者 ntpdate cn.ntp.org.cn
-
-
-
1.web1 & web2 创建测试页面
-
# yum install httpd -y
-
web1
-
#echo web1 > /var/www/html/index.html
-
准备页面
-
-
web2
-
#echo web2 > /var/www/html/index.html
-
准备页面
-
-
systemctl start httpd
-
systemctl enable httpd
-
测试
2.haproxy主机安装haproxy(192.168.229.12)
-
# yum install epel-release -y
-
# yum install haproxy -y
3.配置HAproxy(192.168.229.12)
-
# vim /etc/haproxy/haproxy.cfg
-
haproxy 配置中分成五部分内容
-
global: 设置全局配置参数,属于进程的配置,通常是和操作系统相关。
-
defaults:配置默认参数,这些参数可以被用到frontend,backend,Listen组件;
-
frontend:接收请求的前端虚拟节点,Frontend可以更加规则直接指定具体使用后端的backend;
-
backend:后端服务集群的配置,是真实服务器,一个Backend对应一个或者多个实体服务器;
-
Listen :frontend和backend的组合体。
-
-
配置示例
-
global
log 127.0.0.1 local3 info
maxconn 4096
uid nobody
# uid 99
gid nobody
# gid 99
daemon
nbproc 1
pidfile /run/haproxy.pid
defaults
log global
mode http
maxconn 2048
retries 3
option redispatch
contimeout 5000
clitimeout 50000
srvtimeout 50000
#timeout connect 5000
#timeout client 50000
#timeout server 50000
option abortonclose
stats uri /admin?stats
stats realm Private lands
stats auth admin:password
stats hide-version
frontend http-in
bind 0.0.0.0:80
mode http
log global
option httplog
option httpclose
acl html url_reg -i \.html$
use_backend html-server if html
default_backend html-server
backend html-server
mode http
balance roundrobin
option httpchk GET /index.html
cookie SERVERID insert indirect nocache
server html-A web1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
server html-B web2:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5
gloab:全局配置
log:日志配置
maxconn:最大连接限制(优先级低)
uid:用户
gid:组用户
deamon:守护进程运行
nbproc :haproxy进程数,该值的设置应该和服务器的CPU核心数一致,比如设置为 16,即常见的2颗8核心CPU的服务器,即共有16核心,则可以将其值设置为:<=16 ,创建多个进程数,可以减少每个进程的任务队列,但是过多的进程数也可能会导致进程的崩溃。
pidfile /run/haproxy.pid :haproxy进程ID存储位置
defaults:针对(listen和backend块进行设置没如果块中没设置,则使用默认设置)默认配置
log:日志使用全局配置
mode:模式7层LB
maxconn:最大连接数(优先级中)
retries:健康检查。3次连接失败就认为服务不可用
option:服务不可用后的操作,重定向到其他健康服务器
contimeout :(重传计时器)定义haproxy将客户端!!!请求!!!转发至后端服务器,所等待的超时时长
clitimeout:(向后长连接)haproxy作为客户,和后端服务器之间!!!空闲连接!!!的超时时间,到时候发送fin指令
srvtimeout :(向前长连接)haproxy作为服务器,和用户之间空闲连接的超时时间,到时候发送fin指令
option abortonclose :当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
stats uri /admin?stats:设置统计页面的uri为/admin?stats
stats realm Private lands:设置统计页面认证时的提示内容
stats auth admin:password:设置统计页面认证的用户和密码,如果要设置多个,另起一行写入即可
stats hide-version:隐藏统计页面上的haproxy版本信息
frontend:前端配置块。面对用户侧
bind:面对用户监听地址和端口
mode:http模式的LB
log:日志使用全局配置
option httplog:默认日志格式非常简陋,仅包括源地址、目标地址和实例名称,而“option httplog参数将会使得日志格式变得丰富许多,其通常包括但不限于HTTP请求、连接计时器、会话状态、连接数、捕获的首部及cookie、“frontend”、“backend”及服务器名称,当然也包括源地址和端口号等。
option http close: 每次请求完毕后,关闭http通道
acl html url_reg -i \.html$ :1. 访问控制列表名称html。规则要求访问以html结尾的url时
use_backend html-server if html :2.如果满足acl html规则,则推送给后端服务器 html-server
default_backend html-server 3:默认的后端服务器是 html-server
backend html-server:后端服务器名称为 html-server
mode http:模式为7层代理
balance roundrobin:算法为轮训
option httpchk GET /index.html :允许用http协议检查server 的健康
cookie SERVERID insert indirect nocache:轮询的同时,根据插入的cookie SERVERID 的值来做会话保持,将相同的用户请求,转发给相同的真实服务器。
server html-A web1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5:cookie 3 服务器ID,避免rr算法将客户机请求转发给其他服务器 ,对后端服务器的健康状况检查间隔为2000毫秒,连续2次健康检查成功,则认为是有效的,连续5次健康检查失败,则认为服务器宕机
server html-B web2:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5
vim /etc/hosts
192.168.229.13 web1
192.168.229.14 web2
关于haproxy时间格式配置说明
一些包含了值的参数表示时间,如超时时长。这些值一般以毫秒为单位,但也可以使用其它的时间单位后缀。
us: 微秒(microseconds),即1/1000000秒;
ms: 毫秒(milliseconds),即1/1000秒;
s: 秒(seconds);
m: 分钟(minutes);
h:小时(hours);
d: 天(days);
systemctl start haproxy.service
cat /var/log/messages
查看日志信息
4.测试结果
-
linux客户端(192.168.229.11)
[root@client ~]# elinks --dump http://haproxy
web1
[root@client ~]# elinks --dump http://haproxy
web2
[root@client ~]# elinks --dump http://haproxy
web1
[root@client ~]# elinks --dump http://haproxy
web2
--dupm 以纯文本形式显示
- 配置域名解析
-
vim /etc/hosts
192.168.229.12 haproxy
-
windows客户端缓存问题,会导致看到的是同一个网站。
-
5.测试HAproxy状态
-
客户端浏览器输入http://haproxy的地址/haproxy
示例2
-
环境
-
Client
-
192.168.229.11/24 (真实机做客户端)
-
-
HAproxy
-
192.168.229.12/24
-
-
HTML A
-
192.168.229.13/24
-
-
HTML B
-
192.168.229.14/24
-
-
PHP 1
-
192.168.229.15/24
-
-
PHP 2
-
192.168.229.16/24
-
-
-
HTML A & HTML B
-
[root@localhost ~]# yum install httpd
-
分别创建测试页面 index.html ,开启服务
-
-
PHP1 & php2(192.168.229.15/24 ,192.168.229.16/24 )
-
[root@localhost ~]# yum install httpd php -y
-
分别创建测试页面 index.php ,开启服务
-
vim /var/www/html/index.php
-
php1
-
php1111111
-
-
php2
-
php222222
-
-
-
-
安装HAproxy(192.168.229.12)
-
# yum install epel-release -y
-
# yum install haproxy -y
-
-
修改配置文件(192.168.229.12)
-
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
-
######## 全局配置信息 #########
######参数是进程级的,通常和操作系统相关#######
global
log 127.0.0.1 local3 info #日志服务器
maxconn 4096 #最大连接数
uid nobody #用户身份
gid nobody #组身份
daemon #守护进程方式后台运行
nbproc 1 #工作进程数量
####### ###########默认设置 ###################
#####这些参数是配置 frontend,backend,listen 组的 ###########
defaults #这些参数可以被利用配置到frontend,backend,listen组件
log global
mode http #工作模式 http ,tcp 是 4 层,http是 7 层
maxconn 2048 #最大连接数
retries 3 #3 次连接失败就认为服务器不可用
option redispatch #如果 cookie 写入了 serverId 而客户端不会刷新 cookie,当serverId 对应的服务器挂掉后,强制定向到其他健康的服务器
stats uri /haproxy #使用浏览器访问 http://192.168.229.12/haproxy,可以看到服务器状态
stats auth wing:123 #用户认证,客户端使用elinks浏览器的时候不生效
contimeout 5000 #连接超时时间,单位毫秒ms
clitimeout 50000 #客户端超时
srvtimeout 50000 #服务器超时
frontend http-in
bind 0.0.0.0:80 #监听端口
mode http
log global
option httplog #日志类别 http 日志格式
option httpclose #打开支持主动关闭功能,每次请求完毕后主动关闭http通道 ,ha-proxy不支持keep-alive,只能模拟这种模式的实现
acl php url_reg -i \.php$ #acl <ACL名字> <类型> <大小写> <规则>
acl html url_reg -i \.html$ #use_backend <服务器组> if <ACL名字>
use_backend php-server if php
use_backend html-server if html
default_backend html-server #默认使用的服务器组
backend php-server
mode http
balance roundrobin #负载均衡的方式
option httpchk GET /index.php #健康检查
cookie SERVERID insert indirect nocache #客户端的 cookie 信息,允许插入serverid到cookie中,此处cookie号不同
server php-A 192.168.229.15:80 weight 1 cookie 1 check inter 2000 rise 2 fall 5
server php-B 192.168.229.16:80 weight 1 cookie 2 check inter 2000 rise 2 fall 5
#cookie 1 标识 serverid 为 1
#check inter 2000 检测心跳频率
#rise 2 2 次正确认为服务器可用
#fall 5 5 次失败认为服务器不可用
backend html-server
mode http
balance roundrobin
option httpchk GET /index.html
cookie SERVERID insert indirect nocache
server html-A 192.168.229.13:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
server html-B 192.168.229.14:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5
-
启动HAproxy
-
[root@haproxy ~]# systemctl restart haproxy.service
-
-
查看HAproxy状态
-
[root@localhost conf]# firefox http://localhost/haproxy
-
-
在客户端访问 HAproxy 测试
-
[root@localhost ~]# elinks --dump http://192.168.229.12
-
[root@localhost ~]# elinks --dump http://192.168.229.12/index.html
-
[root@localhost ~]# elinks --dump http://192.168.229.12/index.php
-
nginx
特点
1.功能强大,性能卓越,运行稳定。
2.配置简单灵活。
3.能够自动剔除工作不正常的后端服务器。
4.上传文件使用异步模式。
client---nginx---web1 web2 web3 lvs同步请求 DR client -->dr---web1
5.支持多种分配策略,可以分配权重,分配方式灵活。
优势
-
nginx复制用户请求,在后端服务器出现问题时。nginx会再复制一份请求发给另一台后端服务器。 lvs则在这种情况,只能用户重新发请求
-
缺点
-
流量会经过nginx,nginx成为瓶颈
-
-
生产架构
7层负载均衡语法示例
-
通过location对不同的请求进行进行负载均衡,因为通过http协议,所以称作7层
location / {
}
location ~ \.html${
proxy_pass ...
}
location ~ \.php${
proxy_pass ...
}
location ~ \.(jpg|png|css|js)${
proxy_pass ...
}
nginx负载均衡算法
1.round robin(默认)
-
轮询方式,依次将请求分配到各个后台服务器中,默认的负载均衡方式。 适用于后台机器性能一致的情况。 挂掉的机器可以自动从服务列表中剔除。
2.weight
-
根据权重来分发请求到不同的机器中,指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
-
例如:
例如:
upstream bakend {
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}
3.IP_hash
-
根据请求者ip的hash值将请求发送到后台服务器中,可以保证来自同一ip的请求被打到固定的机器上,可以解决session问题。
-
例如:
例如:
upstream bakend {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}
4.url_hash(第三方)
-
根据请求的url的hash值将请求分到不同的机器中,当后台服务器为缓存的时候效率高。
-
需要额外安装 nginx的upstream_hash模块
-
在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法 复制代码
upstream backend {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
5.fair(第三方)
-
根据后台响应时间来分发请求,响应时间短的分发的请求多。
-
例如:
upstream backend {
server server1;
server server2;
fair;
}
-
每个设备的状态设置为:
-
1.down 表示单前的server暂时不参与负载
-
2.weight 默认为1.weight越大,负载的权重就越大。
-
3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
-
4.fail_timeout:max_fails次失败后,暂停的时间。
-
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
-
nginx支持同时设置多组的负载均衡,用来给不用的server来使用。
-
client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug
-
client_body_temp_path 设置记录文件的目录 可以设置最多3层目录
-
location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡
-
-
环境
-
client:client
-
nginx:nginx
-
apache:web1
-
apache:web2
-
案例
1.关闭防火墙和selinux(略)
2.域名解析
3.同步时间(实验环境略)
-
计划任务执行如下命令
-
ntpdate 时间服务器地址
4.准备web1页面
-
<font color='red'>5.配置nginx(192.168.229.12)</font>
-
yum install -y epel-release
-
如果用的是之前的环境( yum remove haproxy -y)
-
yum install -y nginx
-
<font color='red'>vim /etc/nginx/ngxin.conf</font>
-
注意IP地址
-
-
http {
upstream html {
server web1:80;
server web2:80;
}
server {location / {
proxy_pass http://html;
}
}
}
6.测试负载均衡
-
访问nginx服务器。能交替的看到web1和web2
7.动静分离(略)
-
1 增加一个upstream php server web3
-
2.增加一个location ~ .php {}
-
3.示例
upstream html {
server web1:80;
server web2:80;
}
upstream php {
server web3:80;
server web4:80;
}
server {
location / {
proxy_pass http://html;
}
location ~ \.php$ {
proxy_pass http://php;
}
}
http {
upstream html {
server 192.168.229.13:80;
server 192.168.229.14:80;
}
upstream php {
server 192.168.229.15:80;
server 192.168.229.16:80;
}
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
location / {
proxy_pass http://html;
}
location ~ \.php$ {
proxy_pass http://php;
}
}
重启nginx
systemctl restart nginx
4.客户端测试:
[root@client ~]# elinks --dump http://192.168.229.12/index.html
web11111
[root@client ~]# elinks --dump http://192.168.229.12/index.html
web22222
[root@client ~]# elinks --dump http://192.168.229.12/index.php
php111111
[root@client ~]# elinks --dump http://192.168.229.12/index.php
php2222222