五、配置案例

  1. 前端调度器IP192.168.1.210
  2.  
  3. 后端应用服务器IP: 192.168.1.111 192.168.1.112

1. 定义独立日志文件

  1. [root@node1 haproxy]# vim /etc/rsyslog.conf #为其添加日志功能
  2. # Provides UDP syslog reception
  3. $ModLoad imudp
  4. $UDPServerRun 514 ------>启动udp,启动端口后将作为服务器工作
  5.  
  6. # Provides TCP syslog reception
  7. $ModLoad imtcp
  8. $InputTCPServerRun 514 ------>启动tcp监听端口
  9. local2.* /var/log/haproxy.log
  10.  
  11. [root@node1 haproxy]# service rsyslog restar
  12.  
  13. [root@LB haproxy]# vim haproxy.cfg
  14.  
  15. log 127.0.0.1 local2 --------->在global端中添加此行
  16.  

2. 一个最简单的http服务的配置:

  1. global
  2. log 127.0.0.1 local2
  3.  
  4. chroot /var/lib/haproxy
  5. pidfile /var/run/haproxy.pid
  6. maxconn 4000
  7. user haproxy
  8. group haproxy
  9. daemon
  10.  
  11. stats socket /var/lib/haproxy/stats
  12.  
  13. defaults
  14. mode http
  15. log global
  16. option httplog
  17. option dontlognull
  18. option http-server-close
  19. option forwardfor except 127.0.0.0/8
  20. option redispatch
  21. retries 3
  22. timeout http-request 10s
  23. timeout queue 1m
  24. timeout connect 10s
  25. timeout client 1m
  26. timeout server 1m
  27. timeout http-keep-alive 10s
  28. timeout check 10s
  29. maxconn 3000
  30.  
  31. frontend webser #webser为名称
  32. option forwardfor
  33. bind *:80
  34. default_backend app
  35. backend app
  36. balance roundrobin #使拥roundrobin 算法
  37. server app1 192.168.1.111:80 check
  38. server app2 192.168.1.112:80 check

3. haproxy统计页面的输出机制

  1. frontend webser
  2. log 127.0.0.1 local3
  3. option forwardfor
  4. bind *:80
  5. default_backend app
  6. backend app
  7. cookie node insert nocache
  8. balance roundrobin
  9. server app1 192.168.1.111:80 check cookie node1 intval 2 rise 1 fall 2
  10. server app2 192.168.1.112:80 check cookie node2 intval 2 rise 1 fall 2
  11. server backup 127.0.0.1:8010 check backup
  12.  
  13. listen statistics
  14. bind *:8009 # 自定义监听端口
  15. stats enable # 启用基于程序编译时默认设置的统计报告
  16. stats auth admin:admin # 统计页面用户名和密码设置
  17. stats uri /admin?stats # 自定义统计页面的URL,默认为/haproxy?stats
  18. stats hide-version # 隐藏统计页面上HAProxy的版本信息
  19. stats refresh 30s # 统计页面自动刷新时间
  20. stats admin if TRUE #如果认证通过就做管理功能,可以管理后端的服务器
  21. stats realm Hapadmin # 统计页面密码框上提示文本,默认为Haproxy\ Statistics

4. 动静分离示例:

  1. frontend webservs
  2. bind *:80
  3. acl url_static path_beg -i /static /images /javascript /stylesheets
  4. acl url_static path_end -i .jpg .gif .png .css .js .html
  5. acl url_php path_end -i .php
  6. acl host_static hdr_beg(host) -i img. imgs. video. videos. ftp. image. download.
  7. use_backend static if url_static or host_static
  8.  
  9. use_backend dynamic if url_php
  10. default_backend dynamic
  11.  
  12. backend static
  13. balance roundrobin
  14. server node1 192.168.1.111:80 check maxconn 3000
  15.  
  16. backend dynamic
  17. balance roundrobin
  18. server node2 192.168.1.112:80 check maxconn 1000

5. http服务器配置完整示例

  1. #---------------------------------------------------------------------
  2. # Global settings
  3. #---------------------------------------------------------------------
  4. global
  5. # to have these messages end up in /var/log/haproxy.log you will
  6. # need to:
  7. #
  8. # 1) configure syslog to accept network log events. This is done
  9. # by adding the '-r' option to the SYSLOGD_OPTIONS in
  10. # /etc/sysconfig/syslog
  11. #
  12. # 2) configure local2 events to go to the /var/log/haproxy.log
  13. # file. A line like the following can be added to
  14. # /etc/sysconfig/syslog
  15. #
  16. # local2.* /var/log/haproxy.log
  17. #
  18. log 127.0.0.1 local2
  19.  
  20. chroot /var/lib/haproxy
  21. pidfile /var/run/haproxy.pid
  22. maxconn 4000
  23. user haproxy
  24. group haproxy
  25. daemon
  26.  
  27. defaults
  28. mode http
  29. log global
  30. option httplog
  31. option dontlognull
  32. option http-server-close
  33. option forwardfor except 127.0.0.0/8
  34. option redispatch
  35. retries 3
  36. timeout http-request 10s
  37. timeout queue 1m
  38. timeout connect 10s
  39. timeout client 1m
  40. timeout server 1m
  41. timeout http-keep-alive 10s
  42. timeout check 10s
  43. maxconn 30000
  44.  
  45. listen stats
  46. mode http
  47. bind 0.0.0.0:1080
  48. stats enable
  49. stats hide-version
  50. stats uri /haproxyadmin?stats
  51. stats realm Haproxy\ Statistics
  52. stats auth admin:admin
  53. stats admin if TRUE
  54.  
  55.  
  56. frontend http-in
  57. bind *:80
  58. mode http
  59. log global
  60. option httpclose
  61. option logasap #不等待响应结束就记录日志,表示提前记录日志,一般日志会记录响应时长,此不记录响应时长
  62. option dontlognull #不记录空信息
  63. capture request header Host len 20 #记录请求首部的前20个字符
  64. capture request header Referer len 60 #referer跳转引用,就是上一级
  65. default_backend servers
  66.  
  67. frontend healthcheck
  68. bind :1099 #定义外部检测机制
  69. mode http
  70. option httpclose
  71. option forwardfor
  72. default_backend servers
  73.  
  74. backend servers
  75. balance roundrobin
  76. server websrv1 192.168.1.111:80 check maxconn 2000
  77. server websrv2 192.168.1.112:80 check maxconn 2000

6. 负载均衡MySQL服务的配置示例

  1. #---------------------------------------------------------------------
  2. # Global settings
  3. #---------------------------------------------------------------------
  4. global
  5. # to have these messages end up in /var/log/haproxy.log you will
  6. # need to:
  7. #
  8. # 1) configure syslog to accept network log events. This is done
  9. # by adding the '-r' option to the SYSLOGD_OPTIONS in
  10. # /etc/sysconfig/syslog
  11. #
  12. # 2) configure local2 events to go to the /var/log/haproxy.log
  13. # file. A line like the following can be added to
  14. # /etc/sysconfig/syslog
  15. #
  16. # local2.* /var/log/haproxy.log
  17. #
  18. log 127.0.0.1 local2
  19.  
  20. chroot /var/lib/haproxy
  21. pidfile /var/run/haproxy.pid
  22. maxconn 4000
  23. user haproxy
  24. group haproxy
  25. daemon
  26.  
  27. defaults
  28. mode tcp
  29. log global
  30. option httplog
  31. option dontlognull
  32. retries 3
  33. timeout http-request 10s
  34. timeout queue 1m
  35. timeout connect 10s
  36. timeout client 1m
  37. timeout server 1m
  38. timeout http-keep-alive 10s
  39. timeout check 10s
  40. maxconn 600
  41.  
  42. listen stats
  43. mode http
  44. bind 0.0.0.0:1080
  45. stats enable
  46. stats hide-version
  47. stats uri /haproxyadmin?stats
  48. stats realm Haproxy\ Statistics
  49. stats auth admin:admin
  50. stats admin if TRUE
  51.  
  52.  
  53. frontend mysql
  54. bind *:3306
  55. mode tcp
  56. log global
  57. default_backend mysqlservers
  58.  
  59. backend mysqlservers
  60. balance leastconn
  61. server dbsrv1 192.168.1.111:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300
  62. server dbsrv2 192.168.1.112:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300
  63.