haproxy 安装与配置

简介:

hi 欢迎阅读此文,此文主要讲解 haproxy 的安装与配置,还有两个shell script :

haproxy_install.sh 用于haproxy安装与配置

haproxy.sh 用于管理haproxy 服务,功能 开启 关闭 重启

此文没有介绍haproxy ,因为既然您来到这里,说明您很关心haproxy 可能早就闻其大名了(4,7层负载均衡软件),haproxy 优点与强大的功能我就不重复了,好了下面开始!

 

1 haproxy 配置文档,#号为注释,用于功能说明

update 20120904

$ cat /usr/local/haproxy/haproxy.cfg 

 
  1. #HAProxy配置中分成五部分内容,当然这些组件不是必选的,可以根据需要选择部分作为配置。 
  2. #global:参数是进程级的,通常和操作系统(OS)相关。这些参数一般只设置一次,如果配置无误,就不需要再次配置进行修改 
  3. #defaults:配置默认参数的,这些参数可以被利用配置到frontend,backend,listen组件 
  4. #frontend:接收请求的前端虚拟节点,Frontend可以根据规则直接指定具体使用后端的backend(可动态选择)。 
  5. #backend:后端服务集群的配置,是真实的服务器,一个Backend对应一个或者多个实体服务器。 
  6. #listen:Frontend和Backend的组合体。 
  7.  
  8. global 
  9.     log 127.0.0.1 local1 
  10.     maxconn 65000             #最大连接数 
  11.     chroot /usr/local/haproxy #安装目录 
  12.     uid 99                    #用户haproxy 
  13.     gid 99                    #组haproxy 
  14.     daemon                    #守护进程运行 
  15.     nbproc 2                  #进程数量 
  16.     pidfile /usr/local/haproxy/logs/haproxy.pid #haproxy pid 
  17.  
  18. defaults 
  19.    log     global 
  20.    mode    http               #7层#默认的模式mode {tcp|http|health},tcp是4层,http是7层,health只会返回OK 
  21.    option  httplog            #http 日志格式 
  22.    option  httpclose          #主动关闭http通道,HA-Proxy不支持keep-alive模式 
  23.    option  redispatch         #serverId对应的服务器挂掉后,强制定向到其他健康的服务器 
  24.    option  forwardfor         #后端服务器需要获得客户端的真实IP,将从Http Header中获得客户端IP 
  25.    option  dontlognull        #来防止记录 Alteo(4层负载均衡)发出的健康检测,如果一个 session 交互没有数据,这个 session就不会被记录 
  26.    maxconn 50000              #最大连接数 
  27.    contimeout      5000       #连接超时(毫秒) 
  28.    clitimeout      50000      #客户端超时(毫秒) 
  29.    srvtimeout      50000      #服务器超时(毫秒) 
  30.  
  31.    #errorfile 502 /usr/local/haproxy/html/maintain.html 
  32.    #errorfile 503 /usr/local/haproxy/html/maintain.html 
  33.    #errorfile 504 /usr/local/haproxy/html/maintain.html 
  34.   
  35.  
  36. frontend test.com             #定义前端服务器(haproxy) 
  37.         bind *:80             #监听地址 
  38.         # 
  39.         acl static path_end -i .jpg .png .bmg .gif .css .js 
  40.         #acl web-client path_beg -i /vsphere-client 
  41.         acl bbs hdr_reg(host) -i ^(bbs.test.com|shequ.test.com|forum|phpwind|home) 
  42.         acl blog hdr_reg(host) -i ^(blog.test.com|t) 
  43.         #acl jsp path_end -i .jsp .do 
  44.         acl monitor hdr_beg(host) -i monitor.test.com    #定义ACL名称,对应的请求的主机头是monitor.test.com  
  45.         acl www hdr_beg(host) -i www.test.com 
  46.         acl jsp hdr_reg(host) -i ^(center.test.com|java|jsp) 
  47.         # 
  48.         use_backend  tomcat.test.com if jsp   
  49.         use_backend  cache.test.com if static    
  50.         use_backend  monitor.test.com if monitor 
  51.         use_backend  bbs.test.com if bbs  
  52.         use_backend  blog.test.com if blog 
  53.         use_backend  www.test.com if www 
  54.         #use_backend  vsphere-client if web-client 
  55.         # 
  56.         default_backend www.test.com  #指定默认的后端服务器 
  57.  
  58. backend monitor.test.com      #定义后端服务器群(web server/apache/nginx/iis..) 
  59.         mode http 
  60.         option  forwardfor    #后端服务器(apache/nginx/iis/*),从Http Header中获得客户端IP 
  61.         balance leastconn     #负载均衡的方式,最小连接 
  62.         cookie  SERVERID      #插入serverid到cookie中,serverid后面可以定义 
  63.         option  httpchk HEAD /check.html #用来做健康检查html文档 
  64.         #option httpchk HEAD /index.php HTTP/1.1\r\nHost:monitor.test.com #HTTP && Host 
  65.         server  monitor 10.0.100.70:80 cookie monitor check inter 2000 rise 3 fall 3 weight 3 
  66.         #服务器定义: 
  67.         #cookie server1表示serverid为server1; 
  68.         #check inter 2000 是检测心跳频率(check 默认 ); 
  69.         #rise 3 表示 3次正确认为服务器可用; 
  70.         #fall 3 表示 3次失败认为服务器不可用; 
  71.         #weight 表示权重。 
  72.  
  73. backend bbs.test.com   
  74.         mode http 
  75.         option  forwardfor 
  76.         balance roundrobin    #负载均衡的方式,轮询方式 
  77.         cookie  SERVERID insert indirect      #插入serverid到cookie中,serverid后面可以定义 
  78.         option  httpchk HEAD /check.html  
  79.         #option httpchk HEAD /index.php HTTP/1.1\r\nHost:monitor.test.com #HTTP && Host 
  80.         server  bbs01 10.0.100.75:80 cookie bbs01 check inter 2000 rise 3 fall 3 weight 3 
  81.  
  82. backend blog.test.com 
  83.         mode http 
  84.         option  forwardfor 
  85.         balance roundrobin     
  86.         cookie  SERVERID   
  87.         option  httpchk HEAD /check.html  
  88.         server  blog01 10.0.100.76:80 cookie blog01 check inter 2000 rise 3 fall 3 weight 3 
  89.  
  90. backend www.test.com 
  91.         mode http 
  92.         option  forwardfor 
  93.         balance roundrobin    #负载均衡的方式,轮询方式 
  94.         cookie  SERVERID   
  95.         option  httpchk HEAD /check.html  
  96.         server  www01 10.0.100.71:80 cookie www01 check inter 2000 rise 3 fall 3 weight 3 
  97.  
  98. backend cache.test.com 
  99.         mode http 
  100.         option  forwardfor 
  101.         #balance uri len 15 #url hash 
  102.         balance roundrobin 
  103.         cookie SERVERID   
  104.         server squid01 10.0.100.72:80 cookie squid01 check inter 2000 rise 3 fall 3 weight 3 
  105.         server squid02 10.0.100.73:80 cookie squid02 check inter 2000 rise 3 fall 3 weight 3 
  106.  
  107. backend tomcat.test.com 
  108.         mode http 
  109.         option  forwardfor 
  110.         balance roundrobin     
  111.         cookie  SERVERID   
  112.         option  httpchk HEAD /index.html 
  113.         server  tomcat01 10.0.100.77:8080 cookie tomcat01 check inter 2000 rise 3 fall 3 weight 3 
  114.         server  tomcat02 10.0.100.78:8080 cookie tomcat02 check inter 2000 rise 3 fall 3 weight 3 
  115.  
  116. #backend vsphere-client 
  117. #        mode http 
  118. #        option  forwardfor header ORIG_CLIENT_IP 
  119. #        balance roundrobin 
  120. #        server server1 10.0.100.81:80 redir https://192.168.57.81:443 check inter 2000 rise 3 fall 3 weight 3 
  121.  
  122.  
  123. listen admin_stat                   #status 
  124.     bind 0.0.0.0:8080               #监听端口 
  125.     mode http                       #http的7层模式 
  126.     stats refresh 30s               #统计页面自动刷新时间 
  127.     stats uri /haproxy_stats_url    #统计页面URL 
  128.     stats realm Haproxy\ Statistics #统计页面密码框上提示文本 
  129.     stats auth admin:admin          #统计页面用户名和密码设置 
  130.     stats hide-version              #隐藏统计页面上HAProxy的版本信息 
  131.     stats admin if TRUE             #手工启用/禁用,后端服务器 

2 haproxy 安装脚本

这里

3 haproxy 服务脚本

这里

4 运行 haproxy

 
  1. ./haproxy.sh 
  2. usage: ./haproxy.sh {start|stop|restart} 
  3.  
  4. ./haprroxy.sh start 

5 haproxy 监控页面

http://yourip:8080/haproxy_stats_url

用户名与密码:admin

 

结束

shell 脚本如有bug ,欢迎反馈!

mail:dngood@sina.com

qq群: 37275208

 

#update 20120701

TProxy透明代理相关配置

http://yupengyan.com/quick-installation-of-haproxy-on-centos6-2.html

Configure HAProxy with TPROXY kernel for full transparent proxy

http://blog.loadbalancer.org/configure-haproxy-with-tproxy-kernel-for-full-transparent-proxy/

haproxy-doc

http://code.google.com/p/haproxy-docs/w/list



本文转自 dongnan 51CTO博客,原文链接:http://blog.51cto.com/dngood/738634

相关文章
|
6月前
|
负载均衡 Ubuntu Linux
haproxy简明配置
简介介绍haproxy的配置方法
97 1
|
8月前
|
应用服务中间件 数据安全/隐私保护 nginx
Haproxy-安装与配置
安装 Haproxy
68 0
|
负载均衡 Linux
HaProxy 安装
HaProxy 安装
|
监控 负载均衡 网络协议
|
监控 网络协议 JavaScript
|
监控 负载均衡 算法
|
监控 前端开发 JavaScript
|
监控 Linux 数据安全/隐私保护