1、HAProxy服务器
1.1、概述
- 它是免费、快速并且可靠的一种解决方案
- 适用于那些负载特别大的web站点,这些站点通常有需要会话保持或七层处理
- 提供高可用性、负载均衡以及基于TCP和HTTP应用的代理
- Session concurrency 并发会话数
1.2、配置HAProxy负载平衡集群
1.2.1、实验规划
- 准备4台Linux服务器,两台做Web服务器,1台安装HAProxy,1台做客户端,实现如下功能:
- 客户端访问HAProxy,HAProxy分发请求到后端Real Server
- 开启HAProxy监控页面,及时查看调度器状态
- 设置HAProxy为开机启动
主机名
|
网络配置
|
client
|
eth0:192.168.4.10/24
|
proxy
|
eth0:192.168.4.5/24
eth1:192.168.2.100/24
|
web1
|
eth1:192.168.2.100/24
|
web2
|
eth1:192.168.2.200/24
|
- Hapoxy是代理服务器(帮你干活的人或物就是你的代理)
1.2.2、实验环境配置
- web1配置本地真实IP地址
[root@web1 ~]# nmcli connection modify eth1 ipv4.method manual ipv4.addresses 192.168.2.100/24 connection.autoconnect yes
[root@web1 ~]# nmcli connection up eth1
- Web2配置本地真实IP地址
[root@web2 ~]# nmcli connection modify eth1 ipv4.method manual ipv4.addresses 192.168.2.200/24 connection.autoconnect yes
[root@web2 ~]# nmcli connection up eth1
- proxy关闭keepalived服务,清理LVS规则
[root@proxy ~]# systemctl stop keepalived
[root@proxy ~]# systemctl disable keepalived
[root@proxy ~]# ipvsadm -C
[root@proxy ~]# nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.4.5/24 connection.autoconnect yes
[root@proxy ~]# nmcli connection up eth0
[root@proxy ~]# nmcli connection modify eth1 ipv4.method manual ipv4.addresses 192.168.2.5/24 connection.autoconnect yes
[root@proxy ~]# nmcli connection up eth1
1.2.3、后端web服务器配置
- 安装软件,自定义Web页面(web1和web2主机)
[root@web1 ~]# yum -y install httpd
[root@web1 ~]# echo "192.168.4.100,我是web1" > /var/www/html/index.html
[root@web2 ~]# yum -y install httpd
[root@web2 ~]# echo "192.168.4.200,我是web2" > /var/www/html/index.html
- 启动Web服务器软件(web1和web2主机)
[root@web1 ~]# systemctl start httpd ; systemctl enable httpd
[root@web2 ~]# systemctl start httpd ; systemctl enable httpd
1.2.4、部署HAProxy服务器
- 安装软件
[root@proxy ~]# yum -y install haproxy
- 修改配置文件
[root@proxy ~]# vim /etc/haproxy/haproxy.cfg #在配置文件的最后添加
listen websrv-rewrite *:80 #也可以写成 0.0.0.0:80
balance roundrobin
server web1 192.168.2.100:80 check inter 2000 rise 2 fall 5
server web2 192.168.2.200:80 check inter 2000 rise 2 fall 5
#定义集群,listen后面的名称任意,端口为80
#balance指定调度算法为轮询(不能用简写的rr)
#server指定后端真实服务器,web1和web2的名称可以任意
#check代表健康检查,inter设定健康检查的时间间隔,rise定义成功次数,fall定义失败次数
listen stats *:1080 #监听端口
stats refresh 30s #统计页面自动刷新时间
stats uri /stats #统计页面url
stats realm Haproxy Manager #进入管理解面查看状态信息
stats auth admin:admin #统计页面用户名和密码设置
- 启动服务器并设置开机启动
[root@proxy ~]# systemctl restart haproxy
[root@proxy ~]# systemctl enable haproxy
- 默认配置部分解析
[root@proxy ~]# cat /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http:#haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid #haproxy的pid存放路径
maxconn 4000
user haproxy
group haproxy
daemon #以后台进程的方式启动服务
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http #默认的模式mode { tcp|http|health }
log global
option httplog #日志类别http日志格式
option dontlognull #不记录健康检查的日志信息
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch #当某个服务器挂掉后强制定向到其他健康服务器
retries 3 #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 #最大连接数
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:5000
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
use_backend static if url_static
default_backend app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
server static 127.0.0.1:4331 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance roundrobin
server app1 127.0.0.1:5001 check
server app2 127.0.0.1:5002 check
server app3 127.0.0.1:5003 check
server app4 127.0.0.1:5004 check
1.2.5、客户端验证
[root@client ~]# curl 192.168.4.5
192.168.4.100,我是web1
[root@client ~]# curl 192.168.4.5
192.168.4.200,我是web2
[root@client ~]# curl 192.168.4.5
192.168.4.100,我是web1
[root@client ~]# curl 192.168.4.5
192.168.4.200,我是web2
http:#192.168.4.5:1080/stats测试状态监控页面是否正常
使用配置文件中设置的用户名密码登录
可以使用ab工具做测试
- Cur(当前队列数量)
- Max(最大值)
- Limit(队列限制数量)
- Session rate每秒会话率(当前值,最大值,限制数量);
- Sessions总会话量(当前值,最大值,总量,Lbtot: total number of times a server was selected选中一台服务器所用的总时间);
- Bytes(入站、出站流量);
- Denied(拒绝请求、拒绝回应);
- Errors(错误请求、错误连接、错误回应);
- Warnings(重新尝试警告retry、重新连接redispatches);
- Server(状态、最后检查的时间(多久前执行的最后一次检查)、权重、备份服务器数量、down机服务器数量、down机时长)。
2、集群调度软件对比
Nginx分析
- 工作在7层,可以针对http做分流策略
- 1.9版本开始支持4层代理
- 正则表达式比HAProxy强大
- 安装、配置、测试简单,通过日志可以解决多数问题
- 并发量可以达到几万次
- Nginx还可以作为web服务器使用
- 7层代理仅支持http、https、mail协议,应用面小
- 监控检查仅通过端口,无法使用URL检查
LVS分析
- 负载能力强,工作在4层、对内存、CPU消耗低
- 配置性低,没有太多的配置,减少了认为的错误
- 应用面广,几乎可以为所有应用提供负载均衡
- 不支持正则表达式,不能实现动静分离
- 如果网站架构庞大,LVS-DR配置比较繁琐
HAProxy分析
- 支持session、cookie功能
- 可以通过URL进行健康检查
- 效率、负载均衡速度,高于Nginx,低于LVS
- HAProxy支持TCP,可以对MySQL进行负载均衡
- 调度算法丰富
对比总结
LVS>HAProxy>Nginx
Nginx>HAProxy>LVS