[负载均衡]haproxy

这东西非常强大..配置语法跟之前tengine的类似.

1.健康检查的例子:

主要是使用check关键字:

#
# demo config for Proxy mode
#

global
        maxconn         20000
#   ulimit-n    20000
    # ulimit-n  16384
        log             127.0.0.1 local0
    # log /tmp/haproxy.log
        uid             200
        gid             200
#        chroot          /var/empty
    nbproc      4
        daemon

frontend test-proxy
    bind        0.0.0.0:81
    # bind      192.168.200.10:8080
        mode            http
        log             global
        option          httplog
        option          dontlognull
        option          nolinger
        option          http_proxy
        maxconn         8000
        timeout client  30s

    # layer3: Valid users
#   acl allow_host src 192.168.200.150/32
#   http-request deny if !allow_host

    # layer7: prevent private network relaying
#   acl forbidden_dst url_ip 192.168.0.0/24
#   acl forbidden_dst url_ip 172.16.0.0/12
#   acl forbidden_dst url_ip 10.0.0.0/8
#   http-request deny if forbidden_dst

    default_backend test-proxy-srv


backend test-proxy-srv
    mode            http
    timeout connect 5s
    timeout server  5s
    retries         2
#   option          nolinger
    option          http_proxy

    # layer7: Only GET method is valid
    acl valid_method        method GET
    http-request deny if !valid_method

    # layer7: protect bad reply
    # http-response deny if { res.hdr(content-type) audio/mp3 }

    balance roundrobin  #负载均衡的方式,roundrobin平均方式
#   cookie SERVERID     #允许插入serverid到cookie中,serverid后面可以定义

#心跳检测的URL,HTTP/1.1¥r¥nHost:XXXX,指定了心跳检测HTTP的版本,XXX为检测时请求
#服务器的request中的域名是什么,这个在应用的检测URL对应的功能有对域名依赖的话需要设置
    option httpchk GET / HTTP/1.0

  #服务器定义,cookie 1表示serverid为1,check inter 1500 是检测心跳频率
  #rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重
    server mms1 127.0.0.1:6000 cookie 1 check inter 3000 rise 3 fall 3 weight 1
    server mms2 127.0.0.1:6001 cookie 2 check inter 3000 rise 3 fall 3 weight 2

关于haproxy其实还是不太理解,可以从简单的着手。

2.配置一个简单的本地代理

可以写成这样,一个frontend+一个backend:


  # Simple configuration for an HTTP proxy listening on port 80 on all
    # interfaces and forwarding requests to a single backend "servers" with a
    # single server "server1" listening on 127.0.0.1:8000
    global
        daemon
        maxconn 256

    defaults
        mode http
        timeout connect 5000ms
        timeout client 50000ms
        timeout server 50000ms

    frontend http-in
        bind *:81
        default_backend servers

    backend servers
        server server1 127.0.0.1:80 maxconn 32

也可以写成单独的listen:

 # The same configuration defined with a single listen block. Shorter but
    # less expressive, especially in HTTP mode.
    global
        daemon
        maxconn 256

    defaults
        mode http
        timeout connect 5000ms
        timeout client 50000ms
        timeout server 50000ms

    listen http-in
        bind *:81
        server server1 127.0.0.1:80 maxconn 32

3.日志记录

这里还缺了一个很关键的日志记录,日志需要和syslog软件相结合。
需要配置一下rsyslog:

$ModLoad imudp
$UDPServerRun 514
local1.* /var/log/haproxy.log

然后在代码中配置相关内容即可:


  # Simple configuration for an HTTP proxy listening on port 80 on all
    # interfaces and forwarding requests to a single backend "servers" with a
    # single server "server1" listening on 127.0.0.1:8000
    global
        daemon
        maxconn 256
        log 127.0.0.1 local1

    defaults
        log global
        mode http
        timeout connect 5000ms
        timeout client 50000ms
        timeout server 50000ms
        option httplog

    frontend http-in
        bind *:81
        default_backend servers

    backend servers
        server server1 127.0.0.1:80 maxconn 32

4.配置statistics

listen stats
    bind *:82
    mode http
    stats enable
    stats hide-version
    stats realm Haproxy\ Statistics
    stats uri /
    stats auth admin:Qq123456

haproxy提供一个很方便的monitor界面,比nginx_status好看多了.内容也丰富很多.很直观.

阅读更多
文章标签: 负载均衡 haproxy
个人分类: haproxy
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭