Centos7部署HAProxy负载均衡策略

简介

HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理
HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理,HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接,并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上
HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接,事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题,但是此模型的弊端为在多核系统上,这些程序通常扩展性较差,这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作
haproxy负载均衡保持客户端和服务器Session亲缘性的三种方式
1 用户IP识别
haproxy 将用户IP经过hash计算后 指定到固定的真实服务器上(类似于nginx 的IP hash 指令)
配置指令 balance source
2 cookie识别
haproxy 将WEB服务端发送给客户端的cookie中插入(或添加前缀)haproxy定义的后端的服务器COOKIE ID
配置指令例举 cookie SESSION_COOKIE insert indirect nocache
3 session识别
haproxy 将后端服务器产生的session和后端服务器标识存在haproxy中的一张表里。客户端请求时先查询这张表
健康检查
haproxy自带后端服务器的健康检查,当某一个后端服务器宕机之后,其会自动自动将其移除出轮询队列,并会在监控页面显示服务器异常

前期准备

准备三台Centos7虚拟机,配置IP地址和hostname,同步系统时间,关闭防火墙和selinux,修改IP地址和hostname映射

ip hostname 部署
192.168.29.133 proxyserver HAProxy
192.168.29.132 web1 nginx
192.168.29.138 web2 nginx

web1和web2服务器部署Nginx

[root@web1 ~]# yum install nginx -y
[root@web1 ~]# echo "web1" > /usr/share/nginx/html/index.html
#启动服务
[root@web1 ~]# systemctl start nginx

[root@web2 ~]# yum install nginx -y
[root@web2 ~]# echo "web2" > /usr/share/nginx/html/index.html
#启动服务
[root@web2 ~]# systemctl start nginx

部署HAProxy

[root@proxyserver ~]# yum install haproxy -y

修改配置文件

[root@proxyserver ~]# vi /etc/haproxy/haproxy.cfg
# Global settings
global
   #日志管理为local2载体,需要在rsyslog中设置存放目录
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    stats socket /var/lib/haproxy/stats
    ssl-default-bind-ciphers PROFILE=SYSTEM
    ssl-default-server-ciphers PROFILE=SYSTEM
defaults
    #设定为HTTP模式
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    #设置为关闭长连接
    option                  httpclose
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000
#前端设置
frontend main
    #绑定5000端口进行负载均衡
    bind *:5000
    #对用户申请的不同请求进行分流
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js
    acl url_html         url_reg        -i \.html$
    #设置不同请求的后端服务器组
    use_backend static          if url_static
    use_backend app             if url_html
    #设置默认后端服务器组
    default_backend             app
#后端转发设置
#static后端组服务器设置
backend static
    balance     roundrobin
    server      static 127.0.0.1:4331 check
#app后端组服务器设置
backend app
    #设置转发策略
    balance     roundrobin
    #以下设置为把真实服务器的ID插入到回复用户的信息中,用户相同的请求可根据cookie找到相同的服务器
    #cookie      SERVERID insert indirect nocache
    #server  app1 192.168.29.132:80 check cookie 3
    #server  app2 192.168.29.138:80 check cookie 4
    #设定转发的后台服务器地址并开启对后台服务器的健康检查
    server  app1 192.168.29.132:80 check 
    server  app2 192.168.29.138:80 check 
#设定监控平台
listen admin_stats
        stats   enable
    #绑定监控端口号
        bind    *:8080    
        mode    http    
        option  httplog
        log     global
        maxconn 10
        stats   refresh 30s   
        stats   uri /admin   
        stats   realm haproxy
    #设定访问权限,用户名和密码
        stats   auth admin:admin  
        stats   hide-version   
        stats   admin if TRUE 

配置rsyslog

[root@proxyserver ~]# vi /etc/rsyslog.conf
#配置使用udp协议
module(load="imudp")
input(type="imudp" port="514")
#设定haproxy日志
local2.*                                                /var/log/haproxy.log

#重启服务
[root@proxyserver ~]# systemctl restart rsyslog

更多rsyslog服务配置可参考:https://blog.csdn.net/xixixilalalahaha/article/details/107546468
启动服务

[root@proxyserver ~]# systemctl start haproxy
#查看启动情况
[root@proxyserver ~]# netstat -tnlp |grep haproxy
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      3257/haproxy        
tcp        0      0 0.0.0.0:5000            0.0.0.0:*               LISTEN      3257/haproxy        

测试验证

在这里插入图片描述
在这里插入图片描述
查看日志

[root@proxyserver ~]# cat /var/log/haproxy.log 
Jul 24 11:40:25 localhost haproxy[3257]: 192.168.29.1:37216 [24/Jul/2020:11:40:25.552] main app/app1 0/0/1/1/2 200 234 - - ---- 1/1/0/0/0 0/0 "GET / HTTP/1.1"
Jul 24 11:40:39 localhost haproxy[3257]: 192.168.29.1:37231 [24/Jul/2020:11:40:39.060] main app/app2 0/0/12/1/13 200 234 - - ---- 1/1/0/0/0 0/0 "GET / HTTP/1.1"

关闭web1服务

[root@web1 ~]# systemctl stop nginx.service

查看日志

[root@proxyserver ~]# cat /var/log/haproxy.log 
Jul 24 11:42:34 localhost haproxy[3257]: 192.168.29.1:37321 [24/Jul/2020:11:42:34.896] main app/app2 0/0/1/0/1 304 173 - - ---- 1/1/0/0/+1 0/0 "GET / HTTP/1.1"
Jul 24 11:42:35 localhost haproxy[3257]: 192.168.29.1:37321 [24/Jul/2020:11:42:35.025] main app/app2 0/1/0/1/2 304 173 - - ---- 1/1/0/0/+1 0/0 "GET / HTTP/1.1"
#进行了健康检测
Jul 24 11:42:36 localhost haproxy[3257]: Server app/app1 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.

查看监控情况
在这里插入图片描述
重启web1服务

[root@web1 ~]# systemctl start nginx.service

查看日志

[root@proxyserver ~]# cat /var/log/haproxy.log 
Jul 24 11:45:59 localhost haproxy[3257]: Server app/app1 is UP, reason: Layer4 check passed, check duration: 0ms. 2 active and 0 backup servers online. 0 sessions requeued, 0 total in queue.
Jul 24 11:46:13 localhost haproxy[3257]: 192.168.29.1:37491 [24/Jul/2020:11:46:13.509] main app/app1 0/0/0/1/2 200 234 - - ---- 1/1/0/0/0 0/0 "GET / HTTP/1.1"
Jul 24 11:46:13 localhost haproxy[3257]: 192.168.29.1:37491 [24/Jul/2020:11:46:13.643] main app/app2

查看监控情况
在这里插入图片描述

猜你喜欢

转载自blog.51cto.com/14832653/2513046