一、环境说明
实验环境
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 -c -V -f $config
  26. }
  27. start() {
  28. $exec -c -q -f $config
  29. if [ $? -ne 0 ]; then
  30. echo "Errors in configuration file, check with $prog check."
  31. return 1
  32. fi
  33. echo -n $"Starting $prog: "
  34. # start it up here, usually something like "daemon $exec"
  35. daemon $exec -D -f $config -p /var/run/$prog.pid
  36. retval=$?
  37. echo
  38. [ $retval -eq 0 ] && touch $lockfile
  39. return $retval
  40. }
  41. stop() {
  42. echo -n $"Stopping $prog: "
  43. # stop it here, often "killproc $prog"
  44. killproc $prog
  45. retval=$?
  46. echo
  47. [ $retval -eq 0 ] && rm -f $lockfile
  48. return $retval
  49. }
  50. restart() {
  51. $exec -c -q -f $config
  52. if [ $? -ne 0 ]; then
  53. echo "Errors in configuration file, check with $prog check."
  54. return 1
  55. fi
  56. stop
  57. start
  58. }
  59. reload() {
  60. $exec -c -q -f $config
  61. if [ $? -ne 0 ]; then
  62. echo "Errors in configuration file, check with $prog check."
  63. return 1
  64. fi
  65. echo -n $"Reloading $prog: "
  66. $exec -D -f $config -p /var/run/$prog.pid -sf $(cat /var/run/$prog.pid)
  67. retval=$?
  68. echo
  69. return $retval
  70. }
  71. force_reload() {
  72. restart
  73. }
  74. fdr_status() {
  75. status $prog
  76. }
  77. case "$1" in
  78. start|stop|restart|reload)
  79. $1
  80. ;;
  81. force-reload)
  82. force_reload
  83. ;;
  84. checkconfig)
  85. check
  86. ;;
  87. status)
  88. fdr_status
  89. ;;
  90. condrestart|try-restart)
  91. [ ! -f $lockfile ] || restart
  92. ;;
  93. *)
  94. echo $"Usage: $0 {start|stop|status|checkconfig|restart|try-restart|reload|force-reload}"
  95. exit 2
  96. 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 -D -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. ##这些参数可以被利用配置到frontend,backend,listen组件##
  15. defaults
  16. log global
  17. mode http #所处理的类别 (#7层 http;4层tcp )
  18. maxconn 20480 #最大连接数
  19. option httplog #日志类别http日志格式
  20. option httpclose #每次请求完毕后主动关闭http通道
  21. option dontlognull #不记录健康检查的日志信息
  22. option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
  23. option redispatch #serverId对应的服务器挂掉后,强制定向到其他健康的服务器
  24. option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的连接
  25. stats refresh 30 #统计页面刷新间隔
  26. retries 3 #3次连接失败就认为服务不可用,也可以通过后面设置
  27. balance roundrobin #默认的负载均衡的方式,轮询方式
  28. #balance source #默认的负载均衡的方式,类似nginx的ip_hash
  29. #balance leastconn #默认的负载均衡的方式,最小连接
  30. contimeout 5000 #连接超时
  31. clitimeout 50000 #客户端超时
  32. srvtimeout 50000 #服务器超时
  33. timeout check 2000 #心跳检测超时
  34. ####################监控页面的设置#######################
  35. listen admin_status #Frontend和Backend的组合体,监控组的名称,按需自定义名称
  36. bind 0.0.0.0:65532 #监听端口
  37. mode http #http的7层模式
  38. log 127.0.0.1 local3 err #错误日志记录
  39. stats refresh 5s #每隔5秒自动刷新监控页面
  40. stats uri /admin?stats #监控页面的url
  41. stats realm itnihao\ itnihao #监控页面的提示信息
  42. stats auth admin:admin #监控页面的用户和密码admin,可以设置多个用户名
  43. stats auth admin1:admin1 #监控页面的用户和密码admin1
  44. stats hide-version #隐藏统计页面上的HAproxy版本信息
  45. stats admin if TRUE #手工启用/禁用,后端服务器(haproxy-1.4.9以后版本)
  46. errorfile 403 /etc/haproxy/errorfiles/403.http
  47. errorfile 500 /etc/haproxy/errorfiles/500.http
  48. errorfile 502 /etc/haproxy/errorfiles/502.http
  49. errorfile 503 /etc/haproxy/errorfiles/503.http
  50. errorfile 504 /etc/haproxy/errorfiles/504.http
  51. #################HAProxy的日志记录内容设置###################
  52. capture request header Host len 40
  53. capture request header Content-Length len 10
  54. capture request header Referer len 200
  55. capture response header Server len 40
  56. capture response header Content-Length len 10
  57. capture response header Cache-Control len 8
  58. #######################网站监测listen配置#####################
  59. ###########此用法主要是监控haproxy后端服务器的监控状态############
  60. listen site_status
  61. bind 0.0.0.0:1081 #监听端口
  62. mode http #http的7层模式
  63. log 127.0.0.1 local3 err #[err warning info debug]
  64. monitor-uri /site_status #网站健康检测URL,用来检测HAProxy管理的网站是否可以用,正常返回200,不正常返回503
  65. acl site_dead nbsrv(server_web) lt 2 #定义网站down时的策略当挂在负载均衡上的指定backend的中有效机器数小于1台时返回true
  66. acl site_dead nbsrv(server_blog) lt 2
  67. acl site_dead nbsrv(server_bbs) lt 2
  68. monitor fail if site_dead #当满足策略的时候返回503,网上文档说的是500,实际测试为503
  69. monitor-net 192.168.16.2/32 #来自192.168.16.2的日志信息不会被记录和转发
  70. monitor-net 192.168.16.3/32
  71. ########frontend配置############
  72. #####注意,frontend配置里面可以定义多个acl进行匹配操作########
  73. frontend http_80_in
  74. bind 0.0.0.0:80 #监听端口,即haproxy提供web服务的端口,和lvs的vip端口类似
  75. mode http #http的7层模式
  76. log global #应用全局的日志配置
  77. option httplog #启用http的log
  78. option httpclose #每次请求完毕后主动关闭http通道,HA-Proxy不支持keep-alive模式
  79. option forwardfor #如果后端服务器需要获得客户端的真实IP需要配置次参数,将可以从Http Header中获得客户端IP
  80. ########acl策略配置#############
  81. acl itnihao_web hdr_reg(host) -i ^(www.itnihao.cn|ww1.itnihao.cn)$
  82. #如果请求的域名满足正则表达式中的2个域名返回true -i是忽略大小写
  83. acl itnihao_blog hdr_dom(host) -i blog.itnihao.cn
  84. #如果请求的域名满足www.itnihao.cn返回true -i是忽略大小写
  85. #acl itnihao hdr(host) -i itnihao.cn
  86. #如果请求的域名满足itnihao.cn返回true -i是忽略大小写
  87. #acl file_req url_sub -i killall=
  88. #在请求url中包含killall=,则此控制策略返回true,否则为false
  89. #acl dir_req url_dir -i allow
  90. #在请求url中存在allow作为部分地址路径,则此控制策略返回true,否则返回false
  91. #acl missing_cl hdr_cnt(Content-length) eq 0
  92. #当请求的header中Content-length等于0时返回true
  93. ########acl策略匹配相应#############
  94. #block if missing_cl
  95. #当请求中header中Content-length等于0阻止请求返回403
  96. #block if !file_req || dir_req
  97. #block表示阻止请求,返回403错误,当前表示如果不满足策略file_req,或者满足策略dir_req,则阻止请求
  98. use_backend server_web if itnihao_web
  99. #当满足itnihao_web的策略时使用server_web的backend
  100. use_backend server_blog if itnihao_blog
  101. #当满足itnihao_blog的策略时使用server_blog的backend
  102. #redirect prefix http://blog.itniaho.cn code 301 if itnihao
  103. #当访问itnihao.cn的时候,用http的301挑转到http://192.168.16.3
  104. default_backend server_bbs
  105. #以上都不满足的时候使用默认server_bbs的backend
  106. ##########backend的设置##############
  107. #下面我将设置三组服务器 server_web,server_blog,server_bbs
  108. ##################backend server_web####################
  109. backend server_web
  110. mode http #http的7层模式
  111. balance roundrobin #负载均衡的方式,roundrobin平均方式
  112. cookie SERVERID #允许插入serverid到cookie中,serverid后面可以定义
  113. option httpchk GET /index.html #心跳检测的文件
  114. server web1 192.168.16.2:80 cookie web1 check inter 1500 rise 3 fall 3 weight 1
  115. #服务器定义,cookie 1表示serverid为web1,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,
  116. #fall 3是3次失败认为服务器不可用,weight代表权重
  117. server web2 192.168.16.3:80 cookie web2 check inter 1500 rise 3 fall 3 weight 2
  118. #服务器定义,cookie 1表示serverid为web2,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,
  119. #fall 3是3次失败认为服务器不可用,weight代表权重
  120. ###################backend server_blog######################
  121. backend server_blog
  122. mode http #http的7层模式
  123. balance roundrobin #负载均衡的方式,roundrobin平均方式
  124. cookie SERVERID #允许插入serverid到cookie中,serverid后面可以定义
  125. option httpchk GET /index.html #心跳检测的文件
  126. server blog1 192.168.16.2:80 cookie blog1 check inter 1500 rise 3 fall 3 weight 1
  127. #服务器定义,cookie 1表示serverid为blog1,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重
  128. server blog2 192.168.16.3:80 cookie blog2 check inter 1500 rise 3 fall 3 weight 2
  129. #服务器定义,cookie 1表示serverid为blog2,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重
  130. ##################backend server_bbs########################
  131. backend server_bbs
  132. mode http #http的7层模式
  133. balance roundrobin #负载均衡的方式,roundrobin平均方式
  134. cookie SERVERID #允许插入serverid到cookie中,serverid后面可以定义
  135. option httpchk GET /index.html #心跳检测的文件
  136. server bbs1 192.168.16.2:80 cookie bbs1 check inter 1500 rise 3 fall 3 weight 1
  137. #服务器定义,cookie 1表示serverid为bbs1,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重
  138. server bbs2 192.168.16.3:80 cookie bbs2 check inter 1500 rise 3 fall 3 weight 2
  139. #服务器定义,cookie 1表示serverid为bbs2,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重

haproxy 配置 说明的更多相关文章

  1. Python-day3作业-haproxy配置文件管理脚本

    #!/usr/bin/env python import os,sys,time,re,prettytable,json from collections import defaultdict,Ord ...

  2. Haproxy配置参数

    HAProxy配置中分成五部分内容,当然这些组件不是必选的,可以根据需要选择部分作为配置. ===================== global    参数是进程级的,通常和操作系统(OS)相关. ...

  3. openstack高可用haproxy配置

    #openstack高可用haproxy配置openstack pike 部署 目录汇总 http://www.cnblogs.com/elvi/p/7613861.html #openstack高可 ...

  4. haproxy配置基于ssl证书的https负载均衡

    本实验全部在haproxy1.5.19版本进行测试通过,经过测试1.7.X及haproxy1.3版本以下haproxy配置参数可能不适用,需要注意版本号. 一.业务要求现在根据业务的实际需要,有以下几 ...

  5. haproxy配置详解

    先看一个ha的配置文件: # # Global settings # global # to have these messages end up in /var/log/haproxy.log yo ...

  6. HAproxy 配置参数详解

    HAproxy 配置参数详解 /etc/haproxy/haproxy.cfg # 配置文件 ----------------------------------------------------- ...

  7. redis sentinel集群配置及haproxy配置

    ip分布情况: sentinel-1/redis 主 10.11.11.5 sentinel-2/redis 从 10.11.11.7 sentinel-3/redis 从 10.11.11.8 ha ...

  8. socat管理haproxy配置 ssh-keygen -N '' -t rsa -q -b 2048

    socat管理haproxy配置   haproxy是可以通过socat命令管理haproxy.cfg文件的:1.安装socat yum install socat -y 2.配置haproxy.cf ...

  9. HaProxy配置

    安装 http://www.cnblogs.com/wang1988ming/archive/2012/10/24/2737507.html 配置 global log 127.0.0.1 local ...

  10. 利用keepalived和haproxy配置mysql的高可用负载均衡

    实验系统:CentOS 6.6_x86_64(2.6.32-504.30.3.el6.x86_64) 实验前提:防火墙和selinux都关闭 实验说明:本实验共有4台主机,IP分配如拓扑 实验软件:k ...

随机推荐

  1. mount: /dev/sdb already mounted or /sheepdog1 busy(multipath,wwid,uuid,udev)

    正常处理逻辑: 先umount /dev/sdb或是umount /backup如果还是显示的busy,你试试下面的方法fuser -m /dev/sdb查看一下是否sdb1正在被使用,或是有进程正在 ...

  2. 4.JMeter聚合报告分析

    1.Label:每个Jmeter的element的Name值 2.Samples:发出的请求数量 3.Average:平均响应时间 4.Median:表示50%用户的响应时间 5.90%Line:90 ...

  3. 【STL源码学习】std::list类的类型别名分析

    有了点模板元编程的traits基础,看STL源码清晰多了,以前看源码的时候总被各种各样的typedef给折腾得看不下去, 将<list>头文件的类继承结构简化如下 #include < ...

  4. MySQL查询优化器工作原理解析

    手册上查询优化器概述 查询优化器的任务是发现执行SQL查询的最佳方案.大多数查询优化器,包括MySQL的查询优化器,总或多或少地在所有可能的查询评估方案中搜索最佳方案.对于联接查询,MySQL优化器所 ...

  5. 用活Firewalld防火墙之direct

    原文地址:http://www.excelib.com/article/294/show 学生在前面已经给大家介绍过了Firewalld中direct的作用,使用他可以直接使用iptables.ip6 ...

  6. Linux中常用的查找文件的命令

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索.这些是从网上找到的资料(参考资料1),因为有时很长时间不会用到,当要用的时候经常弄混了,所以放到这里方便使用. w ...

  7. Go - reflection

    Go 语言也有反射的机制,通过这种机制可以大大提高程序的灵活性. reflect包中的 TypeOf 与 ValueOf 方法 首先,当我们想要使用反射技术的时候,我们需要先引用 reflect 包. ...

  8. HDU2546题解

    解题思路:先对价格排序(顺序或倒序都可以),然后,对前n-1(从1开始.排序方式为顺序)做容量为m(卡上余额)-5的01背包(背包体积和价值相等).假设dp[i][j]表示从前i个背包中挑选体积不超过 ...

  9. MySql命令集合

    1.mysql命令用户连接数据库 mysql命令格式: mysql -h主机地址 -u用户名 -p用户密码 (1) 连接到本机上的MYSQL 首先打开DOS窗口,然后进入目录mysql\bin,再键入 ...

  10. 【POJ】2329 Nearest number - 2(搜索)

    题目 传送门:QWQ 分析 在dp分类里做的,然而并不会$ O(n^3) $ 的$ dp $,怒写一发搜索. 看起来是$ O(n^4) $,但仔细分析了一下好像还挺靠谱的? poj挂了,没在poj交, ...