一、haproxy简介
HAProxy是法国开发者威利塔罗在2000年使用c语言开发的一个开源软件,是一款具备高并发、高性能的TCP和HTTP负载均衡器
支持cookie的持久性,自动故障切换,支持正则表达式及web状统计
二、HAProxy的安装
1.环境搭建
1.准备三台虚拟机:
haproxy(172.25.254.100)、webserver1(172.25.254.10)、webseerver2(172.25.254.20)
关闭所有防火墙!!!
2.首先在webserver1和webserver2上安装需要的软件包
webserver1:
[root@webserver1 ~]# dnf install nginx
[root@webserver1 ~]# echo webserver1 - 172.25.254.10 > /usr/share/nginx/html/index.html
[root@webserver1 ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@webserver1 ~]# systemctl stop firewalld
webserver2:
[root@webserver2 ~]# dnf install nginx
[root@webserver2 ~]# echo webserver2 - 172.25.254.20 > /usr/share/nginx/html/index.html
[root@webserver2 ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@webserver2 ~]# systemctl stop firewalld
3.haproxy测试:
[root@haproxy ~]# curl 172.25.254.10
webserver1 - 172.25.254.10
[root@haproxy ~]# curl 172.25.254.20
webserver2 - 172.25.254.20
至此环境搭建成功
2.haproxy的基本部署方法及负载均衡的实现
2.1安装环境
安装haporxy
[root@haproxy ~]# dnf install haproxy -y
2.2查看配置文件
[root@haproxy ~]# rpm -qc haproxy
/etc/haproxy/haproxy.cfg ------配置文件
/etc/logrotate.d/haproxy ------日志文件
/etc/sysconfig/haproxy
gloabl:全局配置
进程及安全配置相关参数
性能调整相关参数
Debug参数
proxies:代理配置
dafeults:为frontend,backend,listen提供默认配置
frontend:前端
backend:后端
listen:同时拥有前端和后端配置,配置简单,推荐使用
2.3配置文件
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
两种方式:
1.分开前端后端
2.合并一起
写完后重启服务
[root@haproxy ~]# systemctl restart haproxy.service
如果重启报错,排错方法:
[root@haproxy ~]# systemctl status haproxy
○ haproxy.service - HAProxy Load Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: di>
Active: inactive (dead)
[root@haproxy ~]# systemctl start haproxy.service
Job for haproxy.service failed because the control process exited with error code.
See "systemctl status haproxy.service" and "journalctl -xeu haproxy.service" for details.
[root@haproxy ~]# journalctl -xeu haproxy.service
修改完成后再次重启,没有报错即为成功
2.4测试
两种方式检测结果相同
2.5后端检测
关闭一台
[root@webserver1 ~]# systemctl stop nginx.service
测试:
3.haproxy的全局配置参数及日志分离
3.1global全局配置
3.1.1global配置:进程数量
global配置参数
多进程设置:
初始
进程数设置为2
修改后重新启动
cpu核心
第一个进程用第一个核心
第二个进程用第二个核心
查看线程:
子进程中只有一个线程
不能同时设定进程(nbproc)和线程(nbthread),会互斥
3.1.2自定义日志
日志为local2
在 /etc/rsyslog.conf 中:
打开UDP
将日志定义在local2中
查看日志:
总结代码:
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
[root@haproxy ~]# vim /etc/rsyslog.conf
[root@haproxy ~]# systemctl restart rsyslog.service
[root@haproxy ~]# ll /var/log/haproxy.log
ls: 无法访问 '/var/log/haproxy.log': 没有那个文件或目录
[root@haproxy ~]# systemctl restart haproxy.service
[root@haproxy ~]#
Message from syslogd@localhost at Aug 8 16:42:22 ...
haproxy[2120]: backend static has no server available!
Message from syslogd@localhost at Aug 8 16:42:22 ...
haproxy[2121]: backend static has no server available!
Message from syslogd@localhost at Aug 8 16:42:24 ...
haproxy[2120]: backend app has no server available!
Message from syslogd@localhost at Aug 8 16:42:24 ...
haproxy[2121]: backend app has no server available!
^C
[root@haproxy ~]# ll /var/log/haproxy.log
-rw------- 1 root root 2718 8月 8 16:42 /var/log/haproxy.log
3.2proxies配置
3.2.1weight
server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2 server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
3.2.2backup
backup --- sorryserver 的端囗(80已经被占用)
[root@haproxy ~]# yum install httpd -y
[root@haproxy ~]# vim /etc/httpd/conf/httpd.conf
[root@haproxy ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/sy
[root@haproxy ~]# echo sorry 下班了 > /var/www/html/index.html
[root@haproxy ~]# echo sorry 下班了 > /var/www/html/index.html
在浏览器访问 172.25.254.100:8080
3.2.3disabled
指定realserver下线
检测,webserver1 - 172.25.254.10下线
3.2.4 redirect
3.2.5maxconn 最大连接数
3.3socat工具
对服务器动态权重和其他状态可以利用socat工具进行调整,socat是Linux下的一个多功能的网络工具,名字由来是Socket CAT,相当于netCAT的增强版。Socat的主要特点是在两个数据流之间建立双向通道,且支持众多协议和链接方式。如:IP、TCP、UDP等。
用socat对服务动态权重调整:
[root@haproxy ~]# dnf install socat -y
[root@haproxy ~]# echo "show servers state" | socat stdio /var/lib/haproxy/stats
[root@haproxy ~]# echo get weight webcluster/web1 | socat stdio /var/lib/haproxy/stats
2 (initial 2)
[root@haproxy ~]# echo set "weight webcluster/web1 1" | socat stdio /var/lib/haproxy/stats
[root@haproxy ~]# echo get weight webcluster/web1 | socat stdio /var/lib/haproxy/stats
1 (initial 2)
[root@haproxy ~]# echo set "weight webcluster/web1 2" | socat stdio /var/lib/haproxy/stats
[root@haproxy ~]# echo "disable server webcluster/web1 " | socat stdio /var/lib/haproxy/stats
[root@haproxy ~]# echo "enable server webcluster/web1 " | socat stdio /var/lib/haproxy/stats
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
[root@haproxy ~]# systemctl restart haproxy
查看权重,修改权重
haproxy多线程如何热处理
4.haproxy的算法
4.1静态算法
static-rr
不支持运行时利用socat进行权重的动态调整
不支持端服务器慢启动
first
在多台主机中执行死循环测试效果
4.2动态算法
1.基于后端服务器状态进行适度调整
2.新请求将有限调整调度至当前负载较低的服务器
3.权重可以在haproxy运行时动态状态无需重启
4.2.1 roundrobin
1.基于权重的轮询动态调度算法
2.支持权重的运行时调整
3.支持慢启动
示例:
4.2.2 leastconn
4.3其他算法
其他算法既可以作为静态算法,也可以通过选项成为动态算法
source
uri
url_param
hdr
针对每个用户头部请求中的指定信息做hash
算法总结
静态算法 | 动态算法 | 其他算法(取决于hsah_type是否consistent) |
---|---|---|
static-rr | roundrobin | source |
first | leastconn | uri |
random | url_param | |
hdr |
5.haproxy状态页
通过web界面,显示当前Haproxy的运行状态
5.1状态页配置:
浏览器测试
6.基于cookie的会话保持
配置选项
cookie WEBCOOKIE(自己给cookie起的name)
insert 插入新的cookie
172.25.254.10:80 名字为cookie wang1
172.25.254.20:80 名字为cookie wang2
6.1验证cookie信息
浏览器查看,信息一致
7. IP透传
web服务器中需要记录客户端的真是ip地址,用于做访问统计,安全防护,行为分析,区域排行等场景
7.1七层透传
协议+内容交换
当haproxy工作在七层时,也可以透传真实ip至后端服务器
当未开启传透时:
透过web访问
后端无法看到真实ip
当开启透传时:
web访问
可以看到真实ip
7.2四层透传
IP+PROT转发
8.ACL
访问控制列表,是哟中基于包过滤的访问控制技术。他可以根据设定的条件对经过服务器的数据包进行过滤即可收到报文进行匹配和过滤,基于请求报文头部中的源地址,目标地址,目标端口,请求方法,URL,文件后缀等信息进行匹配并执行进一步操作。
ACL配置选项
acl <aclname> <criterion> [flags] [operator] [<value>]
acl 名称 匹配规范 匹配模式 具体操作符 操作对象类型
8.1 ACL-name
acl test hdr_dom(host) -i www.wang.org
acl名称,可以使用大写或小写字母a-z,数字0-9,冒号,点,中横线和下划线
8.2 ACL-criterion匹配规范
hdr([<name> [,<occ>]]):完全匹配字符串,<occ>表示在多值中使用的值的出现次数
hdr | 匹配字符 |
---|---|
hdr_beg | 前缀匹配,header中指定匹配内容的begin |
hdr_end | 后缀匹配 |
hdr_dom | 域匹配,heade中的domain name(host) |
hdr_dir | 路径匹配,uri路径 |
hdr_len | 长度匹配 |
hdr_reg | 正则式表达匹配 |
hdr_sub | 子串匹配 |
hdr(<string>)用于测试请求头部指定内容
hdr_dom(host) 请求的host名称,如:www.wang.org
hdr_beg (host)请求的开头,如:www. bbs.
hdr_end(host)请求的结尾,如: .com .org
8.2.1 hdr(dom)
8.2.2 hdr_dom(host)
8.2.3 hdr_end(hsot)
匹配以 .org结尾
8.2.4 hdr_beg(host)
匹配以 bbs开头
8.3 多个ACL组合调用方式
多个acl的逻辑处理
与 | 默认使用 |
---|---|
或 | or 或 || |
否定 | ! |
与:ACL 中两个条件都满足为true,默认为与
或:ACL中两个满足一个为true
非:取反,不满足ACL才为true
与
或 ||
非 !
基于域名访问
基于浏览器访问
基于文件后缀名实现动静分离
匹配访问路径实现动静分离
9.Haproxy四层负载
9.1在两台rs上安装mysal并新建用户
[root@webserver1 ~]# dnf install mariadb-server -y
[root@webserver1 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[root@webserver1 ~]# systemctl start mariadb.service
[root@webserver1 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.5.16-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 1 |
+-------------+
1 row in set (0.000 sec)
MariaDB [(none)]> create user wang@'%' identified by 'wang';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> grant all on *.* to wang@'%';
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> quit
Bye
[root@webserver2 ~]# dnf install mariadb-server -y
[root@webserver2 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[root@webserver2 ~]# systemctl start mariadb.service
[root@webserver2 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.5.16-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select @@server_id
-> ;
+-------------+
| @@server_id |
+-------------+
| 2 |
+-------------+
1 row in set (0.000 sec)
MariaDB [(none)]> create user wang@'%' identified by 'wang';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> grant all on *.* to wang@'%';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> quit
Bye
9.2在haproxy中登录Mysql
[root@haproxy ~]# mysql -uwang -pwang -h 172.25.254.100
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.16-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select @@server_id
-> ;
+-------------+
| @@server_id |
+-------------+
| 2 |
+-------------+
1 row in set (0.001 sec)
MariaDB [(none)]> select @@server_id
-> ;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 5
Current database: *** NONE ***
+-------------+
| @@server_id |
+-------------+
| 1 |
+-------------+
1 row in set (0.002 sec)
MariaDB [(none)]> quit
Bye
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
[root@haproxy ~]# systemctl restart haproxy.service
10.自定义Haproxy错误界面
10.1 自定义错误页面文件
[root@haproxy ~]# rpm -ql haproxy | grep http
/usr/share/doc/haproxy/design-thoughts/http2.txt
/usr/share/doc/haproxy/design-thoughts/http_load_time.url
/usr/share/doc/haproxy/internals/http-cookies.txt
/usr/share/doc/haproxy/internals/http-docs.txt
/usr/share/doc/haproxy/internals/http-parsing.txt
/usr/share/doc/haproxy/option-http_proxy.cfg
/usr/share/haproxy/400.http
/usr/share/haproxy/403.http
/usr/share/haproxy/408.http
/usr/share/haproxy/500.http
/usr/share/haproxy/502.http
/usr/share/haproxy/503.http
/usr/share/haproxy/504.http
[root@haproxy ~]# mkdir /etc/haproxy/errorpage -p
[root@haproxy ~]# cp /usr/share/haproxy/503.http /etc/haproxy/errorpage/
[root@haproxy ~]# cd /etc/haproxy/errorpage/
[root@haproxy errorpage]# ll
总用量 4
-rw-r--r-- 1 root root 213 8月 10 16:35 503.http
[root@haproxy errorpage]# vim 503.http
[root@haproxy errorpage]# vim /etc/haproxy/haproxy.cfg
[root@haproxy errorpage]# systemctl restart haproxy.service
浏览器访问
10.2 基于http重定向错误页面
输入172.25.254.100,重定向到百度页面
11 HAproxy https 实现
11.1 证书制作
[root@haproxy ~]# mkdir -p /etc/haproxy/certs
[root@haproxy ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /etc/haproxy/certs/wang.org.key -x509 -days 365 -out /etc/haproxy/certs/wang.org.crt
+.........+.....+.+......+.....+......+.......+.....+............+....+......+.....................+..+............+.+...+..............+..........+......+..+...+.+...+...........+.+.........+......+......+...+.........+...+.....+....+.....+......+...+....+......+......+.....+......+...+.+........+...+.......+..+.+.................+.+..+.......+..+.............+..+......+...............+....+...+.....+...+...+...............+.+.....+...+............+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
..+...............+............+....+..+....+...+...........+.+...+..+.........+......+..........+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*.+.........+..+....+......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*..+......+.......+.....+...+...................+...+............+....................+...+...+...+...............+.......+..+.+..+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Shanxi
Locality Name (eg, city) [Default City]:XI`AN
Organization Name (eg, company) [Default Company Ltd]:wang
Organizational Unit Name (eg, section) []:webserver
Common Name (eg, your name or your server's hostname) []:www.wang.org
Email Address []:admin@wang.org
[root@haproxy ~]# cat /etc/haproxy/certs/wang.org.key /etc/haproxy/certs/wang.org.crt > /etc/haproxy/certs/wang.pem
[root@haproxy ~]# cat /etc/haproxy/certs/wang.pem
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BALYG3Gj4opBqffSizX3CC2ayN7hdT/+u6qldoOcbxI239RIm5Yd+/YrS
tOrynCSoTinjYN22uNqPZwWVcrWFqDpGEaqHePMzjM5mGOz4SEYk0tNwQ6lw5eiX
2VsotFTEdQPT/EmJX9GAqR87z/lxK82KhqAlqs5vXIan9cPixa5WF2FgY7BLFbo6
iaS+IeL4TMQoF+LR35wTd3c1u7UeCLpPHQbH8Je/ankffTYCfc3+O0AKnW39w8os
zT1yrN3QO4IUdacJ3dqR/Fmqc1WLaeHMrZ6zzI2KZs4fSFWsJuzwR6CtaPQDkYC3
k3r0nczzn+5swvNb84ZryEUB5xheUycCAwEAAaNTMFEwHQYDVR0OBBYEFG1HL+LV
2Ii0HJbTRMzCgtzzHK2/MB8GA1UdIwQYMBaAFG1HL+LV2Ii0HJbTRMzCgtzzHK2/
MA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAK0Au9ejQEPiz7c2
zThIFoaaJH2GJJ/+5mpF1V+RvHdjTXey7d/fc8cwABnp0E0LcaofyMaL7i9dRNOK
9qlnBcg9hEFM5MyC76p3wgAJNeZqG+FeJisbZ0rYqTpgAXSIrulxtDPPlZ0Ant1v
qs9UdG2p0ie02QiFgREou3PbwPS5lVVaJHKRehVbE2RhpReSZU2+QXOvnuTo7yf7
vWF5atZXsPNPjUYz3okE3fpBeo1SVmVUo79kREMVE5uv2jcv6c3MZnlRPzHvZJBi
oPPcqXLGDKmsija42lk8TfZHL1nOrpI8BOPHen6lgDVq6VmYxaCmNTH/ymPjEy92
HKAsDF8=
-----END CERTIFICATE-----
[root@haproxy ~]#
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
[root@haproxy ~]# ll /etc/haproxy/certs/wang.pem
-rw-r--r-- 1 root root 3136 8月 10 16:54 /etc/haproxy/certs/wang.pem
[root@haproxy ~]# systemctl restart haproxy.service
查看3306端口已开启
浏览器访问:https://172.25.254.100
高级 >>> 继续访问
全站加密