使用haproxy搭建web群集

Haproxy调度算法

Haproxy支持多种调度算法,最常用的有三种:RR(Round Robin),LC(Least Connections),SH(Source Hashing)

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

Haproxy群集搭建

环境准备

VMware软件
两台centos7虚拟机作为NGINX(IP地址:192.168.100.10 IP地址:192.168.100.20)
一台centos7虚拟机作为Haproxy(IP地址:192.168.100.30)

编译安装 Nginx

[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/
[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
[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 ~]# 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.

编辑测试web页面

[root@localhost ~]# echo "this is 192.168.100.10" > /usr/local/nginx/html/index.html

此时可以用浏览器访问192.168.100.10,查看页面

编译安装Nginx服务器1 192.168.100.20

[root@localhost ~]# echo "this is 192.168.100.20" > /usr/local/nginx/html/index.html

配置Haproxy 服务器 192.168.100.30

[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

配置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							#配置日志记录,配置日志记录,local0为日志设备,默认存放到系统日志
        log 127.0.0.1   local1 notice				#notice为日志级别,通常有24个级别
        #log loghost    local0 info				
        maxconn 4096									#最大连接数
        #chroot /usr/share/haproxy
        uid 99
        gid 99
        daemon
        #debug
        #quiet

defaults														#默认配置
        log     global											#定义日志为global配置中的日志定义
        mode    http											#模式为http
        option  httplog										#采用http日志格式记录日志
        option  dontlognull								#保证HAProxy不记录上级负载均衡发送过来的用于检测状态没有数据的心跳包
        retries 3												#检查节点服务器失败连续达到三次则认为节点不可用
        #redispatch
        maxconn 2000									#最大连接数
        contimeout      5000							#连接超时时间
        clitimeout      50000								#客户端超时时间
        srvtimeout      50000							#服务器超时时间

listen  webcluster 0.0.0.0:80						#应用组件配置
        option httpchk GET /index.html			#检查服务器的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

测试:192.168.100.30 切换会发现不同的网站页面,代表测试成功

访问 Haproxy集群测试网页并测试日志信息

[root@localhost haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg
global                       ####下面log开头的 统统去掉换下面的配置
        log /dev/log    local0 info
        log /dev/log    local1 notice
[root@localhost haproxy-1.4.24]# systemctl restart haproxy.service        
[root@localhost haproxy-1.4.24]# touch /etc/rsyslog.d/haproxy.conf
[root@localhost haproxy-1.4.24]# vi /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@localhost haproxy-1.4.24]#  systemctl restart rsyslog.service 	#linux系统自带的日志管理系统
[root@localhost haproxy-1.4.24]# tail -f /var/log/haproxy/haproxy-info.log 
Nov  1 10:32:30 promote haproxy[1304]: 192.168.100.1:64352 [01/Nov/2020:10:32:07.102] webcluster webcluster/inst2 0/0/0/0/23080 200 1097 - - ---- 1/1/1/0/0 0/0 "GET / HTTP/1.1"
Nov  1 10:32:55 promote haproxy[1304]: 192.168.100.1:64340 [01/Nov/2020:10:32:03.269] webcluster webcluster/inst1 0/0/1/0/52445 404 3630 - - cD-- 2/2/1/1/0 0/0 "GET /favicon.ico HTTP/1.1"
Nov  1 10:33:10 promote haproxy[1304]: 192.168.100.1:64368 [01/Nov/2020:10:32:54.813] webcluster webcluster/inst1 1/0/0/1/15527 200 965 - - ---- 0/0/0/0/0 0/0 "GET / HTTP/1.1"


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值