21fsdsd 发表于 2016-9-20 10:31:47

haproxy学习之安装部署和应用

haproxy学习之安装部署和应用


【概念】
frontend: 书写的规则 有规则如何去匹配后端pool。
backend: 就是定义的后端的pool,RS就像服务器组。

一般是一个frontend和多个backend结合。
【实验部署环境】
192.168.100.10haproxy 80端口
192.168.100.20nginx 三个虚拟主机 www.test.com bbs.test.com blog.test.com

【nginx的虚拟主机配置文件】
server {
      listen 80 default_server;
      server_name _;
      root /usr/local/nginx/html;
}

#注意这个默认的虚拟主机,用于curl 192.168.100.20 的访问和和提供监控检查的页面。

server {
      listen 80;
      root /usr/local/nginx/html/www;
      server_name www.test.com;
      access_log /usr/local/nginx/logs/www_access.log;
      autoindex on;
      location / {
                allow all;
      }
}

server {
      listen 80;
      root /usr/local/nginx/html/bbs;
      server_name bbs.test.com;
      access_log /usr/local/nginx/logs/bbs_access.log;
      autoindex on;
      location / {
                allow all;
      }
}

server {
      listen 80;
      root /usr/local/nginx/html/blog;
      server_name blog.test.com;
      access_log /usr/local/nginx/logs/blog_access.log;
      autoindex on;
      location / {
                allow all;
      }
}

【HA的配置文件】
# cat haproxy.conf
#Global settings
global
    log 127.0.0.1:514 local3 info###
    maxconn 20480
    chroot /usr/local/haproxy/var/chroot
    user haproxy #为haproxy 用户的uid ,haproxy用户需要自己手动创建
    group haproxy
    daemon#后台运行
    quiet
    nbproc 2
    pidfile /usr/local/haproxy/var/run/haproxy.pid

##---------------------------------------------------------------------
## common defaults that all the 'listen' and 'backend' sections will
## use if not designated in their block
##---------------------------------------------------------------------
defaults
    log global   ####采用全局定义的日志
    mode http   ###默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
    maxconn 20480
    option httplog
    option httpclose
    option forwardfor
    option dontlognull ###不记录健康检查的日志信息
    option redispatch###serverId对应的服务器挂掉后,强制定向到其他健康的服务器
    retries 3   #3次连接失败就认为服务不可用,也可以通过后面设置
    balance roundrobin
    contimeout 5000
    clitimeout 50000
    srvtimeout 50000
    option httpchk GET /check.html

frontend www
    bind 192.168.100.10:80
    acl www_domain hdr(host) -i www.test.com
    acl bbs_domain hdr(host) -i bbs.test.com

    redirect prefix http://bbs.baidu.com code 301 if bbs_domain
    use_backend www if www_domain
    default_backend blog


backend www
    balance leastconn
    option httpclose
    option forwardfor
    server host3 192.168.100.20:80 check inter 2000 rise 3 fall 3

backend blog
    balance leastconn
    option httpclose
    option forwardfor
    server host3 192.168.100.20:80 checkinter 2000 rise 3 fall 3


listen status
    bind 192.168.100.10:8080
    stats enable
    stats uri /stats   #管理地址
    stats auth admin:123456#管理用户和密码

#注意这个监控检查是= curl -I 192.168.100.20/check.html
而不是 curl -I www.test.com/check.html的域名形式。

页: [1]
查看完整版本: haproxy学习之安装部署和应用