haproxy运用总结

haproxy使用总结

DESCRIPTION
       HAProxy is a TCP/HTTP reverse proxy which is particularly suited for high availability environments.  Indeed,  it
       can:
        - route HTTP requests depending on statically assigned cookies ;
        - spread the load among several servers while assuring server
          persistence through the use of HTTP cookies ;
        - switch to backup servers in the event a main one fails ;
        - accept connections to special ports dedicated to service
          monitoring ;
        - stop accepting connections without breaking existing ones ;
        - add/modify/delete HTTP headers both ways ;
        - block requests matching a particular pattern ;
        - hold clients to the right application server depending on
          application cookies
        - report detailed status as HTML pages to authenticated users from an
          URI intercepted from the application.

       It needs very little resource. Its event-driven architecture allows it to easily handle thousands of simultaneous
       connections on hundreds of instances without risking the system's stability.

  • 负载均衡
     global
            daemon
            maxconn 256
    
        defaults
            mode tcp
            timeout connect 5000ms
            timeout client 50000ms
            timeout server 50000ms
    
        frontend testhost
            bind *:1998
            default_backend servers
    
        backend servers
            server server52 127.0.0.1:52 check
            server server872 127.0.0.1:872 check
            server server42 127.0.0.1:42 check
            retries 3
            redispatch
    
     
  • 灾难备份
    global
            daemon
            maxconn 256
            log 127.0.0.1   local3
            pidfile /tmp/haproxy.pid
            #stats socket /tmp/haproxy_1.5_dev_11.stats.socket
    
    
        defaults
            mode tcp
            timeout connect 5000ms
            timeout client 50000ms
            timeout server 50000ms
            log     127.0.0.1       local3
            option log-health-checks
    
    
        frontend testhost
            bind *:1998
            default_backend servers
    
        backend servers
            server server52 127.0.0.1:52 check
            server server872 127.0.0.1:872 check backup
    
     
  • 热启动(修改config让程序立即生效)
    #!/bin/bash
    #haproxy_hot_reload_config.sh
    #the program will restart the haproxy by using  its hot reconfiguration
    
    config=" /home/prj/haproxy_conf/haproxy.cfg.mysql212"
    pidfile=`grep pidfile $config | awk  '{print   $2}'`
    pid=`cat $pidfile`
    status=-2
    
    if [ -e /proc/$pid/exe ]; then
       /usr/local/sbin/haproxy -D -f  $config -sf $pid
       status=0
    else
       /usr/local/sbin/haproxy -D -f  $config
       status=1
    fi
    
    echo $status
    exit $status