HAProxy

来源:互联网 发布:js 日期不支持 format 编辑:程序博客网 时间:2024/06/16 02:22
参考:
http://blog.itpub.net/25704976/viewspace-1319781/
http://blog.csdn.net/nimasike/article/details/48048341
http://www.jb51.net/article/80172.htm
http://bestvivi.com/2015/09/06/MySQL%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB%E5%8E%9F%E7%90%86%E5%8F%8A%E6%90%AD%E5%BB%BA/


1、
wget http://www.haproxy.org/download/1.4/src/haproxy-1.4.24.tar.gz
wget http://www.haproxy.org/download/1.5/src/haproxy-1.5.19.tar.gz

tar -zxvf haproxy-1.4.24.tar.gz
tar -zxvf haproxy-1.5.19.tar.gz

2、
安装依赖的包
yum install gcc gcc-c++ make zlib-devel bzip2-devel openssl-devel

3、
cd haproxy-1.4.24
make TARGET=linux26 ARCH=x86_64    #ARCH指定CPU架构,TARGET是指定内核版本(uname -a 或 cat /proc/version)
make install


4、安装完毕后,创建配置文件和启动文件
mkdir /etc/haproxy
cp examples/haproxy.cfg /etc/haproxy
cp examples/haproxy.init /etc/init.d/haproxy
chmod +x /etc/init.d/haproxy
ln -s /usr/local/sbin/haproxy /usr/sbin/
mkdir /usr/share/haproxy

5、编辑配置文件(俩台haproxy配置文件要相同)
#vim /etc/haproxy/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
    log 127.0.0.1   local0  #日志输出配置,所有日志都记录在本机,通过local0输出
    log 127.0.0.1   local1 notice
    #log loghost    local0 info
    maxconn 4096                #最大连接数
    chroot /usr/share/haproxy   #改变当前工作目录。
    uid 99                  #所属用户的uid
    gid 99                  #所属运行的gid
    daemon                  #以后台形式运行haproxy
    #debug
    #quiet

defaults
    log global
    mode    http
  #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
    option  httplog
    option  dontlognull
    option   redispatch
  #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
    option  abortonclose
  #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
    retries 3               #两次连接失败就认为是服务器不可用
    maxconn 2000            #默认的最大连接数
  #timeout http-keep-alive 10s
  # timeout queue 1m
    contimeout  5000        #连接超时
    clitimeout  50000       #客户端超时
    srvtimeout  50000       #服务器超时
    timeout check 5s            #心跳检测超时
    stats refresh 30s           #统计页面自动刷新时间
    stats uri  /stats           #统计页面url
    stats realm baison-test-Haproxy         #统计页面密码框上提示文本
    stats auth admin:admin123           #统计页面用户名和密码设置
    stats hide-version                  #隐藏统计页面上HAProxy的版本信息
frontend www
    bind *:80
    #这里建议使用bind *:80的方式,要不然做集群高可用的时候有问题,vip切换到其他机器就不能访问了。
    acl web hdr(host) -i www.zhirs.com
    #acl后面是规则名称,-i是要访问的域名,如果访问www.zhirs.com这个域名就分发到下面的webserver 的作用域。
    acl img hdr(host) -i img.zhirs.com
    #如果访问img.baison.com.cn就分发到imgserver这个作用域。
    use_backend webserver if web
    use_backend imgserver if img

backend webserver             #webserver作用域
    mode http
    balance   roundrobin     
    #banlance roundrobin 轮询,balance source 保存session值,支持static-rr,leastconn,first,uri等参数
    option  httpchk /index.html
    #检测文件,如果分发到后台index.html访问不到就不再分发给它
    server     web01 192.168.0.201:80  check inter 2000 fall 3 weight 30
   server     web01 192.168.0.202:80  check inter 2000 fall 3 weight 20
   server     web01 192.168.0.203:80  check inter 2000 fall 3 weight 10

backend imgserver
    mode http
    option  httpchk /index.php
    balance     roundrobin                         
    server      img01 192.168.0.101:80  check inter 2000 fall 3
    server      img02 192.168.0.102:80  check inter 2000 fall 3

5、启动haproxy 服务,查看状态
service haproxy start

原创粉丝点击