Haproxy搭建Web群集

1. Haproxy搭建Web群集概述

1.1 简介

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

1.2 请求方式

GET方式:从指定的服务器上获取数据
GET请求能被缓存
GET请求会被保存在浏览器记录里
GET请求有长度的限制
主要用于获取数据
查询的字符串会显示在URL中,不安全

1.3 POST方式

提交数据给指定的服务器处理
POST请求不能被缓存
POST请求不会保存在浏览器记录里
POST请求没有长度限制
查询字符串不会显示在URL中,比较安全

1.4 返回状态码

正常的状态码为2××、3××
异常的状态码为4××、5××

2. 负载均衡常用调度算法

  1. RR(Round Robin):轮询调度

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

  1. 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有很大的改进,是目前用的比较多的算法

  1. SH(Source Hashing):基于来源访问调度

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

3. 常见的WEB群集调度器

目前常见的WEB群集调度器分别为软件和硬件,软件通常使用开源的LVS、Haproxy、Nginx

硬件一般使用比较多的F5,也有很多人使用国内一些产品,如梭子鱼、绿盟等

4. 使用Haproxy搭建Web群集

实验环境
Haproxy服务器 192.168.100.50
Nginx服务器1 192.168.100.40
Nginx服务器2 192.168.100.30
存储服务器 192.168.100.20

4.1 调试存储服务器 192.168.100.20

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

4.2 Nginx服务器1 192.168.100.40

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/
[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”选项来指定路径

启动、 停止 Nginx
killall -1 nginx                                                     ####安全重启
killall -3 nginx                                                     ###停止服务
注意点:如果出现: -bash: killall: command not found

解决方案;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

3、添加 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.
这样一来, 就可以 systemctl 命令来启动、 停止、 重启、 重载 Nginx 服务器了, 方法是
在执行时添加相应的 start、 stop、 restart、 reload 参数
killall -3 nginx                                                     ###停止服务
systemctl start nginx.service

systemctl start nginx.service
systemctl stop nginx.service
systemctl reload nginx.service
systemctl restart nginx.service
systemctl status nginx.service

4、安装httpd 挂载测试页

[root@localhost ~]# showmount -e 192.168.100.20     ####如果还没发布,请到存储服务器发布下,exportfs -rv
Export list for 192.168.100.20:
/opt/51xit  (everyone)
/opt/52xit  (everyone)

[root@localhost ~]# mount 192.168.100.20:/opt/51xit /usr/local/nginx/html/
[root@localhost ~]# vi /etc/fstab 
192.168.100.20:/opt/51xit/ /usr/local/nginx/html/ nfs     rw,tcp,intr     0 1        ###开机自动挂载,注意格式对齐

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

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

[root@localhost ~]# yum -y install elinks
[root@localhost ~]# elinks http://localhost       #####使用 elinks 浏览器      ####显示“this is www.51xit.com”页面,表明 Nginx 服务已经正常运行

4.3 Nginx服务器1 192.168.100.30

配置方式和Nginx服务器1 192.168.100.40的方式一样

4.4 配置Haproxy 服务器 192.168.100.50

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.114.56:80 check inter 2000 fall 3:定义在线节点
server inst2 192.168.114.56: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 

4.5 测试

浏览器登录192.168.100.50 切换 会发现不同的网站页面 说明已经实现了负载均衡

5. 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 

[root@localhost haproxy-1.4.24]# tail -f /var/log/haproxy/haproxy-info.log 
Sep 27 00:08:33 localhost haproxy[13864]: 192.168.32.1:57172 [27/Sep/2018:00:07:43.232] webcluster webcluster/inst1 1/0/1/2/50006 404 726 - - cD-- 2/2/1/0/0 0/0 "GET /favicon.ico HTTP/1.1"
Sep 27 00:08:40 localhost haproxy[13864]: 192.168.32.1:57223 [27/Sep/2018:00:07:47.477] webcluster webcluster/inst2 0/0/1/2/52806 200 750 - - cD-- 0/0/0/0/0 0/0 "GET / HTTP/1.1"

网页打开:192.168.100.20网站 然后查看日志文件不断生成情况

6. Haproxy参数优化

随着企业网站负载增加,haproxy参数优化相当重要
maxconn:最大连接数,根据应用实际情况进行调整,推荐使用10240
daemon:守护进程模式,Haproxy可以使用非守护进程模式启动,建议使用守护进程模式启动
nbproc:负载均衡的并发进程数,建议与当前服务器CPU核数相等或为其2倍
retries:重试次数,主要用于对集群节点的检查,如果节点多,且并发量大,设置为2次或3次

option http-server-close:主动关闭http请求选项,建议在生产环境中使用此选项
timeout http-keep-alive:长连接超时时间,设置长连接超时时间,可以设置为10s
timeout http-request:http请求超时时间,建议将此时间设置为5~10s,增加http连接释放速度
timeout client:客户端超时时间,如果访问量过大,节点响应慢,可以将此时间设置短一些,建议设置为1min左右就可以了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值