设为首页 收藏本站
查看: 890|回复: 0

[经验分享] haproxy的stats管理和负载均衡mysql的实例

[复制链接]

尚未签到

发表于 2019-1-2 09:31:31 | 显示全部楼层 |阅读模式
  
   cookie  [rewrite| insert | prefix]  使用场景:defaults   listen  backend
  当我们的请求时动态站点是会哟cookie,就是当我们请求是动态页面数,会自动附在cookie信息,发送你感到动态服务器,还有session绑定,作用于一个域名或者一个域名下的url
  例子:
      backend webserver
         balance  roundrobin
          cookie webservername  insert(插入的方式插入一个cookie  nocache(不缓存铭感数据)  
        server 172.16.249.10080
          server 172.16.249.20080
       
  要是我们想要cookie绑定,只需要在各个server后面加上cookie
      server s1 172.16.249.10080 cookie  s1
  server  s2 172.16.249.20080  cookie s2
  注意:目的是session的绑定,就是第一次选定的server是谁,以后也不会变,所发的请求会发到同一个server
     haroxy的工作模式:mode
        httpd:调度httpd协议的服务器
              会对应用层数据作深入分析,支持7层过滤,处理,装换等机制   
        tcp:非http协议的服务器调度,包括https
  mode{http|tcp|health(健康状态监测)}
   工作于: defaults frontend  listen backend
  例子:
     frontend main
       bind *;80
       mode tcp| httpd
       default_backend  webserver
  tcp模式:不会对应用层协议做任何检查      
  指定服务器代理的日志:
    log global
  2log  
   没一个代理都可以有一个log
  
      捕获请求报文首部
       capture rquest  hesder host len 15 捕获以       host首部的15个字符
      capture rquest  hesder X-forwarded-For len 15
  hash-type
    hash-type
  推荐对cache servers负载均衡调度
          
     
  服务器端:eth0:172.16.249.229
        Eth1:192.16.20.1
  Server端:
  Server1:192.168.20.2
  Server1:192.168.20.3
  定义显示stats页面:haproxy
     是在backend 中定义一个stats enable来显示状态页,
  例子
   frontend main
    maxconn 6000:最大并发数
    bind :80:绑定端口
    default_backend  webserver :调用下面的backend
  
  backend webserver
     balance     roundrobin
  # cookie webserver insert nocache  定义一个cookiesession绑定
  #server s1 192.168.20.2  cookies1 check port 80 maxconn 4000  weight 2;cookie定义案例
  #server s2 192.168.20.3 cookie s2 check port 80 maxconn 4000  weight 1; cokie定义案例
     server s1 192.168.20.2 check port 80 maxconn 4000  weight 2 :定义查看80的健康检查,最大并发数为4000 权重2
     server s2 192.168.20.3check port 80 maxconn 2000  weight 1定义查看80的健康检查,最大并发数为4000 权重1
   server b1 127.0.0.1:8080    backup check port 8080:做备份用,server不能工作时启用
     stats enable :启用stats状态页
     
  挺掉任意一个sever时,会变成红色
    Active:后端服务器中的server
    Backuo:后端中的备用backupserver
  Stats定义方式:
      backend public_www
      server websrv1172.16.100.11:80
      stats enable:启用stats状态页
      stats hide-version:可以隐藏其版本号
      stats scope . stats作用的路径’.’当前路径
      stats uri     /haproxyadmin?stats:自定义路径,登陆
      stats realm   Haproxy\ Statistics 定义当你以用户密码登陆时会显示出来
      stats auth    statsadmin:password :定义用户:密码
stats auth    statsmaster:password;定义用户:密码
  例子2
  
frontend main
maxconn 6000
bind :80
default_backend  webserver

backend webserver
  balance     roundrobin
# cookie webserver insert nocache
  server s1 192.168.20.2 check port 80 maxconn 4000  weight 2
  server s2 192.168.20.3 check port 80 maxconn 2000  weight 1
  server b1 127.0.0.1:8080    backup
  stats enable
  stats hide-version
  stats uri /hap?stats
  stats scope .
  stats realm HAPprxy\ statistics
  stats auth han:han
这样我们会就可以测试了
显示:

在输入账号和密码显示:

这个页面也可以实现管理haproxy的功能:stats admin {if | unless}
   在指定的条件满足时启用统计报告页面的管理级别功能,它允许通过web接口启用或禁用服务器,不过,基于安全的角度考虑,统计报告页面应该尽可能为只读的。此外,如果启用了HAProxy的多进程模式,启用此管理级别将有可能导致异常行为。

目前来说,POST请求方法被限制于仅能使用缓冲区减去保留部分之外的空间,因此,服务器列表不能过长,否则,此请求将无法正常工作。因此,建议一次仅调整少数几个服务器。下面是两个案例,第一个限制了仅能在本机打开报告页面时启用管理级别功能,第二个定义了仅允许通过认证的用户使用管理级别功能。

backend stats_localhost:定义当你使用当前主机时,允许你可以管理
   stats enable
   stats admin if LOCALHOST

backend stats_auth:定义当你使用密码认证过后允许你管理haproxy
   stats enable
   stats auth  haproxyadmin:password
   stats admin if TRUE   
例子2最后加上   stats admin if TRUE(大写),在次登陆就可以管理haproxyle

我们也可以自己定义一个listen段,来定义其stats
例子
   四、配置案例

4.1 http服务器配置示例

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
   # to have these messages end up in /var/log/haproxy.log you will
   # need to:
   #
   # 1) configure syslog to accept network log events.  This is done
   #    by adding the '-r' option tothe SYSLOGD_OPTIONS in
   #    /etc/sysconfig/syslog
   #
   # 2) configure local2 events to go to the /var/log/haproxy.log
   #   file. A line like thefollowing can be added to
   #   /etc/sysconfig/syslog
   #
   #    local2.*                       /var/log/haproxy.log
   #
   log         127.0.0.1 local2

   chroot      /var/lib/haproxy
   pidfile     /var/run/haproxy.pid
   maxconn     4000
   user        haproxy
   group       haproxy
   daemon

defaults
   mode                    http
   log                     global
   option                  httplog
   option                  dontlognull
   option http-server-close
   option forwardfor       except127.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                 30000

listen stats
   mode http
   bind 0.0.0.0:1080
   stats enable
   stats hide-version
   stats uri     /haproxyadmin?stats
   stats realm   Haproxy\ Statistics
   stats auth    admin:admin
   stats admin if TRUE


frontend http-in
   bind *:80
   mode http
   log global
   option httpclose
   option logasap
   option dontlognull
    capture request  header Host len 20
   capture request  header Refererlen 60
   default_backend servers

frontend healthcheck
   bind :1099
   mode http
   option httpclose
   option forwardfor
   default_backend servers

backend servers
balance roundrobin
   server websrv1 192.168.10.11:80 check maxconn 2000
   server websrv2 192.168.10.12:80 check maxconn 2000
   但是这样你登陆是会发现我们的权限也变小了
  
  4.2 负载均衡MySQL服务的配置示例
  listen stats

  mode http
  bind :10 自定义的端口
  stats enable
  stats hide-version
  stats uri     /haproxyadmin?stats
  stats realm   Hap\ S 自定义我们在浏览器上的输出stats的路径
  stats auth    han:han
  stats admin if TRUE
  

  

  frontend mysql
  maxconn 6000
  bind *:3306
  mode tcp
  log global
  default_backend mysqlservers
  

  backend mysqlservers
  balance leastconn
  server dbsrv1 192.168.20.2:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300
  server dbsrv2 192.168.20.3:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300
  

  

  





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-658531-1-1.html 上篇帖子: 一个拆分haproxy serverlist的小脚本 下篇帖子: 初识千万级高并发负载均衡软件HaProxy
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表