使用Haproxy搭建高效Web群集的完整指南
目录
一、案例分析
1.案例前置知识点
2.资源清单
二、案例实施
1.修改主机名
2.编译安装Nginx服务器
3.安装Haproxy
4.Haproxy服务器配置
5.测试Web群集
6.Haproxy的日志
7.Haproxy的参数优化
一、案例分析
1.案例前置知识点
a.HTTP请求
通过URL访问网站使用的是HTTP协议,此类请求一般称为HTTP请求
HTTP请求的方式分为GRT方式和POST方式
当使用浏览器访问某一个URL,会根据请求URL返回状态码,通常正常的状态码为2xx,3xx,如果出现异常会返回4xx,5xx
b.负载均衡常用调度算法
RR(轮询调度):此算法还有一种加权轮询,即根据每个节点的权重轮询分配访问请求
LC(最小连接数算法):根据后端的节点连接数大小动态分配前端请求
SH(基于来源访问调度算法):此算法用于一些有Session会话记录在服务器端的场景,可以基于来源的IP、Cookie等做群集调度
c.常见的Web群集调度器
软件:通常使用开源的LVS、Haproxy、Nginx
硬件:一般使用较多的是F5,或者国内的产品:梭子鱼、绿盟
2.资源清单
主机 | 操作系统 | IP地址 | 应用 |
nginx1 | OpenEuler24.03 | 192.168.16.142 | nginx |
nginx2 | OpenEuler24.03 | 192.168.16.143 | nginx |
haproxy | OpenEuler24.03 | 192.168.16.144 | haproxy |
二、案例实施
1.修改主机名
hostnamectl set-hostname nginx1
hostnamectl set-hostname nginx2
hostnamectl set-hostname haproxy
2.编译安装Nginx服务器
#Nginx1上
dnf install -y gcc make pcre-devel zlib-devel openssl-devel perl-ExtUtils-MakeMaker tar
useradd -M -s /sbin/nologin nginx
tar zxf nginx-1.26.3.tar.gz
cd nginx-1.26.3
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-pcre make && make install
echo "This is nginx1" > /usr/local/nginx/html/index.html
/usr/local/nginx/sbin/nginx#Nginx2上
dnf install -y gcc make pcre-devel zlib-devel openssl-devel perl-ExtUtils-MakeMaker tar
useradd -M -s /sbin/nologin nginx
tar zxf nginx-1.26.3.tar.gz
cd nginx-1.26.3
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-pcre make && make install
echo "This is nginx2" > /usr/local/nginx/html/index.html
/usr/local/nginx/sbin/nginx
3.安装Haproxy
dnf install -y haproxy
4.Haproxy服务器配置
a.建立Haproxy配置文件
vi /etc/haproxy/haproxy.cfggloballog 127.0.0.1 local2chroot /var/lib/haproxypidfile /var/run/haproxy.piduser haproxygroup haproxydaemonmaxconn 4000defaultsmode httplog globaloption httplogoption dontlognullretries 3timeout http-request 5stimeout queue 1mtimeout connect 5stimeout client 1mtimeout server 1mtimeout http-keep-alive 5stimeout check 5smaxconn 3000listen webclusterbind 0.0.0.0:80option httpchk GET /index.htmlbalance roundrobin#192.168.16.142:80 修改为ngix1的IPserver inst1 192.168.16.142:80 check inter 2000 fall 3#192.168.16.143:80 修改为ngix2的IPserver inst2 192.168.16.143:80 check inter 2000 fall 3
b.启动服务
systemctl restart haproxy
systemctl enable haproxy
5.测试Web群集
C:\Users\Y>curl 192.168.16.144
This is nginx1C:\Users\Y>curl 192.168.16.144
This is nginx2C:\Users\Y>curl 192.168.16.144
This is nginx1C:\Users\Y>curl 192.168.16.144
This is nginx2
6.Haproxy的日志
a.修改haproxy配置文件
vi /etc/haproxy/haproxy.cfg globallog /dev/log local0 info#chroot /var/lib/haproxy
b.配置Rsyslog服务
vi /etc/rsyslog.d/99-haproxy.conf
local0.* /var/log/haproxy.log
c.创建日志文件并设置权限
touch /var/log/haproxy.log
chmod 640 /var/log/haproxy.log
chown root:adm /var/log/haproxy.log
d.重启Rsyslog和HAProxy服务
systemctl restart rsyslog
systemctl restart haproxy
e.测试日志信息
tail -f /var/log/haproxy.log
7.Haproxy的参数优化
参数 | 参数说明 | 优化建议 |
maxconn | 最大连接数 | 此参数根据应用的时间情况进行调整,推荐使用10240 |
daemon | 守护进程模式 | Haproxy可以使用非守护进程模式启动,生产环境建议使用守护进程模式启动 |
nbproc | 负载均衡的并发进程数 | 建议与当前服务器CPU核数相等或为2倍 |
retries | 重试次数 | 此参数主要用于对群集节点的检查,如果节点多,且并发量大,设置为2次或3次;在服务器节点不多的情况下,可设置5次或6次 |
option | 主动关闭http请求选项 | 建议在生产环境中使用此选项,避免由于timeout时间设置过长导致http连接堆积 |
http-server-close | ||
timeout | 长连接超时时间 | 此选项设置长连接超时时间,可以设置为10s |
http-keep-alive | ||
timeout http-request | http请求超时时间 | 建议将此时间设置5-10s,增加http连接释放速度 |
timeout client | 客户端超时时间 | 如果访问量过大,节点响应慢,可以将时间设置短一些,建议设置为1min左右 |