您的位置:首页 > 数据库 > MySQL

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

2014-09-21 21:14 260 查看
cookie <name> [rewrite| insert | prefix] 使用场景:defaults listen backend
当我们的请求时动态站点是会哟cookie,就是当我们请求是动态页面数,会自动附在cookie信息,发送你感到动态服务器,还有session绑定,作用于一个域名或者一个域名下的url,
例子:
在backend webserver
balance roundrobin
cookie webserver(name) insert(插入的方式插入一个cookie) nocache(不缓存铭感数据)
server 172.16.249.100:80
server 172.16.249.200:80

要是我们想要cookie绑定,只需要在各个server后面加上cookie
server s1 172.16.249.100:80 cookie s1
server s2 172.16.249.200:80 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
2:log <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 定义一个cookie的session绑定
#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:后端中的备用backup,server
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服务的配置示例
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息