ha-proxy是一款高性能的负载均衡软件。因为其专注于负载均衡这一件事情,因此与nginx比起来在负载均衡这件事情上做的更好,更专业。
ha-proxy的特点:
•支持tcp/http两种协议层的负载均衡,使得其负载均衡功能非常丰富。
•支持8种左右的负载均衡算法,尤其是在http模式时,有许多非常实在的负载均衡算法,适用各种需求。
•性能非常优秀,基于单进程处理模式(和Nginx类似)让其性能卓越。
•拥有一个功能出色的监控页面,实时了解系统的当前状况。
•功能强大的ACL支持,给用户极大的方便
拓扑结构:
ha-proxy-master:ip-192.168.111.4 vip-192.168.111.66
ha-proxy-slave:ip-192.168.111.7
test-nginx1:ip-192.168.111.8
test-nginx2:ip-192.168.111.9
关闭防火墙与selinux:systemctl stop firewalld && setenforce 0
准备工作(集群中所有主机)
# cat /etc/hosts
127.0.0.1 localhost
192.168.111.4 ha-proxy-master
192.168.111.7 ha-proxy-slave
192.168.111.8 test-nginx1
192.168.111.9 test-nginx2
所有RS:
# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
# yum -y install nginx
# systemctl start nginx
# echo "test-nginx1" >> /usr/share/nginx/html/index.html #nginx-1的界面
# echo "test-nginx2" >> /usr/share/nginx/html/index.html #nginx-2的界面
master
# yum -y install haproxy
# cp -rf /etc/haproxy/haproxy.cfg{,.bak}
# sed -i -r '/^[ ]*#/d;/^$/d' /etc/haproxy/haproxy.cfg
# vim /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://192.168.111.4:81/haproxy,可以看到服务器状态
stats auth zhao: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 192.168.111.8:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
server http2 192.168.111.9:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
# scp /etc/haproxy/haproxy.cfg 192.168.111.7:/etc/haproxy/
开启:systemctl start haproxy
现在可以先用浏览器访问一下192.168.111.4:81/haproxy与192.168.111.7:81/haproxy
现利用keepalived实现调度器HA
master:
# yum install -y keepalived
# mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id director1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 80
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.111.66/24
}
}
# systemctl start keepalived
# systemctl enable keepalived
slave:
# yum install -y keepalived
# mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
# vim /etc/keepalived/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 {
192.168.111.66/24
}
}
# systemctl start keepalived
# systemctl enable keepalived
注:必须先启动haproxy,再启动keepalived