负载均衡是通过OSI协议对应的
7层负载均衡:用的7层http协议
4层负载均衡:用的是tcp协议加端口号做的负载均衡
ha-proxy是一款高性能的负载均衡软件。因为其专注于负载均衡这一些事情,因此与nginx比起来在负载均衡这件事情上做的更好,更专业。
ha-proxy 作为目前流行的负载均衡软件,必须有其出色的一面。下面介绍一下ha-proxy负载均衡软件的优点。
•支持tcp / http 两种协议层的负载均衡,使得其负载均衡功能非常丰富。
•支持多种负载均衡算法,尤其是在http模式时,有许多非常实在的负载均衡算法,适用各种需求。
•性能非常优秀,基于单进程处理模式(和Nginx类似)让其性能卓越。
•拥有一个功能出色的监控页面,实时了解系统的当前状况。
•功能强大的ACL支持,给用户极大的方便。
haproxy三种算法:
1.roundrobin
基于权重进行轮询,在服务器的处理时间保持均匀分布时,这是最平衡,最公平的算法.此算法是动态的,这表示其权重可以在运行时进行调整.
2.static-rr
基于权重进行轮询,与roundrobin类似,但是为静态方法,在运行时调整其服务器权重不会生效.不过,其在后端服务器连接数上没有限制
3.leastconn
新的连接请求被派发至具有最少连接数目的后端服务器.
准备四台虚拟机,两台做代理服务器,两台做真实服务器(真实服务器只是用来进行web测试)
1、选择两台Haproxy服务器作为代理服务器(一台master 一台backup)。
真实服务器需要nginx来提供web服务进行测试
2、给两台代理服务器安装keepalived制作高可用生成VIP
3、配置nginx的负载均衡,以上两台nginx服务器配置文件一致
10.0.0.10 master #主节点
10.0.0.11 backup #备节点
10.0.0.12 server01 #第一台真实服务器
10.0.0.13 server02 #第二台真实服务器
所有虚拟主机执行
vim /etc/hosts
10.0.0.10 master
10.0.0.11 backup
10.0.0.12 server01
10.0.0.13 server02
10.0.0.12 server01 、10.0.0.13 server02执行
[root@server01 ~]# systemctl stop firewalld && setenforce 0
[root@server01 ~]# cd /etc/yum.repos.d/
[root@server01 yum.repos.d]# cat nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
[root@server01 yum.repos.d]# yum install yum-utils nginx -y
[root@server01 yum.repos.d]# vim /etc/nginx/nginx.conf
keepalive_timeout 0; #设置长链接
[root@server01 yum.repos.d]# systemctl start nginx #启动nginx
#server01执行
echo "this is first real-server01" > /usr/share/nginx/html/index.html #方便区分,看出效果
#server02执行
echo "this is first real-server02" > /usr/share/nginx/html/index.html #方便区分,看出效果
[root@master ~]# yum -y install haproxy
[root@master ~]# cp -rf /etc/haproxy/haproxy.cfg{,.bak} #备份
#master配置
[root@master ~]# cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2 info
pidfile /var/run/haproxy.pid
maxconn 4000 #优先级低
user haproxy
group haproxy
daemon #以后台形式运行ha-proxy
nbproc 1 #工作进程数量 cpu内核是几就写几
defaults
mode http #工作模式 http ,tcp 是 4 层,http是 7 层
log global
retries 3 #健康检查。3次连接失败就认为服务器不可用,主要通过后面的check检查
option redispatch #服务不可用后重定向到其他健康服务器。
maxconn 4000 #优先级中
contimeout 5000 #ha服务器与后端服务器连接超时时间,单位毫秒ms
clitimeout 50000 #客户端超时
srvtimeout 50000 #后端服务器超时
listen stats
bind *:81
stats enable
stats uri /haproxy #使用浏览器访问 http://10.0.0.10:81/haproxy,可以看到服务器状态
stats auth yjssjm:123 #用户认证,客户端使用elinks浏览器的时候不生效
frontend web
mode http
bind *:80 #监听哪个ip和什么端口
option httplog #日志类别 http 日志格式
acl html url_reg -i \.html$ #1.访问控制列表名称html。规则要求访问以html结尾的url(可选)
use_backend httpservers if html #2.如果满足acl html规则,则推送给后端服务器httpservers
default_backend httpservers #默认使用的服务器组
backend httpservers #名字要与上面的名字必须一样
balance roundrobin #负载均衡的方式
server http1 10.0.0.12:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
server http2 10.0.0.13:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
#backup配置
[root@master ~]# cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2 info
pidfile /var/run/haproxy.pid
maxconn 4000 #优先级低
user haproxy
group haproxy
daemon #以后台形式运行ha-proxy
nbproc 1 #工作进程数量 cpu内核是几就写几
defaults
mode http #工作模式 http ,tcp 是 4 层,http是 7 层
log global
retries 3 #健康检查。3次连接失败就认为服务器不可用,主要通过后面的check检查
option redispatch #服务不可用后重定向到其他健康服务器。
maxconn 4000 #优先级中
contimeout 5000 #ha服务器与后端服务器连接超时时间,单位毫秒ms
clitimeout 50000 #客户端超时
srvtimeout 50000 #后端服务器超时
listen stats
bind *:81
stats enable
stats uri /haproxy #使用浏览器访问 http://10.0.0.11:81/haproxy,可以看到服务器状态
stats auth yjssjm:123 #用户认证,客户端使用elinks浏览器的时候不生效
frontend web
mode http
bind *:80 #监听哪个ip和什么端口
option httplog #日志类别 http 日志格式
acl html url_reg -i \.html$ #1.访问控制列表名称html。规则要求访问以html结尾的url(可选)
use_backend httpservers if html #2.如果满足acl html规则,则推送给后端服务器httpservers
default_backend httpservers #默认使用的服务器组
backend httpservers #名字要与上面的名字必须一样
balance roundrobin #负载均衡的方式
server http1 10.0.0.12:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
server http2 10.0.0.13:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
#启动haproxy(master和backup都执行)
[root@master ~]# systemctl start haproxy
[root@master ~]# systemctl status haproxy
[root@master ~]# curl 10.0.0.10
this is second real-server01
[root@master ~]# curl 10.0.0.10
this is second real-server02
浏览器访问:10.0.0.10:81/haproxy 或者访问10.0.0.11:81/haproxy都可以
#master和backup都执行
#因为我们在haproxy配置中配置的是log 127.0.0.1 local2 info
[root@master ~]# vim /etc/rsyslog.conf
#打开tcp和udp的接收参数
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514
#我添加在了62行
local2.* /var/log/haproxy.log
#重启日志服务
[root@master ~]# systemctl restart rsyslog
#重启haproxy
[root@master ~]# systemctl restart haproxy
[root@master ~]# cd /var/log/
[root@master log]# ll haproxy.log
-rw------- 1 root root 688 May 4 15:32 haproxy.log
master和backup执行
#安装keepalived服务
yum install keepalived -y
cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak
#master中keepalived配置文件
[root@master keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id directory1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
nopreempt
virtual_router_id 80
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.100/24
}
}
#backup中keepalived配置文件
[root@backup keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id directory2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
nopreempt
virtual_router_id 80
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.100/24
}
}
#启动keepalived服务
systemctl start keepalived
systemctl status keepalived
#查看VIP是不是在master中(10.0.0.100)
[root@master keepalived]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:74:48:45 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.10/24 brd 10.0.0.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 10.0.0.100/24 scope global secondary ens33
valid_lft forever preferred_lft forever
inet6 fe80::20e1:3a6c:cce2:313f/64 scope link noprefixroute
valid_lft forever preferred_lft forever
#停掉master中keepalived,看VIP是否在backup服务器中
[root@backup keepalived]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:8b:59:2b brd ff:ff:ff:ff:ff:ff
inet 10.0.0.11/24 brd 10.0.0.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 10.0.0.100/24 scope global secondary ens33
valid_lft forever preferred_lft forever
inet6 fe80::91cf:93df:680b:3436/64 scope link noprefixroute
valid_lft forever preferred_lft forever
用VIP地址10.0.0.100访问