文章目录

  • 一、常见的Web集群调度器
  • 二、haproxy介绍
  • 2.1Haproxy应用分析
  • 2.2Haproxy调度算法原理
  • 2.3Haproxy安装与启动
  • 2.4Haproxy配置文件详解
  • 三、搭建过程


一、常见的Web集群调度器

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

二、haproxy介绍

2.1Haproxy应用分析

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

2.2Haproxy调度算法原理

Haproxy支持多种调度算法,最常用的有三种

  1. RR(Round Robin)
    RR算法是最简单最常用的一种算法,即轮询调度
    理解举例:
    ◆有三个节点A、B、C
    ◆第一个用户访问会被指派到节点A
    ◆第二个用户访问会被指派到节点B
    ◆第三个用户访问会被指派到节点C
    ◆第四个用户访问继续指派到节点A,轮询分配访问请求实现负载均衡效果
  2. 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、B、C的连接数会动态释放,很难会出现一样连接数的情况
    ◆ 此算法相比较rr算法有很大改进,是目前用到比较多的一种算法
  3. SH (Source Hashing)
    基于来源访问调度算法,用于一些有Session会话记录在服务器端的场景,可以基于来源的IP、Cookie等做集群调度
    理解举例:
    ◆ 有三个节点A、B、C,第一个用户第一次访问被指派到了A,第二个用户第一次访问被指派到了B
    ◆ 当第一个用户第二次访问时会被继续指派到A,第二个用户第二次访问时依旧会被指派到B,只要负载均衡调度器不重启,第一个用户访问都会被指派到A,第二个用户访问都会被指派到B,实现集群的调度
    ◆ 此调度算法好处是实现会话保持,但某些IP访问量非常大时会引起负载不均衡部分节点访问量超大,影响业务使用

2.3Haproxy安装与启动

在负载均衡器上安装Haproxy
安装步骤

  1. 安装基础软件包
  2. 编译安装haproxy
  3. 要注意操作系统版本,是32位系统还是64位
  4. 建立Haproxy的配置文件
  5. 创建配置文件目录/etc/haproxy
  6. 将源码包提供的配置文件样例haproxy.cfg复制到配置文件目录中

2.4Haproxy配置文件详解

Haproxy配置文件通常分为三个部分

  • global:为全局配置
  • defaults:为默认配置
  • listen:为应用组件配置

global配置参数

参数

说明

log 127.0.0.1 local0

配置日志记录,local0为日志设备,默认存放到系统日志

log 127.0.0.1 local1

noticenotice为日志级别,通常有24个级别

maxconn 4096

最大连接数

uid 99

用户uid

gid 99

用户gid

三、搭建过程

场景介绍:

主机

操作系统

IP地址

主要软件

Haproxy服务器

CentoS7.6

192.168.100.41

haproxy-1.5.19.tar.gz

Nginx服务器1

CentoS7.6

192.168.100.42

Nginx-1.12.2.tar.gz

Nginx服务器2

CentoS7.6

192.168.100.43

Nginx-1.12.2.tar.gz

存储服务器

CentoS7.6

192.168.100.44

nfs-utils rpcbind

调试存储服务器 192.168.100.44

rpm -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
[root@localhost ~]# echo "this is www.51xit.com" >/opt/51xit/index.html
[root@localhost ~]# echo "this is www.52xit.com" >/opt/52xit/index.html

编译安装Nginx服务器1 192.168.100.42
nginx搭建部分 安装httpd 挂载测试页51xit

[root@localhost ~]# showmount -e 192.168.100.44     ###看一下有什么可挂载
Export list for 192.168.100.44:
/opt/51xit  (everyone)
/opt/52xit  (everyone)
挂载51网页
[root@localhost ~]# mount 192.168.100.44:/opt/51xit /usr/local/nginx/html/
[root@localhost ~]# vi /etc/fstab 
192.168.100.44:/opt/51xit/ /usr/local/nginx/html/ nfs     rw,tcp,intr     0 1   ###永久挂载
[root@localhost nginx-1.12.2]# systemctl restart nginx 重启

编译安装Nginx服务器2 192.168.100.43
nginx搭建部分 安装httpd 挂载测试页52xit

[root@localhost ~]# showmount -e 192.168.100.44   
Export list for 192.168.100.44:
/opt/51xit  (everyone)
/opt/52xit (everyone)

[root@localhost ~]# mount 192.168.100.44:/opt/52xit/ /usr/local/nginx/html/
[root@localhost ~]# vi /etc/fstab 
192.168.100.44:/opt/52xit/ /usr/local/nginx/html/ nfs     rw,tcp,intr     0 1        ###开机自动挂载

[root@localhost ~]# systemctl restart nginx

配置Haproxy 服务器 192.168.100.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
[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.100.41,发现每次访问的网站页面都来回切换,说明已经实现了轮询负载均衡