HAProxy的下载网址:Releases · haproxy/haproxy · GitHub
HAProxy是一个高性能的开源负载均衡器和代理服务器,使用C语言编写,提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。其特别适用于负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy可以运行在当前的硬件上,支持数以万计的并发连接,并且能够很简单安全地整合进用户当前的架构中,同时保护web服务器不被暴露到网络上。
HAProxy的工作原理 :当客户端发起连接请求时,它将连接到HAProxy提供的IP地址和端口。HAProxy能够将传入的请求分发到多个后端服务器,并提供各种负载均衡算法,如轮询、加权轮询、最少连接等。同时,HAProxy具有高度可配置性和可定制性,适用于Web应用、数据库负载均衡、应用程序代理等场景,提供高可用性和可伸缩性。
haproxy---主要是做7层负载均衡,也可以做4层负载均衡
apache也可以做7层负载均衡,但是很麻烦。实际工作中没有人用。
负载均衡是通过OSI协议对应的
7层负载均衡:用的7层http协议,
4层负载均衡:用的是tcp协议加端口号做的负载均衡
ha-proxy的特点:
实验环境:Keepalived + Haproxy
192.168.137.10 | haproxy-master | |
192.168.137.20 | haproxy-backup | |
192.168.137.30 | server01 | |
192.168.137.40 | server02 |
所有主机:
- # 四台服务器正常上网,互相可以ping通,关闭防火墙,修改主机名
- ping www.jd.com
- ping 192.168.137.10
- ping 192.168.137.20
- ping 192.168.137.30
- ping 192.168.137.40
- hostnamectl set-hostname 主机名
- systemctl disable --now firewalld
- setenforce 0
真实服务器配置:
- # 安装 nginx
- yum install -y nginx
- systemctl start nginx
- #
- echo "test-nginx01" > /usr/share/nginx/html/index.html
- echo "test-nginx02" > /usr/share/nginx/html/index.html
- vim /etc/nginx/nginx.conf
- keepalive_timeout 65;
- systemctl restart nginx
调度器配置Haproxy(主/备):
- yum -y install haproxy keepalived
- # 备份:
- cp -rf /etc/haproxy/haproxy.cfg{,.bak}
- # 修改配置文件
- 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
- nbproc 1
- defaults
- mode http
- log global
- retries 3
- option redispatch
- maxconn 4000
- contimeout 5000
- clitimeout 50000
- srvtimeout 50000
- listen stats
- bind *:81
- stats enable
- stats uri /haproxy
- stats auth qianfeng:123
- frontend web
- mode http
- bind *:80
- option httplog
- acl html url_reg -i \.html$
- use_backend httpservers if html
- default_backend httpservers
- backend httpservers
- balance roundrobin
- server http1 192.168.137.30:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
- server http2 192.168.137.40:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
-
- # 将配置文件拷贝到slave服务器
- scp /etc/haproxy/haproxy.cfg 192.168.137.20:/etc/haproxy/
- systemctl start haproxy
- systemctl enable haproxy
master 服务器:
- # 备份配置文件
- cp /etc/keepalived/{keepalived.conf,keepalived.conf.bak}
- vim /etc/keepalived/keepalived.conf
- #
- ! Configuration File for keepalived
-
- global_defs {
- router_id master01
- }
- 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.137.100/24
- }
- }
backup 服务器:
- # 备份配置文件
- cp /etc/keepalived/{keepalived.conf,keepalived.conf.bak}
- vim /etc/keepalived/keepalived.conf
- #
- ! Configuration File for keepalived
-
- global_defs {
- router_id backup01
- }
- 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.137.100/24
- }
- }
调度器配置Haproxy(主/备)2:
- systemctl start keepalived
- systemctl enable keepalived
- ip a
扩展:对调度器Haproxy健康检查(可选)
思路:两台机器都做
- # 让 Keepalived 每隔一定时间执行脚本,脚本的功能是当Haproxy失败,则关闭本机的 Keepalived
- vim /etc/keepalived/check_haproxy_status.sh
- # 编写脚本
- #!/bin/bash
-
- /usr/bin/curl -I http://localhost &>/dev/null
- if [ $? -ne 0 ];then
- # /etc/init.d/keepalived stop
- systemctl stop keepalived
- fi
-
- # 给脚本添加权限
- chmod a+x /etc/keepalived/check_haproxy_status.sh
- # keepalived 使用脚本
- vim /etc/keepalived/keepalived.conf
- #
- ! Configuration File for keepalived
-
- global_defs {
- router_id master01
- }
-
- vrrp_script check_haproxy {
- script "/etc/keepalived/check_haproxy_status.sh"
- interval 5
- }
-
- 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.137.100/24
- }
- track_script {
- check_haproxy
- }
- }
-
- # backup机器也需要:
- vim /etc/keepalived/keepalived.conf
- #
- ! Configuration File for keepalived
-
- global_defs {
- router_id backup02
- }
- vrrp_script check_haproxy {
- script "/etc/keepalived/check_haproxy_status.sh"
- interval 5
- }
-
- 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.137.100/24
- }
- track_script {
- check_haproxy
- }
- }
-
- # 注:必须先启动haproxy,再启动keepalived
- systemctl restart keepalived
- ip a
- # VIp在master上,关闭master的keepalived,发现VIP漂移到了backup上。
两台负载均衡机器都配置 haproxy 的日志:
- vim /etc/rsyslog.conf
- # 需要打开注释并添加
- # Provides UDP syslog reception #由于haproxy的日志是用udp传输的,所以要启用rsyslog的udp监听
- $ModLoad imudp -----UDP协议
- $UDPServerRun 514 -----514端口
- 找到 #### RULES #### 下面添加
- local2.* /var/log/haproxy.log
- # 参数解释:
- # $ModLoad imudp:ModLoad加载指定模块。imudp:模块名称,用于从 UDP 网络连接中接收日志消息。
- # local2 日志消息的一个分类标识符。在此配置中,local2可能与HAProxy的日志配置相关联。* 是优先级选择器,表示所有优先级的日志消息(从最紧急的emerg到最不重要的debug)都要被记录。
-
- systemctl restart rsyslog
- systemctl restart haproxy
- tail -f /var/log/haproxy.log
- global
- log 127.0.0.1 local2 info
- pidfile /var/run/haproxy.pid
- maxconn 4000
- user haproxy
- group haproxy
- daemon
- nbproc 1
log 127.0.0.1 local2 info:
pidfile /var/run/haproxy.pid:
maxconn 4000:
user haproxy 和 group haproxy:
daemon:
nbproc 1:
- defaults
- mode http
- log global
- retries 3
- option redispatch
- maxconn 4000
- contimeout 5000
- clitimeout 50000
- srvtimeout 50000
mode http:
log global:
retries 3:
option redispatch:
maxconn 4000:
contimeout 5000:
clitimeout 50000:
srvtimeout 50000:
- listen stats
- bind *:81
- stats enable
- stats uri /haproxy
- stats auth qianfeng:123
bind *:81:
stats enable:
stats uri /haproxy:
stats auth qianfeng:123:
- frontend web
- mode http
- bind *:80
- option httplog
- acl html url_reg -i \.html$
- use_backend httpservers if html
- default_backend httpservers
frontend web:
mode http:
bind *:80:
option httplog:
acl html url_reg -i .html$:
use_backend httpservers if html:
default_backend httpservers:
- backend httpservers
- balance roundrobin
- server http1 192.168.137.30:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
- server http2 192.168.137.40:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
backend httpservers:
balance roundrobin:
server http1 192.168.246.162:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2:
- # 两台real server安转mariadb mariadb-server
- [root@real-server ~]# yum install -y mariadb-server
- # 启动mariadb
- [root@real-server ~]# systemctl enable --now mariadb
- # 连接数据库
- [root@real-server ~]# mysql -uroot -p
- MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by '123456';
- MariaDB [(none)]> flush privileges;
- MariaDB [(none)]> exit;
-
- 两台haproxy配置文件:
- [root@ha-proxy-master ~]# cat /etc/haproxy/haproxy.cfg
- Haproxy L4
- ===================================================================================
- global
- log 127.0.0.1 local2
- pidfile /var/run/haproxy.pid
- maxconn 4000
- user haproxy
- group haproxy
- daemon
- nbproc 1
- defaults
- mode http
- log global
- option redispatch
- retries 3
- maxconn 4000
- contimeout 5000
- clitimeout 50000
- srvtimeout 50000
- listen stats
- bind *:81
- stats enable
- stats uri /haproxy
- stats auth qianfeng:123
- frontend web
- mode http
- bind *:80
- option httplog
- default_backend httpservers
- backend httpservers
- balance roundrobin
- server http1 192.168.246.162:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
- server http2 192.168.246.163:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
- # 添加已下字段
- listen mysql
- bind *:3306
- mode tcp
- balance roundrobin
- server mysql1 192.168.246.163:3306 weight 1 check inter 1s rise 2 fall 2
- server mysql2 192.168.246.162:3306 weight 1 check inter 1s rise 2 fall 2
- inter表示健康检查的间隔,单位为毫秒 可以用1s等,fall代表健康检查失败2回后放弃检查。
- rise代表连续健康检查成功2此后将认为服务器可用。
- 默认的,haproxy认为服务时永远可用的,除非加上check让haproxy确认服务是否真的可用。
- 找一台机器做为客户端去测试,在测试的时候注意mysql的远程登录权限