haproxy- 2016-0726

本文详细介绍HAProxy的安装步骤及配置方法,包括配置文件解析、日志设置、启动脚本编写等内容,并提供常见错误排查技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实例1: 配置文件:haproxy.conf

global

log 127.0.0.1local0

log 127.0.0.1local1 notice

#log loghostlocal0 info

maxconn 65535

#chroot /usr/share/haproxy

user haproxy

group haproxy

daemon

nbproc 10

#debug

#quiet

defaults

logglobal

modetcp

retries3

option redispatch

option  dontlognull

#option  forwardfor

maxconn65535

contimeout5000

clitimeout50000

srvtimeout50000

listen  web_1 0.0.0.0:80

        mode tcp

        server  nginx_1 *:80

        server  nginx_2 *:80

listen  web_2 0.0.0.0:18000

        mode tcp

        server  nginx_1 *:18000

        server  nginx_2 *:18000


一、环境说明
实验环境
OS CentOS5.4
192.168.0.14    proxy
192.168.0.24    web1
192.168.0.64    web2


官方地址:http://haproxy.1wt.eu/
下载地址:http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz


二、软件安装

点击(此处)折叠或打开

  1. [root@cacti src]# tar xzvf haproxy-1.4.24.tar.gz

  2. [root@cacti haproxy-1.4.24]# make TARGET=linux26 PREFIX=/usr/local/haproxy

  3. [root@cacti haproxy-1.4.24]# make install PREFIX=/usr/local/haproxy



三、创建配置文件
haproxy 默认是没有配置文件的,需要自己手机创建

vim /etc/haproxy.cfg

点击(此处)折叠或打开

  1. global

  2.     log 127.0.0.1 local3

  3.     maxconn 20480

  4.     chroot /usr/local/haproxy

  5.     uid 1004 #1004为haproxy 用户的uid ,haproxy用户需要自己手动创建

  6.     gid 1004

  7.     daemon

  8.     quiet

  9.     nbproc 1

  10.     pidfile /var/run/haproxy.pid

  11. defaults

  12.     log global

  13.     mode http

  14.     maxconn 20480

  15.     option httplog

  16.     option httpclose

  17.     option forwardfor

  18.     option dontlognull

  19.     option redispatch

  20.     retries 3

  21.     balance roundrobin

  22.     contimeout 5000

  23.     clitimeout 50000

  24.     srvtimeout 50000

  25. listen web_poll 192.168.0.14:80

  26.     mode http

  27.     option httplog

  28.     option dontlognull

  29.     option logasap

  30.     option forwardfor

  31.     option httpclose

  32. option httpchk GET /index.html

  33. server web1 192.168.0.24:80 cookie 1 check inter 2000 rise 3 fall 3

  34. server web2 192.168.0.64:80 cookie 1 check inter 2000 rise 3 fall 3

  35. listen status 192.168.0.14:8080

  36.     stats enable

  37.     stats uri /stats

  38.     stats auth admin:123456

  39.     stats realm (Haproxy\ statistic)


四、添加日志

点击(此处)折叠或打开

  1. [root@cacti ~]# vim /etc/syslog.conf

  2. 添加:

  3. local3.* /var/log/haproxy.log

  4. local0.* /var/log/haproxy.log

  5. [root@cacti ~]# vim /etc/sysconfig/syslog

  6. 修改:

  7. SYSLOGD_OPTIONS="-r -m 0"

  8. service syslog restart


五、创建haproxy启动脚本

点击(此处)折叠或打开

  1. [root@cacti ~]# vim /etc/init.d/haproxy

  2. #!/bin/bash

  3. #

  4. # haproxy

  5. #

  6. # chkconfig: 35 85 15

  7. # description: HAProxy is a free, very fast and reliable solution \

  8. # offering high availability, load balancing, and \

  9. # proxying for TCP and HTTP-based applications

  10. # processname: haproxy

  11. # config: /etc/haproxy.cfg

  12. # pidfile: /var/run/haproxy.pid


  13. # Source function library.

  14. . /etc/rc.d/init.d/functions


  15. # Source networking configuration.

  16. . /etc/sysconfig/network


  17. # Check that networking is up.

  18. [ "$NETWORKING" = "no" ] && exit 0


  19. config="/etc/haproxy.cfg"

  20. exec="/usr/local/haproxy/sbin/haproxy"

  21. prog=$(basename $exec)


  22. [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog


  23. lockfile=/var/lock/subsys/haproxy


  24. check() {

  25.     $exec ---f $config

  26. }


  27. start() {

  28.     $exec ---f $config

  29.     if [ $? -ne 0 ]; then

  30.         echo "Errors in configuration file, check with $prog check."

  31.         return 1

  32.     fi

  33.  

  34.     echo -n $"Starting $prog: "

  35.     # start it up here, usually something like "daemon $exec"

  36.     daemon $exec --f $config -p /var/run/$prog.pid

  37.     retval=$?

  38.     echo

  39.     [ $retval -eq 0 ] && touch $lockfile

  40.     return $retval

  41. }


  42. stop() {

  43.     echo -n $"Stopping $prog: "

  44.     # stop it here, often "killproc $prog"

  45.     killproc $prog 

  46.     retval=$?

  47.     echo

  48.     [ $retval -eq 0 ] && rm -f $lockfile

  49.     return $retval

  50. }


  51. restart() {

  52.     $exec ---f $config

  53.     if [ $? -ne 0 ]; then

  54.         echo "Errors in configuration file, check with $prog check."

  55.         return 1

  56.     fi

  57.     stop

  58.     start

  59. }


  60. reload() {

  61.     $exec ---f $config

  62.     if [ $? -ne 0 ]; then

  63.         echo "Errors in configuration file, check with $prog check."

  64.         return 1

  65.     fi

  66.     echo -n $"Reloading $prog: "

  67.     $exec --f $config -p /var/run/$prog.pid -sf $(cat /var/run/$prog.pid)

  68.     retval=$?

  69.     echo

  70.     return $retval

  71. }


  72. force_reload() {

  73.     restart

  74. }


  75. fdr_status() {

  76.     status $prog

  77. }


  78. case "$1" in

  79.     start|stop|restart|reload)

  80.         $1

  81.         ;;

  82.     force-reload)

  83.         force_reload

  84.         ;;

  85.     checkconfig)

  86.         check

  87.         ;;

  88.     status)

  89.         fdr_status

  90.         ;;

  91.     condrestart|try-restart)

  92.       [ ! -f $lockfile ] || restart

  93.     ;;

  94.     *)

  95.         echo $"Usage: $0 {start|stop|status|checkconfig|restart|try-restart|reload|force-reload}"

  96.         exit 2

  97. esac


六、启动

点击(此处)折叠或打开

  1. [root@cacti etc]# /etc/init.d/haproxy start

  2. Starting haproxy: [ OK ]

  3. [root@cacti etc]# ps -ef | grep haproxy

  4. 100 2305 1 0 17:55 ? 00:00:00 /usr/local/sbin/haproxy --f /etc/haproxy.cfg -p /var/run/haproxy.pid

  5. root 2308 774 0 17:55 pts/0 00:00:00 grep haproxy

  6. [root@cacti etc]#


报错:

点击(此处)折叠或打开

  1. [root@cacti ~]# /etc/init.d/haproxy start

  2. Starting haproxy: [ALERT] 177/105503 (18602) : Starting proxy cacti: cannot bind socket

  3.                                                            [FAILED]


  4. 产生这个错误可能有两个原因,

  5. 1)没有加入内核参数 (net.ipv4.ip_nonlocal_bind=1)

  6. 2)端口冲突


查看状态页面
http://192.168.0.14:8080/stats







测试:
打开http:192.168.0.14
可以轮询到两个web服务器上。


haproxy 配置文件说明 
来源
http://www.linuxidc.com/Linux/2012-07/65350.htm

点击(此处)折叠或打开

  1. ####################全局配置信息######################## 

  2. #######参数是进程级的,通常和操作系统(OS)相关######### 

  3. global 

  4.        maxconn 20480 #默认最大连接数 

  5.        log 127.0.0.1 local3 #[err warning info debug] 

  6.        chroot /var/haproxy #chroot运行的路径 

  7.        uid 99 #所属运行的用户uid 

  8.        gid 99 #所属运行的用户组 

  9.        daemon #以后台形式运行haproxy 

  10.        nbproc 1 #进程数量(可以设置多个进程提高性能) 

  11.        pidfile /var/run/haproxy.pid #haproxy的pid存放路径,启动进程的用户必须有权限访问此文件 

  12.        ulimit-n 65535 #ulimit的数量限制 

  13.  

  14.  

  15. #####################默认的全局设置###################### 

  16. ##这些参数可以被利用配置到frontend,backend,listen组件## 

  17. defaults 

  18.        log global 

  19.        mode http #所处理的类别 (#7层 http;4层tcp ) 

  20.        maxconn 20480 #最大连接数 

  21.        option httplog #日志类别http日志格式 

  22.        option httpclose #每次请求完毕后主动关闭http通道 

  23.        option dontlognull #不记录健康检查的日志信息 

  24.        option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip 

  25.        option redispatch #serverId对应的服务器挂掉后,强制定向到其他健康的服务器 

  26.        option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的连接 

  27.        stats refresh 30 #统计页面刷新间隔 

  28.        retries 3 #3次连接失败就认为服务不可用,也可以通过后面设置 

  29.        balance roundrobin #默认的负载均衡的方式,轮询方式 

  30.       #balance source #默认的负载均衡的方式,类似nginx的ip_hash 

  31.       #balance leastconn #默认的负载均衡的方式,最小连接 

  32.        contimeout 5000 #连接超时 

  33.        clitimeout 50000 #客户端超时 

  34.        srvtimeout 50000 #服务器超时 

  35.        timeout check 2000 #心跳检测超时 

  36.  

  37. ####################监控页面的设置####################### 

  38. listen admin_status #Frontend和Backend的组合体,监控组的名称,按需自定义名称 

  39.         bind 0.0.0.0:65532 #监听端口 

  40.         mode http #http的7层模式 

  41.         log 127.0.0.1 local3 err #错误日志记录 

  42.         stats refresh 5s #每隔5秒自动刷新监控页面 

  43.         stats uri /admin?stats #监控页面的url 

  44.         stats realm itnihao\ itnihao #监控页面的提示信息 

  45.         stats auth admin:admin #监控页面的用户和密码admin,可以设置多个用户名 

  46.         stats auth admin1:admin1 #监控页面的用户和密码admin1 

  47.         stats hide-version #隐藏统计页面上的HAproxy版本信息 

  48.         stats admin if TRUE #手工启用/禁用,后端服务器(haproxy-1.4.9以后版本) 

  49.  

  50.  

  51.        errorfile 403 /etc/haproxy/errorfiles/403.http 

  52.        errorfile 500 /etc/haproxy/errorfiles/500.http 

  53.        errorfile 502 /etc/haproxy/errorfiles/502.http 

  54.        errorfile 503 /etc/haproxy/errorfiles/503.http 

  55.        errorfile 504 /etc/haproxy/errorfiles/504.http 

  56.  

  57. #################HAProxy的日志记录内容设置################### 

  58.        capture request header Host len 40 

  59.        capture request header Content-Length len 10 

  60.        capture request header Referer len 200 

  61.        capture response header Server len 40 

  62.        capture response header Content-Length len 10 

  63.        capture response header Cache-Control len 8 

  64.      

  65. #######################网站监测listen配置##################### 

  66. ###########此用法主要是监控haproxy后端服务器的监控状态############ 

  67. listen site_status 

  68.        bind 0.0.0.0:1081 #监听端口 

  69.        mode http #http的7层模式 

  70.        log 127.0.0.1 local3 err #[err warning info debug] 

  71.        monitor-uri /site_status #网站健康检测URL,用来检测HAProxy管理的网站是否可以用,正常返回200,不正常返回503 

  72.        acl site_dead nbsrv(server_web) lt 2 #定义网站down时的策略当挂在负载均衡上的指定backend的中有效机器数小于1台时返回true 

  73.        acl site_dead nbsrv(server_blog) lt 2 

  74.        acl site_dead nbsrv(server_bbs) lt 2 

  75.        monitor fail if site_dead #当满足策略的时候返回503,网上文档说的是500,实际测试为503 

  76.        monitor-net 192.168.16.2/32 #来自192.168.16.2的日志信息不会被记录和转发 

  77.        monitor-net 192.168.16.3/32 

  78.  

  79. ########frontend配置############ 

  80. #####注意,frontend配置里面可以定义多个acl进行匹配操作######## 

  81. frontend http_80_in 

  82.        bind 0.0.0.0:80 #监听端口,即haproxy提供web服务的端口,和lvs的vip端口类似 

  83.        mode http #http的7层模式 

  84.        log global #应用全局的日志配置 

  85.        option httplog #启用http的log 

  86.        option httpclose #每次请求完毕后主动关闭http通道,HA-Proxy不支持keep-alive模式 

  87.        option forwardfor #如果后端服务器需要获得客户端的真实IP需要配置次参数,将可以从Http Header中获得客户端IP 

  88.        ########acl策略配置############# 

  89.        acl itnihao_web hdr_reg(host) -i ^(www.itnihao.cn|ww1.itnihao.cn)$ 

  90.        #如果请求的域名满足正则表达式中的2个域名返回true -i是忽略大小写 

  91.        acl itnihao_blog hdr_dom(host) -i blog.itnihao.cn 

  92.        #如果请求的域名满足www.itnihao.cn返回true -i是忽略大小写 

  93.        #acl itnihao hdr(host) -i itnihao.cn 

  94.        #如果请求的域名满足itnihao.cn返回true -i是忽略大小写 

  95.        #acl file_req url_sub -i killall= 

  96.        #在请求url中包含killall=,则此控制策略返回true,否则为false 

  97.        #acl dir_req url_dir -i allow 

  98.        #在请求url中存在allow作为部分地址路径,则此控制策略返回true,否则返回false 

  99.        #acl missing_cl hdr_cnt(Content-length) eq 0 

  100.        #当请求的header中Content-length等于0时返回true 

  101.  

  102. ########acl策略匹配相应############# 

  103.        #block if missing_cl 

  104.        #当请求中header中Content-length等于0阻止请求返回403 

  105.        #block if !file_req || dir_req 

  106.        #block表示阻止请求,返回403错误,当前表示如果不满足策略file_req,或者满足策略dir_req,则阻止请求 

  107.        use_backend server_web if itnihao_web 

  108.        #当满足itnihao_web的策略时使用server_web的backend 

  109.        use_backend server_blog if itnihao_blog 

  110.        #当满足itnihao_blog的策略时使用server_blog的backend 

  111.        #redirect prefix http://blog.itniaho.cn code 301 if itnihao 

  112.        #当访问itnihao.cn的时候,用http的301挑转到http://192.168.16.

  113.        default_backend server_bbs 

  114.        #以上都不满足的时候使用默认server_bbs的backend 

  115.  

  116.  

  117. ##########backend的设置############## 

  118. #下面我将设置三组服务器 server_web,server_blog,server_bbs

  119. ##################backend server_web####################

  120. backend server_web 

  121.        mode http #http的7层模式 

  122.        balance roundrobin #负载均衡的方式,roundrobin平均方式 

  123.        cookie SERVERID #允许插入serverid到cookie中,serverid后面可以定义 

  124.        option httpchk GET /index.html #心跳检测的文件 

  125.        server web1 192.168.16.2:80 cookie web1 check inter 1500 rise 3 fall 3 weight 1 

  126.        #服务器定义,cookie 1表示serverid为web1,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用, 

  127.        #fall 3是3次失败认为服务器不可用,weight代表权重 

  128.        server web2 192.168.16.3:80 cookie web2 check inter 1500 rise 3 fall 3 weight 2 

  129.        #服务器定义,cookie 1表示serverid为web2,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用, 

  130.        #fall 3是3次失败认为服务器不可用,weight代表权重 

  131.  

  132. ###################backend server_blog######################

  133. backend server_blog 

  134.        mode http #http的7层模式 

  135.        balance roundrobin #负载均衡的方式,roundrobin平均方式 

  136.        cookie SERVERID #允许插入serverid到cookie中,serverid后面可以定义 

  137.        option httpchk GET /index.html #心跳检测的文件 

  138.        server blog1 192.168.16.2:80 cookie blog1 check inter 1500 rise 3 fall 3 weight 1 

  139.        #服务器定义,cookie 1表示serverid为blog1,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重 

  140.        server blog2 192.168.16.3:80 cookie blog2 check inter 1500 rise 3 fall 3 weight 2 

  141.        #服务器定义,cookie 1表示serverid为blog2,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重 

  142.  

  143. ##################backend server_bbs######################## 

  144.  

  145. backend server_bbs 

  146.        mode http #http的7层模式 

  147.        balance roundrobin #负载均衡的方式,roundrobin平均方式 

  148.        cookie SERVERID #允许插入serverid到cookie中,serverid后面可以定义 

  149.        option httpchk GET /index.html #心跳检测的文件 

  150.        server bbs1 192.168.16.2:80 cookie bbs1 check inter 1500 rise 3 fall 3 weight 1 

  151.        #服务器定义,cookie 1表示serverid为bbs1,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重

  152.        server bbs2 192.168.16.3:80 cookie bbs2 check inter 1500 rise 3 fall 3 weight 2 

  153.        #服务器定义,cookie 1表示serverid为bbs2,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重 



本文转自 蔡小赵 51CTO博客,原文链接:http://blog.51cto.com/zhaopeiyan/1830270
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值