负载均衡之HAproxy(三)


一. HAproxy特点

  • 支持tcp/http 两种协议层的负载均衡,使得其负载均衡功能非常丰富。

  • 支持8种左右的负载均衡算法,尤其是在http模式时,有许多非常实在的负载均衡算法,适用各种需求。

  • 性能非常优秀,基于事件驱动的链接处理模式及单进程处理模式(和Nginx类似)让其性能卓越。

  • 拥有一个功能出色的监控页面,实时了解系统的当前状况。

  • 功能强大的ACL支持,给用户极大的方便。

二. 处理模式

  1. 单进程处理模式

    所有客户端连接全部都由同一个服务进程来处理,目标就是等待连接,来一个分配一个,主要消耗cpu

  2. 多线程处理模式

    多线程模式消耗内存,会限制并发而且多线程需要进程间通信,也会消耗相当多的cpu资源

三. 测试案例

  环境:

  haproxy: 192.168.93.137

  web1: 192.168.93.138

  web2: 192.168.93.139

  client: 192.168.93.140
  1. ntp对时

  三台服务器都要安装ntp

yum -y install ntp 

  在haproxy端配置ntp 并启动ntp服务

# vim /etc/ntp.conf

  #server [0.centos.pool.ntp.org](http://0.centos.pool.ntp.org) iburst     # 注释这4行

  #server [1.centos.pool.ntp.org](http://1.centos.pool.ntp.org) iburst

  #server [2.centos.pool.ntp.org](http://2.centos.pool.ntp.org) iburst

  #server [3.centos.pool.ntp.org](http://3.centos.pool.ntp.org) iburst

  server 127.127.1.0                # 添加以下2行

  fudge  127.127.1.0 stratum 10

# systemctl start/enable ntpd         # 启动服务

  在两台web服务器 进行ntp对时

ntpdate 192.168.93.137

  ntp网络对时:

ntpdate  1.cn.pool.ntp.org
  1. 安装并配置HAproxy
yum -y install haproxy

# vim /etc/haproxy/haproxy.cfg    (清空原有文件)

  global                         //全局配置

    log 127.0.0.1 local3 info           //日志配置

    maxconn 4096                //最大连接数(优先级低)

    uid nobody

    gid nobody

    daemon                     //守护进程

    nbproc 1                   //haproxy进程数

  defaults                            //默认配置(如果listen和backend块没有进行设置,则使用该块设置)

    log     global                 //日志需要全局配置

    mode    http                 //模式  7层LB  基于http

    maxconn 2048                 //最大连接数(优先级中)

    retries 3                       //健康检查 3次失败后服务不可用

    option  redispatch                          //服务不可用后 重定向到其他服务器

    stats   uri  /monitor             //监控页面 192.168.93.137/monitor

    stats auth   liang:xx                       //登录监控界面账号密码

    contimeout          5000     //定义haproxy将客户端请求转发至后端服务器,所等待的超时时长

    clitimeout          50000     //haproxy作为客户端向后端服务端之间空闲连接的超时时间

    srvtimeout          50000     //haproxy作为服务端和客户端之间空闲连接的超时时间

  frontend http-in                   //前端配置块  对用户

    bind 0.0.0.0:80                //面对用户的地址和端口

    mode http                   //模式  7层LB  基于http

    log global                   //日志使用全局配置

    option httplog               //使日志格式变的丰富,其通常包括但不限于HTTP请求、连接计时器、会话状态、连接数、捕获的首部及cookie、“frontend”、“backend”及服务器名称,当然也包括源地址和端口号等

    option httpclose             //HAProxy会针对客户端的第一条请求的返回 添加cookie并返回给客户端,客户端发送后续请求时会发送 此cookie到HAProxy,HAProxy会针对此cookie分发到上次处理此请求的服务器上,如果服务器不能忽略此cookie值会影响处理结果

    acl html url_reg  -i  \.html$          //访问控制列表 名字:html  规则:以.html结尾

    use_backend html-server if html     //满足html条件 转向后端 html-server

    default_backend html-server       //默认后端服务器html-server

  backend html-server                         //后端服务器名字

    mode http                           //模式  7层LB  基于http

    balance roundrobin                  //rr轮询

    option httpchk GET /index.html      //允许http检查server健康

    cookie SERVERID insert indirect nocache  //在转发客户端请求时插入cookie再转给真实服务器。如果第二次访问就把cookie换掉

    server html-A 192.168.93.138:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5

    server html-B 192.168.93.139:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5

    //对后端服务器的健康状况检查间隔为2000毫秒,连续2次健康检查成功,则认为是有效的,连续5次健康检查失败则认为服务器宕机  html-A 不同即可  weight权重  cookie不同即可

    注:若用基于端口的虚拟主机,虚拟主机的主页面必须是index.html
    

# systemctl start haproxy
  1. client访问测试
# curl 192.168.93.137

访问监控页面:

  浏览器输入:http://192.168.93.137/monitor    //配置文件中定义

四. HAproxy实现动静分离

  环境:

  client: 192.168.93.136

  haproxy: 192.168.93.137 

  web1: 192.168.93.138

  web2: 192.168.93.139

  php1: 192.168.93.140

  php2: 192.168.93.142
  1. 所有集群配置ntp对时
  2. web1 web2 配置index.html (2台机器指定不同的页面,方便观察)
yum -y install nginx
  1. php1 php2 配置index.php (2台机器指定不同的页面)
yum -y install php-fpm php-mysql php-gd nginx

vim /etc/nginx/nginx.conf

location ~ \.php$ {

  root           /usr/share/nginx/html;

  fastcgi_pass   127.0.0.1:9000;

  fastcgi_index  index.php;

  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

  include        fastcgi_params;

    }

  # scp /usr/share/nginx/html/index.php 192.168.93.142:/usr/share/nginx/html/index.php

  # scp /etc/nginx/nginx.conf 192.168.93.142:/etc/nginx/nginx.conf

  # systemctl restart nginx

  # systemctl restart php-fpm
  1. 安装并配置HAproxy
vim /etc/haproxy/haproxy.cfg

global

  log 127.0.0.1 local3 info

  maxconn 4096

  uid nobody

  gid nobody

  daemon

  nbproc 1
  
defaults

  log     global

  mode    http

  maxconn 2048

  retries 3

  option  redispatch

  stats   uri  /monitor

  stats auth      liang:xx

  contimeout          5000

  clitimeout          50000

  srvtimeout          50000
  
frontend http-in

  bind 0.0.0.0:80

  mode http

  log global

  option httplog

  option httpclose

  acl html url_reg  -i  \.html$

  acl php  url_reg -i  \.php$             //配置php访问控制

  use_backend html-server if html 

  use_backend php-server if php       //配置php虚拟主机

  default_backend html-server       //默认为html-server
  
backend html-server

  mode http

  balance roundrobin

  option httpchk GET /index.html

  cookie SERVERID insert indirect nocache

  server html-A 192.168.93.138:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5

  server html-B 192.168.93.139:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5

backend php-server

  mode http

  balance roundrobin

  option httpchk GET /index.php

  cookie SERVERID insert indirect nocache

  server html-A 192.168.93.140:80 weight 1 cookie 6 check inter 2000 rise 2 fall 5

  server html-B 192.168.93.142:80 weight 1 cookie 7 check inter 2000 rise 2 fall 5

  1. client端访问测试
# curl 192.168.93.137         //只会轮询静态页面 因为html-server为默认组

# curl 192.168.93.137/index.php     //访问动态界面
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偷学技术的梁胖胖yo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值