奥尔覅几22 发表于 2019-1-1 08:00:22

haproxy都能够做些什么

haproxy凭借自身的优势,常在生产环境中用于web和数据库的负载,下面是个人对haproxy的了解。
准备:3台centos系统,1台做haproxy,2台做realserver(已经配置好用apache提供web服务),yum源,编译环境
配置步骤:
添加haproxy用户和组,用于运行haproxy,下载源码包,编译安装,haproxy的配置文件需要自己手动创建,最后使用haproxy自带脚本启动。
#groupadd -r haproxy
#useradd -r -M -g haproxy -s /sbin/nologin haproxy
#wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.20.tar.gz
#tar xf haproxy-1.4.20.tar.gz
#cd haproxy-1.4.20
#make TARGET=linux26 PREFIX=/usr/local/haproxy install
# cd /usr/local/
# chown -R haproxy. haproxy/
#cd /usr/local/haproxy/
#vim haproxy.cfg
global
   log 127.0.0.1 local0 info #
   maxconn 4096
   user haproxy
   group haproxy
   daemon
   nbproc 1
   pidfile /var/run/haproxy.pid
defaults
   maxconn 2000
   contimeout 5000
   clitimeout 30000
   srvtimeout 30000
listen    admin_status
   mode    http
   bind 192.168.1.104:8899
   option httplog
   log global
   stats enable
   stats refresh 10s
   stats hide-version
   stats realm Haproxy\ Statistics
   stats uri    /admin_status
   stats auth    haproxy :gaby
   stats admin if TRUE
   server app1_1 192.168.1.103:80 cookie app1inst1 check inter 2000 rise 2 fall 5
   server app1_2 192.168.1.104:80 cookie app1inst2 check inter 2000 rise 2 fall 5


#chown haproxy. haproxy.cfg
# cd /usr/local/haproxy/sbin/
# ./haproxy -f ../haproxy.cfg
此刻用ie访问http://192.168.1.104:8899会发现调度到两个不同的realserver中。用http://192.168.1.104:8899/admin-status输入账号密码(由 stats authhaproxy :gaby
定义)后可以查看当前haproxy的工作状态。
很多朋友喜欢sv脚本,写个呗!
# cd /etc/init.d/
#vim haproxy

#!/bin/bash
#
#haproxy      Startup script for the haproxy Server,write by gabylinux
#
# chkconfig: - 86 14
# description: haproxy
# processname: haproxy
# config: /usr/local/haproxy/haproxy.cfg
# pidfile: /usr/local/haproxy/haproxy.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
HAPROXY_CONF_FILE=/usr/local/haproxy/haproxy.cfg
haproxy="/usr/local/haproxy/sbin/haproxy"
prog=$(basename $haproxy)
lockfile=/var/lock/subsys/haproxy
start() {
    [ -x $haproxy ] || exit 5
    [ -f $HAPROXY_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $haproxy -f $HAPROXY_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
    echo "ok"
}
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -9
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
restart() {
    #configtest || return $?
    stop
    sleep 1
    start
}
reload() {
    #configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $haproxy -HUP
    RETVAL=$?
    echo
}
rh_status() {
    status $prog
}
rh_status_q() {
    rh_status >/dev/null 2>&1
}
case "$1" in
    start)
      rh_status_q && exit 0
      $1
      ;;
    stop)
      rh_status_q || exit 0
      $1
      ;;
    restart|configtest)
      $1
      ;;
    reload)
      rh_status_q || exit 7
      $1
      ;;
*)
      echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
      exit 2
esac




ps:这个脚本的pid文件必须在/var/run/目录下,同时要懂/etc/rc.d/init.d/function函数s。
# chmod +x haproxy
# chkconfig --add haproxy
验证
# service haproxy start
Starting haproxy:                                                                                    [    OK    ]
# service haproxy restart
Stopping haproxy:                                                                                    [    OK    ]
Starting haproxy:                                                                                    [    OK    ]
# service haproxy reload
Reloading haproxy:                                                                                 [    OK    ]
# service haproxy stop
Stopping haproxy:                                                                                    [    OK    ]
# service haproxy start
Starting haproxy:                                                                                    [    OK    ]



ok,到现在为止一个基本的haproxy代理web功能一启用,下面对haproxy更多功能介绍。
global段没有什么好说的,直接来listen段
listen段语法如下
listen[ :[,...] ]
从我上面的配置文件可以看出,我写的配置文件并不规范,规范的配置文件,根据不能内容定义不通的listen段,如下面代码
----------------专用于haproxy页面信息统计--------------------
listen admin_stats
bind 0.0.0.0:8899   
mode http
log 127.0.0.1 local0 err #      
stats refresh 30s   
stats uri /admin?stats
stats realm Gemini\ Haproxy
stats auth haproxy:gaby      
stats hide-version            

listen后面名字随便起,只要和下面的统计页面uri(stats uri /admin?stats )对应,同时为了安全可以设用房,密码和隐藏统计页面上HAProxy的版本信息。还可以设置统计页面的刷新时间。
自1.3版本以后引入了frontend,backend配置段,这样更显得haproxy的配置层次化和结构化。 frontend根据任意HTTP请求头内容做规则匹配,然后把请求定向到相关的backend。frontend和backend关联通过use_backend 或default_backend 实现。
-------------------------------------------backend----------------------------------------------------------
backend www
      mode http
      balance roundrobin
      cookie SERVERID
      server web1 192.168.1.103:80 cookie 1 check inter 1500 rise 3 fall 3 weight 1
      server web2 192.168.1.105:80 cookie 2 check inter 1500 rise 3 fall 3 weight 2

-----------------------------------------frontend--------------------------------------------------------------
frontend www.gaby.com
bind 0.0.0.0:8877
mode http
log global
option httplog
option httpclose
option forwardfor
monitor-uri   /site_alive
acl site_dead nbsrv(www) lt 1
monitor fail    if    site_dead
default_backend www


这俩部分内容加上上面的专用于haproxy页面信息统计的代码组成我开始时候给的配置文件中的listen段(除了我为了区别使用端口不同外)。
从给出的内容,看不出引入了frontend,backend有什么优势,但是backend可以多个,frontend可以根据不同的判断把请求转到不同的backend上。
其中:cookie SERVERID表示允许插入serverid到cookie中,这是haproxy实现多主机session共享问题解决方法之一,如我上面   server web1 192.168.1.103:80 cookie 1 check inter 1500 rise 3 fall 3 weight 1,中cookie 1就是serverid。check inter 1500 是检测心跳频率,rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重。
haproxy还可以通过reqisetbe实现动静分离。动静分离在公司随后项目会上,验证后再添加。



页: [1]
查看完整版本: haproxy都能够做些什么