您的位置:首页 > 博客中心 > 数据库 >

haproxy的stats管理和负载均衡mysql的实例

时间:2022-03-14 00:07

 cookie <name> [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 <address>

 没一个代理都可以有一个log

 

    捕获请求报文首部

     capture rquest  hesder host len 15 捕获以       host首部的15个字符

    capture rquest  hesder X-forwarded-For len 15

hash-type

  hash-type <method>

推荐对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 <name>中定义一个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.2check 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}<cond>

   在指定的条件满足时启用统计报告页面的管理级别功能,它允许通过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服务的配置示例


#---------------------------------------------------------------------

# 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 to the SYSLOGD_OPTIONS in

    #    /etc/sysconfig/syslog

    #

    # 2) configure local2 events to go to the /var/log/haproxy.log

    #   file. A line like the following 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                    tcp

    log                     global

    option                  httplog

    option                  dontlognull

    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                 600


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 mysql

    bind *:3306

    mode tcp

    log global

    default_backend mysqlservers


backend mysqlservers

    balance leastconn

    server dbsrv1 192.168.10.11:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300

    server dbsrv2 192.168.10.12:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300




本文出自 “” 博客,请务必保留此出处

热门排行

今日推荐

热门手游