理论+实验·Haproxy搭建Web群集

tech2025-04-28  6

理论+实验·Haproxy搭建Web群集

文章目录

理论+实验·Haproxy搭建Web群集一、Haproxy调度算法1.1 常见的Web集群调度器1.2 Haproxy应用分析1.3 Haproxy调度算法原理 二、Haproxy群集配置环境需求Nginx的安装与启动Haproxy配置文件详解listen配置项目一般为配置应用模块参数配置与测试测试Haproxy集群 三、Haproxy日志管理四、Haproxy参数优化五、部署模拟实验搭建web服务器搭建Haproxy服务器定义日志

一、Haproxy调度算法

1.1 常见的Web集群调度器

目前常见的Web集群调度器分为软件和硬件软件通常使用开源的LVS、Haproxy、Nginx硬件一般使用比较多的F5

1.2 Haproxy应用分析

LVS在企业应用中抗负载能力很强,但存在不足 LVS不支持正则处理,不能实现动静分离对于大型网站,LVS的实施配置负载,维护成本相对较高 Haproxy是一款可提供高可用性、负载均衡、及基于TCP和HTTP应用的代理的软件 适用于负载大的Web站点运行在硬件上可支持数以万计的并发连接的连接请求

1.3 Haproxy调度算法原理

RR (Round Robin) RR算法是最简单最常用的一种算法,即轮询调度 //举例// 有三个节点ABC 1、第一个用户访问会被指派到节点A 2、第二个用户访问会被指派到节点B 3、第三个用户访问会被指派到节点C 4、第四个用户访问继续指派到节点A,轮询分配访问请求实现负载均衡效果 LC (Least Connections) 最小连接数算法,根据后端的节点连接数大小动态分配前端请求 //举例// 1、有三个节点ABC,各节点的连接数分别为A:4B:5C:6 2、第一个用户连接请求,会被指派到A,连接数变为A:5B:5C:6 3、第二个用户请求会继续分配到A,连接数变为A:6B:5C:6;再有新的请求会分配给B,每次将新的请求指派给连接数最小的客户端 4、由于实际情况下ABC的连接数会动态释放,很难会出现一样连接数的情况 5、此算法相比较rr(轮询)算法有很大改进,是目前用到的比较多的一种算法 SH (Source Hashing) 基于来源访问调度算法,用于一些有Session会话记录在服务器端的场景,可以基于来源的IP、Cppkie等做集群调度 //举例// 1、有三个节点ABC,第一个用户第一次访问被指派到了A,第二个用户第一次访问被指派到了B 2、当第一个用户第二次访问时会被继续指派到A,第二个用户第二次访问时依旧会被指派到B,只要负载均衡调度器不重启,第一个用户访问都会被指派到A,第二个用户访问都会被指派到B,实现集群的调度 3、此调度算法好处是实现会话保持,但某些IP访问量非常大时会引起负载不均衡,部分节点访问量超大,影响业务使用

二、Haproxy群集配置

环境需求

用一台客户端,三台虚拟服务器模拟搭建一套Web集群虚拟机安装CentOS 7 //Haproxy调度器// 20.0.0.90 //server01// 20.0.0.80 //server02// 20.0.0.70

Nginx的安装与启动

在两台网站服务器上安装Nginx,并启动服务 使用源码编译的方式进行安装关闭Firewalld防火墙安装基础软件包增加系统用户账号nginx编译安装Nginx并启动 在两台Nginx上配置测试网站,注意测试网页的内容应该不同,以便于测试在负载均衡器上安装Haproxy安装步骤 安装基础软件包编译安装Haproxy 建立Haproxy的配置文件 创建配置文件目录/etc/haproxy将源码包提供的配置文件样例haproxy。cfg复制到配置文件目录中

Haproxy配置文件详解

defaults配置项配置默认参数,一般会被应用组件继承如果应用组件中没有特别声明,将按默认配置参数设置 log global:定义日志为global配置中的日志定义mode http:模式为httpoption httplog:采用http日志格式记录日志retries 3:检查节点服务器失败连接达到三次则认为节点不可用maxconn 2000:最大连接数contimeout 5000:连接超时时间clitimeout 50000:客户端超时时间srvtimeout 50000:服务器超时时间

listen配置项目一般为配置应用模块参数

listen appli4-backup 0.0.0.0:10004:定义一个appli4-backup的应用option httpchk /index.html:检查服务器的index.html文件option persist:强制将请求发送到已经down掉的服务器balance roundrobin:负载均衡调度算法使用轮询算法server inst1 20.0.0.80:80 check inter 2000 fall 3:定义在线节点server inst2 20.0.0.70:80 check inter 2000 fall 3 backup :定义备份节点

配置与测试

根据实际的案例修改相应参数 listen webcluster 0.0.0.0:80option httpchk GET /index.htmlbalance roundrobinserver inst1 20.0.0.80:80 check inter 2000 fall 3server inst2 20.0.0.70:80 check inter 2000 fall 3 创建自启动脚本,并启动Haproxy服务

测试Haproxy集群

测试Haproxy集群 可以两个不同的浏览器中分别访问两个测试网站,正常情况下应该出现两个不同网站的测试页面 高可用性 将其中一台Nginx服务器停用,在客户端浏览器中访问Haproxy,正常情况下应出现另外一台Nginx服务器的网站测试页面

三、Haproxy日志管理

默认是输出到系统的syslog中,生产环境中一般单独定义定义的方法步骤 修改Haproxy配置文件中关于日志配置的选项,加入配置 log /dev/log local0 infolog /dev/log local0 notice 修改rsyslog配置,将Haproxy相关的配置独立定义到haproxy.conf,并放到/etc/rsyslog.d下保存配置文件并重启rsyslog服务,完成rsyslog配置 访问Haproxy集群测试网页并查看日志信息

四、Haproxy参数优化

随着企业网站负载增加,haproxy参数优化相当重要 maxconn:最大连接数,根据应用实际情况进行调整,推荐使用10240daemon:守护进程模式,Haproxy可以使用非守护进程模式启动,建议使用守护进程模式启动nbproc:负载均衡的并发进程数,建议与当前服务器CPU核数相等或为其2倍 retries:重试次数,主要用于对集群节点的检查,如果节点多,且并发量大,设置为2次或3次 option http-server-close:主动关闭htpp请求选项,建议在生产环境中使用此选项timeout httpkeep-alive:长连接超时时间,设置长连接超时时间,可以设置为10stimeout http-request:http请求超时时间,建议将此时间设置为5~10s,增加http连接释放速度timeout client:客户端超时时间,如果访问量过大,节点响应慢,可以将此时间设置短一些,建议设置为1min左右就可以了

五、部署模拟实验

搭建web服务器

//server01:20.0.0.80// //server02:20.0.0.70// //创建用户// [root@server01|server02 ~]# useradd -M -s /sbin/nologin nginx //安装环境// [root@server01|server02 ~]# yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel make //解压源码包// [root@server01|server02 ~]# tar zxvf nginx-1.12.0.tar.gz -C /opt/ [root@server01|server02 ~]# cd /opt/nginx-1.12.0/ //编译安装// [root@server01|server02 nginx-1.12.0]# ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx [root@server01|server02 nginx-1.12.0]# make && make install [root@server01|server02 nginx-1.12.0]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ //启动服务// [root@server01|server02 nginx-1.12.0]# systemctl stop firewalld [root@server01|server02 nginx-1.12.0]# setenforce 0 [root@server01|server02 nginx-1.12.0]# nginx //设置网站// [root@server01|server02 nginx-1.12.0]# echo "<h1>this is server01|server02 web</h1>" > /usr/local/nginx/html/test.html

搭建Haproxy服务器

//haproxy:20.0.0.90// //安装环境// [root@haproxy ~]# yum -y install pcre-devel bzip2-devel gcc gcc-c++ make //编译安装// [root@haproxy ~]# tar zxvf haproxy-1.5.19.tar.gz -C /opt/ [root@haproxy ~]# cd /opt/haproxy-1.5.19/ [root@haproxy haproxy-1.5.19]# make TARGET=linux26 [root@haproxy haproxy-1.5.19]# make install [root@haproxy haproxy-1.5.19]# mkdir /etc/haproxy [root@haproxy haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/ [root@haproxy haproxy-1.5.19]# vim /etc/haproxy/haproxy.cfg ... #redispatch //注释这一行// ... #chroot /usr/share/haproxy //注释这一行// ... listen webcluster 0.0.0.0:80 //listen以下全部删除重新添加// option httpchk GET /test.html balance roundrobin server inst1 20.0.0.80:80 check inter 2000 fall 3 server inst1 20.0.0.70:80 check inter 2000 fall 3 [root@haproxy haproxy-1.5.19]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy [root@haproxy haproxy-1.5.19]# chmod +x /etc/init.d/haproxy [root@haproxy haproxy-1.5.19]# chkconfig --add /etc/init.d/haproxy [root@haproxy haproxy-1.5.19]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy //启动服务// [root@haproxy haproxy-1.5.19]# setenforce 0 [root@haproxy haproxy-1.5.19]# systemctl stop firewalld [root@haproxy haproxy-1.5.19]# service haproxy start

定义日志

[root@haproxy haproxy-1.5.19]# vim /etc/haproxy/haproxy.cfg ... log /dev/log local0 info log /dev/log local0 notice [root@haproxy haproxy-1.5.19]# service haproxy restart [root@haproxy haproxy-1.5.19]# touch /etc/rsyslog.d/haproxy.conf [root@haproxy haproxy-1.5.19]# vim /etc/rsyslog.d/haproxy.conf if ($programname == 'haproxy' and $syslogseverity-text == 'info') then -/var/log/haproxy/haproxy-info.log &~ if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log &~ [root@haproxy haproxy-1.5.19]# systemctl restart rsyslog [root@haproxy haproxy]# cd /var/log/haproxy/ [root@haproxy haproxy]# ls haproxy-info.log
最新回复(0)