当前位置:首页 > Web开发 > 正文

使用Haproxy搭建Web群集(理论结合实战,全程可跟做!)

2024-03-31 Web开发

标签:

常见的Web集群调度器:

目前常见的Web集群调度器分为软件和硬件,,软件通常使用开源的LVS、Haproxy、 Nginx, 硬件一般使用比较多的是F5,也有很多人使用国内的一些产品,如梭子鱼、绿盟等

Haproxy应用分析 LVS在企业应用中抗负载能力很强,但存在不足

1.LVS不支持正则处理,不能实现动静分离
2.对于大型网站,LVS的实施配置复杂,维护成本相对较高

Haproxy是一款可提供高可用性、负载均衡、及基于TCP和HTTP应用的代理的软件

1.特别适用于负载特别大的Web站点
2.运行在当前的硬件.上可支持数以万计的并发连接连接请求

Haproxy调度算法原理 Haproxy支持多种调度算法,最常用的有3种:

1.RR (Round Robin):
RR算法是最简单最常用的一-种算法,即轮询调

2.理解举例:
有三个节点A、B、C,第一个用户访问会被指派到节点A,第二个用户访问会被指派到节点B,第三个用户访问会被指派到节点第四个用户访问继续指派到节点A,轮询分配访问请求实现负载均衡效果

Haproxy支持多种调度算法, 最常用的有三种: 1.SH (Source Hashing):

SH即基于来源访问调度算法,此算法用于一些有Session会话记录在服务器端的场景,可以基于来源的IP、Cookie等做集群调度

2.理解举例

①有三个节点A、B、C,第一个用户第一次访问被指派到了A,第二个用户第一次访问被指派到了B
②当第一个用户第二次访问时会被继续指派到A,第二个用户第二次访问时依旧会被指派到B,只要负载均衡调度器不重启,第一个用户访问都会被指派到A,第二个用户访问都会被指派到B,实现集群的调度
③此调度算法好处是实现会话保持,但某些IP访问量非常大时会引起负载不均衡,部分节点访问量超大,影响业务使用

Demo: 环境准备:

Nginx1:192.168.100.201---->CentOS 7-2

Nginx2:192.168.100.202---->CentOS 7-3

调度服务器:192.168.100.210---->CentOS 7-4

客户机:192.168.100.58---->win 7-1

Nginx2:CentOS 7-3的操作 [[email protected] ~]# yum install pcre-devel zlib-devel gcc gcc-c++ make -y //改网卡为仅主机模式 [[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 BOOTPROTO="static" IPADDR=192.168.100.201 NETMASK=255.255.255.0 GATEWAY=192.168.100.1 //修改完成后输入:wq保存退出 [[email protected] ~]# service network restart Restarting network (via systemctl): [ 确定 ] [[email protected] ~]# mkdir /aaa [[email protected] ~]# mount.cifs //192.168.10.189/rpm /aaa Password for [email protected]//192.168.10.189/rpm: [[email protected] ~]# cd /aaa [[email protected] aaa]# ls apr-1.6.2.tar.gz jdk apr-util-1.6.0.tar.gz john-1.8.0.tar.gz awstats-7.6.tar.gz lf.jpg cronolog-1.6.2-14.el7.x86_64.rpm mysql Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz error.png php extundelete-0.2.4.tar.bz2 TC haproxy-1.5.19.tar.gz tomcat httpd-2.4.29.tar.bz2 tomcat.tmp hzw.jpeg wh.jpg [[email protected] aaa]# cd tomcat/ [[email protected] tomcat]# ls apache-tomcat-7.0.54.tar.gz jdk-8u91-linux-x64.tar.gz apache-tomcat-8.5.16.tar.gz nginx-1.12.0.tar.gz jdk-7u65-linux-x64.gz nginx-1.6.0.tar.gz [[email protected] tomcat]# tar zxvf nginx-1.12.0.tar.gz -C /opt/ [root[email protected] tomcat]# cd /opt/ [[email protected] opt]# useradd -M -s /sbin/nologin nginx [[email protected] opt]# cd nginx-1.12.0/ [[email protected] nginx-1.12.0]# ls auto CHANGES.ru configure html man src CHANGES conf contrib LICENSE README [[email protected] nginx-1.12.0]# ./configure --prefix=http://www.mamicode.com/usr/local/nginx --user=nginx --group=nginx [[email protected] nginx-1.12.0]# make && make install [[email protected] nginx-1.12.0]# cd /usr/local/nginx/html/ [[email protected] html]# echo "this is accp web" > test.html [[email protected] html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ [[email protected] html]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [[email protected] html]# nginx [[email protected] html]# netstat -ntap | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6408/nginx: master [[email protected] html]# systemctl stop firewalld.service [[email protected] html]# setenforce 0 在火狐浏览器中输入: 验证:此时显示 this is accp web

Nginx1:CentOS 7-2的 操作 [[email protected] ~]# yum install pcre-devel zlib-devel gcc gcc-c++ make -y //改网卡为仅主机模式 [[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 BOOTPROTO="static" IPADDR=192.168.100.202 NETMASK=255.255.255.0 GATEWAY=192.168.100.1 //修改完成后输入:wq保存退出 [[email protected] ~]# service network restart Restarting network (via systemctl): [ 确定 ] [[email protected] ~]# mkdir /aaa [[email protected] ~]# mount.cifs //192.168.10.189/rpm /aaa Password for [email protected]//192.168.10.189/rpm: [[email protected] ~]# cd /aaa [[email protected] aaa]# ls apr-1.6.2.tar.gz jdk apr-util-1.6.0.tar.gz john-1.8.0.tar.gz awstats-7.6.tar.gz lf.jpg cronolog-1.6.2-14.el7.x86_64.rpm mysql Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz error.png php extundelete-0.2.4.tar.bz2 TC haproxy-1.5.19.tar.gz tomcat httpd-2.4.29.tar.bz2 tomcat.tmp hzw.jpeg wh.jpg [[email protected] aaa]# cd tomcat/ [[email protected] tomcat]# ls apache-tomcat-7.0.54.tar.gz jdk-8u91-linux-x64.tar.gz apache-tomcat-8.5.16.tar.gz nginx-1.12.0.tar.gz jdk-7u65-linux-x64.gz nginx-1.6.0.tar.gz [[email protected] tomcat]# tar zxvf nginx-1.12.0.tar.gz -C /opt/ [[email protected] tomcat]# cd /opt/ [[email protected] opt]# useradd -M -s /sbin/nologin nginx [[email protected] opt]# cd nginx-1.12.0/ [[email protected] nginx-1.12.0]# ls auto CHANGES.ru configure html man src CHANGES conf contrib LICENSE README [[email protected] nginx-1.12.0]# ./configure --prefix=http://www.mamicode.com/usr/local/nginx --user=nginx --group=nginx [[email protected] nginx-1.12.0]# make && make install [[email protected] nginx-1.12.0]# cd /usr/local/nginx/html/ [[email protected] html]# echo "this is benet web" > test.html [[email protected] html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ [[email protected] html]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [[email protected] html]# nginx [[email protected] html]# netstat -ntap | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6408/nginx: master [[email protected] html]# systemctl stop firewalld.service [[email protected] html]# setenforce 0 在火狐浏览器中输入: 验证:此时显示 this is benet web

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/41596.html