生产环境必备的Web群集Haproxy ,超级详细的原理和实验,一看秒懂!!!

Haproxy应用分析

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

1.LVS不支持正则处理,不能实现动静分离
2.对于大型网站,LVS的实施配置复杂,维护成本相对较高
3.haproxy是一 款可提供高可用性、负载均衡、及基于TCP和HTTP应用的代理的软件
4.适用于负载大的Web站点
5.运行在硬件.上可支持数以万计的并发连接的连接请求

负载均衡常用调度算法

◆RR(Round Robin):轮询调度
有三个节点A、B、C 第一个用户会被指派到节点A,第二个用户会被指派到节点B,第三个
用户会被直白到节点C,第四个用户会被指派到节点A,轮询分配访问,请求实现负载均衡效果,
此算法还有一种加权轮询,即根据每个节点的权重轮询分配请求。

◆LC(Least Connections):最小连接数
根据后端的节点连接数大小动态的分配前端请求,列如,有三个节点A、B、C,连接数是
A:4 B:5 C:6 第一个用户请求会被分配到A上,连接数会变为
A:5 B:5 C:6 第二个用户请求会被分配到A上,连接数会变为
A:6 B:5 C:6 第三个用户请求会被分配到B上,连接数会变为
A:6 B:6 C:6
此算法比较RR有很大的改进,是目前用的比较多的算法

◆SH(Source Hashing):基于来源访问调度
此算法用于一些有Session回话记录在服务器端的场景,可以基于源IP,cookie等做群集调度,
列如,使用基于源IP的调度算法,有三个节点A、B、C,
第一个用户第一次访问被指派到A,第二个用户第一次访问被指派到B,当地一个用户第二次访问
时都会被指派到A,第二个用户访问都会被指派到B。实现群集的调度。此调度算法好处会实现会话保持。
但某些IP访问量非常大的时候会引起负载不均衡,部分节点访问量超大,影响业务使用。

在这里插入图片描述

常见的WEB群集调度器

在这里插入图片描述

案例概述

1、Haproxy是目前比较流行的一种集群调度工具
2、Haproxy 与LVS、Nginx的比较,LVS性能最好,但是搭建相对复杂,Nginx的upstream模块支持集群功能,但是对集群节点健康检查功能不强,性能没有Haproxy好
3、本案例介绍使用Haproxy及Nginx搭建一套Web集群

实验

使用Haproxy搭建Web群集

场景介绍:
主机 操作系统 IP地址 主要软件
Haproxy服务器 CentoS7.6 192.168.30.41 haproxy-1.5.19.tar.gz
Nginx服务器1 CentoS7.6 192.168.30.42 Nginx-1.12.2.tar.gz
Nginx服务器2 CentoS7.6 192.168.30.43 Nginx-1.12.2.tar.gz
存储服务器 CentoS7.6 192.168.30.44 nfs-utils rpcbind

调试存储服务器 192.168.30.44rpm -q nfs-utils ###如果没装,yum -y install nfs-utils

rpm -q rpcbind ###如果没装,yum -y install rpcbind
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl start rpcbind

[root@localhost ~]# vi /etc/exports

/opt/51xit 192.168.100.0/24 (rw,sync)
/opt/52xit 192.168.100.0/24 (rw,sync)

[root@localhost ~]# systemctl restart nfs
[root@localhost ~]# systemctl restart rpcbind
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# mkdir /opt/51xit /opt/52xit

然后用Xftp工具连接存储主机192.168.30.44

在这里插入图片描述
然后分别在51xit和52xit俩个目录里面传人俩张图片作为网页的首页,但是一定要注意文件类型一定是.html
在这里插入图片描述
在这里插入图片描述

编译安装Nginx服务器1 192.168.30.42

1、编译安装 Nginx
Nginx 安装文件可以从官方网站 http://www.nginx.org/下载。
下面以稳定版 Nginx 1.12.2为例 上传至/opt下
[root@localhost ~]#yum -y install pcre-devel zlib-devel gcc-c++
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# cd /opt
[root@localhost ~]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost ~]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]#

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx

[root@localhost nginx-1.12.2]# make && make install
root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
注释:这边做软连接的目的是为了优化路径,为什么能优化路径呢?因为/usr/local/sbin/ 下相当于一个环境变量,之后执行相关程序的时候不需要再前面添加绝对路径了
[root@localhost nginx-1.12.2]# ls -l /usr/local/sbin/nginx

lrwxrwxrwx 1 root root 27 5 月 16 16:50 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx

2、Nginx 的运行控制

■ 检查配置文件
与 Apache 的主程序 httpd 类似, Nginx 的主程序也提供了“-t”选项用来对配置文件进行
检查, 以便找出不当或错误的配置。 配置文件 nginx.conf 默认位于安装目录下的 conf/子目
录中。 若要检查位于其他位置的配置文件, 可使用“-c”选项来指定路径

[root@localhost ~]# 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

■ 启动、 停止 Nginx
[root@localhost ~]# killall -1 nginx ####安全重启
[root@localhost ~]# killall -3 nginx ###停止服务

如果出现: -bash: killall: command not found

[root@localhost ~]# yum -y install psmisc //如果出现上述的报错,证明没有安装这个工具

[root@localhost ~]# nginx ####启动
[root@localhost ~]# netstat -anpt | grep nginx

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
7180/nginx: master

■ 添加 Nginx 系统服务

[root@localhost ~]# vim /lib/systemd/system/nginx.service

[Unit]
Description=nginx                                                 ####描述
After=network.target                                            ####描述服务类别
[Service]
Type=forking                                                          ####后台运行形式
PIDFile=/usr/local/nginx/logs/nginx.pid                ####PID 文件位置
ExecStart=/usr/local/nginx/sbin/nginx                  ####启动服务
ExecReload=/usr/bin/kill -s HUP $MAINPID         ####根据 PID 重载配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID           ####根据 PID 终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target

[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost ~]# systemctl enable nginx.service

Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to
/usr/lib/systemd/system/nginx.service.

[root@localhost ~]# killall -3 nginx ###停止服务
[root@localhost ~]# systemctl start nginx.service

3、安装httpd 挂载测试页

[root@localhost ~]# showmount -e 192.168.30.44

Export list for 192.168.30.44:
/opt/51xit  (everyone)
/opt/52xit  (everyone)

[root@localhost ~]# mount 192.168.30.44:/opt/51xit /usr/local/nginx/html/ // 临时挂载
[root@localhost ~]# vi /etc/fstab //进行永久挂载

192.168.30.44:/opt/51xit/ /usr/local/nginx/html/ nfs     rw,tcp,intr     0 1        ###开机自动挂载

[root@localhost nginx-1.12.2]# systemctl restart nginx

4、使用 浏览器测试挂载网页是否正常

在这里插入图片描述

编译安装Nginx服务器2 192.168.30.43

1、编译安装 Nginx
Nginx 安装文件可以从官方网站 http://www.nginx.org/下载。
下面以稳定版 Nginx 1.12.2为例 上传至/opt下
[root@localhost ~]#yum -y install pcre-devel zlib-devel gcc-c++
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# cd /opt
[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost opt]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]#

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx

[root@localhost nginx-1.12.2]# make && make install

[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.2]# ls -l /usr/local/sbin/nginx

lrwxrwxrwx 1 root root 27 5 月 16 16:50 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx

2、Nginx 的运行控制

与 Apache 的主程序 httpd 类似, Nginx 的主程序也提供了“-t”选项用来对配置文件进行
检查, 以便找出不当或错误的配置。 配置文件 nginx.conf 默认位于安装目录下的 conf/子目
录中。 若要检查位于其他位置的配置文件, 可使用“-c”选项来指定路径
[root@localhost ~]# 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

[root@localhost ~]# yum -y install psmisc
[root@localhost ~]# killall -1 nginx ####安全重启
[root@localhost ~]# killall -3 nginx ###停止服务

[root@localhost nginx-1.12.2]# nginx ####启动服务
[root@localhost nginx-1.12.2]# netstat -anpt | grep nginx

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15768/nginx: master 

■添加 Nginx 系统服务
[root@localhost ~]# vim /lib/systemd/system/nginx.service

[Unit]
Description=nginx                                                 ####描述
After=network.target                                            ####描述服务类别
[Service]
Type=forking                                                          ####后台运行形式
PIDFile=/usr/local/nginx/logs/nginx.pid                ####PID 文件位置
ExecStart=/usr/local/nginx/sbin/nginx                  ####启动服务
ExecReload=/usr/bin/kill -s HUP $MAINPID         ####根据 PID 重载配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID           ####根据 PID 终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target

[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost ~]# systemctl enable nginx.service

Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to
/usr/lib/systemd/system/nginx.service.

[root@localhost ~]# killall -3 nginx ###停止服务
[root@localhost ~]# systemctl start nginx.service

3、安装httpd 挂载测试页

[root@localhost nginx-1.12.2]# yum -y install nfs-utils 一定要安装,这个软件装了才能访问到存储服务目录
[root@localhost ~]# showmount -e 192.168.30.44

Export list for 192.168.30.44:
/opt/51xit (everyone)
/opt/52xit (everyone)

[root@localhost ~]# mount 192.168.30.44:/opt/52xit/ /usr/local/nginx/html/ 进行临时挂载
[root@localhost ~]# vi /etc/fstab

192.168.30.44:/opt/52xit/ /usr/local/nginx/html/ nfs     rw,tcp,intr     0 1        ###开机自动挂载

[root@localhost ~]# systemctl restart nginx

4、使用 浏览器测试挂载网页是否正常

在这里插入图片描述

配置Haproxy 服务器 192.168.30.41

1、编译安装 Haproxy
上传 haproxy-1.4.24.tar.gz 到/opt目录下
[root@localhost ~]# yum -y install pcre-devel bzip2-devel gcc gcc-c++
[root@localhost ~]# cd /opt
[root@localhost opt]# tar xzvf haproxy-1.4.24.tar.gz
[root@localhost opt]# cd haproxy-1.4.24/
[root@localhost haproxy-1.4.24]# make TARGET=linux26
[root@localhost haproxy-1.4.24]# make install

2、配置Haproxy 服务
[root@localhost haproxy-1.4.24]# mkdir /etc/haproxy
[root@localhost haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg

global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        #log loghost    local0 info
        maxconn 4096
        #chroot /usr/share/haproxy
        uid 99
        gid 99
        daemon
        #debug
        #quiet

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        #redispatch
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000

listen  webcluster 0.0.0.0:80
        option httpchk GET /index.html
        balance roundrobin
        server inst1 192.168.100.42:80 check inter 2000 fall 3
        server inst2 192.168.100.43:80 check inter 2000 fall 3

上述配置文件解释:

1、Haproxy配置文件通常分为三个部分
global:为全局配置
defaults:为默认配置
listen:为应用组件配置
global配置参数
log 127.0.0.1 local0:配置日志记录,配置日志记录,local0为日志设备,默认存放到系统日志
log 127.0.0.1 local1 notice:notice为日志级别,通常有24个级别
maxconn 4096:最大连接数
uid 99:用户uid gid 99:用户gid

2、defaults配置项配置默认参数,一般会被应用组件继承,如果在应用组件中没有特别声明,将安装默认配置参数设置
log global:定义日志为global配置中的日志定义
mode http:模式为http
option httplog:采用http日志格式记录日志
option dontlognull :保证HAProxy不记录上级负载均衡发送过来的用于检测状态没有数据的心跳包
retries 3:检查节点服务器失败连续达到三次则认为节点不可用
maxconn 2000:最大连接数
contimeout 5000:连接超时时间
clitimeout 50000:客户端超时时间
srvtimeout 50000:服务器超时时间

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

[root@localhost haproxy-1.4.24]# cp examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy-1.4.24]# chmod 755 /etc/init.d/haproxy
[root@localhost haproxy-1.4.24]# chkconfig --add haproxy
[root@localhost haproxy-1.4.24]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@localhost haproxy-1.4.24]# service haproxy start
[root@localhost haproxy-1.4.24]# systemctl stop haproxy.service
[root@localhost haproxy-1.4.24]# systemctl start haproxy.service

在浏览器输入192.168.30.41,切换 会发现不同的网站页面 说明已经实现了负载均衡

在这里插入图片描述
清除一下缓存之后,再次输入192.168.30.41
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值