[关闭]
@cdmonkey 2017-12-20T08:23:42.000000Z 字数 7169 阅读 2294

HAProxy-RabbitMQ

网络服务


HAProxy-RabbitMQ.png-14.7kB

http://88250.b3log.org/rabbitmq-clustering-ha
http://www.tuicool.com/articles/viqAbe
http://blog.csdn.net/huithe/article/details/13971847
Official manual:http://cbonte.github.io/haproxy-dconv/configuration-1.5.html

一、安装

1. Install HAProxy

  1. [root@Node-A3 tools]# tar zxvf haproxy-1.5.15.tar.gz
  2. [root@Node-A3 tools]# cd haproxy-1.5.15
  3. [root@Node-A3 haproxy-1.5.15]# make TARGET=linux26 PREFIX=/usr/local/haproxy
  4. [root@Node-A3 haproxy-1.5.15]# make install PREFIX=/usr/local/haproxy

调整相关的内核参数:

  1. echo "net.ipv4.ip_forward = 1" >>/etc/sysctl.conf #打开内核的转发功能。
  2. echo "net.ipv4.ip_nonlocal_bind = 1" >>/etc/sysctl.conf #
  3. sysctl -p

创建服务启动文件及设置文件:

  1. # Create service startup script:
  2. [root@Node-A3 ~]# cp tools/haproxy-1.5.15/examples/haproxy.init /etc/init.d/haproxy
  3. [root@Node-A3 ~]# chmod 755 /etc/init.d/haproxy
  4. [root@Node-A3 ~]# chkconfig --add haproxy
  5. [root@Node-A3 ~]# chkconfig --list | grep haproxy
  6. haproxy 0:off 1:off 2:off 3:off 4:off 5:off 6:off
  7. # Create configure file:
  8. [root@Node-A3 ~]# mkdir /etc/haproxy
  9. [root@Node-A3 ~]# vim /etc/haproxy/haproxy.cfg
  10. # 可参考软件安装包内提供的配置文件示例进行设定:/root/tools/haproxy-1.5.15/examples/haproxy.cfg
  11. # 配置文件的内容基本分为五个部分:
  12. global ### 全局配置信息(参数是进程级的,通常和操作系统相关)###
  13. maxconn 100000 # 限制单个进程的最大连接数,该参数也是性能调优时最常被修改的。
  14. chroot /usr/local/haproxy
  15. uid 99 # 所属运行用户及用户组,默认值99为nobody
  16. gid 99
  17. daemon # 让服务进程作为守护进程于后台运行。
  18. nbproc 1 # 指定作为守护进程运行时的进程数,推荐设置为与CPU核心数相同。
  19. # 创建多个进程能够减少每个进程负担的任务数,但是过多的进程也会导致服务的崩溃。
  20. ulimit -n 65535 # 设定最大打开的文件描述符数量。注意:该配置项官方已不建议设定,而由进程自动计算。
  21. pidfile /usr/local/haproxy/logs/haproxy.pid # 启动进程的用户必须有权限访问该文件。
  22. log 127.0.0.1 local3 info # 设定日志级别
  23. defaults ### 默认的全局配置信息(这部分参数可以被默认配置到监控页面、前端及后端)###
  24. option http-keep-alive # Enable or disable HTTP keep-alive from client to server
  25. option dontlognull # 日志中将不会记录空连接。
  26. # 官方文档建议如果该服务上游没有其他负载均衡器的话,建议不要使用该参数。
  27. maxconn 100000
  28. mode http # 设定代理的模式,即四层还是七层(具体的说明见下面的表格,包括:http、tcp、health)
  29. log 127.0.0.1 local4 err # 设定使用本机的指定设备来记录错误日志。
  30. timeout connect 5000ms # 设置等待连接到服务器成功的最大时间(如果不写单位则默认为毫秒)。
  31. timeout client 50000ms # 设置客户端发送数据时成功连接的最长等待时间。
  32. timeout server 50000ms # 设置服务器回应客户端数据发送的最长等待时间。
  33. listen stats ### 监控页面的设置 ###
  34. mode http
  35. bind 0.0.0.0:8888
  36. stats enable
  37. stats refresh 5s # 每隔五秒自动刷新监控页面。
  38. stats uri /haproxy-status # 设定监控页面的访问地址。
  39. stats auth haproxy:haproxy # 设定监控页面的用户和密码,这里可设置多个户名。
  40. stats hide-version # 隐藏版本号。
  41. frontend frontend_rabbitmq_cluster ### 自定义前端的名称 ###
  42. bind 172.16.1.30:5672 # 监听的地址及端口(如果是配置了高可用,则需要监听VIP)。
  43. mode http
  44. option httplog # 设定采用HTTP格式的日志。
  45. log global
  46. default_backend backend_rabbitmq_cluster
  47. backend backend_rabbitmq_cluster ### 自定义后端的名称 ###
  48. option forwardfor header X-REAL-IP # Set X-Forwarded-For
  49. # option httpchk <method> <uri> <version>
  50. option httpchk HEAD / HTTP/1.0
  51. balance source
  52. server node1 172.16.1.25:5672 check inter 2000 rise 30 fall 15
  53. server node2 172.16.1.26:5672 check inter 2000 rise 30 fall 15
  54. server node3 172.16.1.27:5672 check inter 2000 rise 30 fall 15
  55. ----------------
  56. # 修改完配置文件后需要重新加载(如果服务未启动则直接启动服务即可):
  57. [root@Node-A3 ~]# /etc/init.d/haproxy reload

更为详细的讲解请见田飞雨:http://www.tianfeiyu.com/?p=1061

http://www.tuicool.com/articles/viqAbe

222.png-4.2kB

2. Install keepalived

  1. [root@Node-A3 tools]# tar zxvf keepalived-1.2.19.tar.gz
  2. [root@Node-A3 tools]# cd keepalived-1.2.19
  3. ./configure --prefix=/usr/local/keepalived --disable-fwmark
  4. make && make install

创建服务启动文件及设置文件:

  1. # Create service startup script:
  2. [root@Node-A3 ~]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
  3. [root@Node-A3 ~]# chmod 755 /etc/init.d/keepalived
  4. # Create configure file:
  5. [root@Node-A3 ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
  6. [root@Node-A3 ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
  7. [root@Node-A3 ~]# mkdir /etc/keepalived
  8. [root@Node-A3 ~]# vim /etc/keepalived/keepalived.conf
  9. ! Configuration File for keepalived
  10. global_defs {
  11. notification_email {
  12. saltstack@example.com
  13. }
  14. notification_email_from keepalived@example.com
  15. smtp_server 127.0.0.1
  16. smtp_connect_timeout 30
  17. router_id Node-A3
  18. }
  19. vrrp_instance haproxy_ha {
  20. state MASTER
  21. interface eth0
  22. virtual_router_id 36
  23. priority 150
  24. advert_int 1
  25. authentication {
  26. auth_type PASS
  27. auth_pass 1111
  28. }
  29. virtual_ipaddress {
  30. 172.16.1.30/14
  31. }
  32. }
  33. --------------
  34. # Startup the service:
  35. [root@Node-A3 ~]# /etc/init.d/keepalived start

二、日志

http://www.shencan.net/index.php/2013/06/26/haproxy-%E6%97%A5%E5%BF%97%E8%AF%A6%E8%A7%A3
我们设定的日志记录条目为:

  1. log 127.0.0.1 local3 info

因而相应的我们需要修改Rsyslog服务的设定内容:

  1. [root@Node-A3 ~]# vim /etc/rsyslog.conf
  2. # Remove the comment symbol, Open UDP:
  3. $ModLoad imudp
  4. $UDPServerRun 514
  5. # Add the following:
  6. local3.* /var/log/haproxy.log
  7. -----------------
  8. [root@Node-A3 ~]# vim /etc/sysconfig/rsyslog
  9. SYSLOGD_OPTIONS="-c 5" --> SYSLOGD_OPTIONS="-r -m 0 -c 2"
  10. -----------------
  11. # Restart the rsyslog service:
  12. [root@Node-A3 ~]# /etc/init.d/rsyslog restart
  13. # 服务的访问日志设定完毕。

三、Health check

健康检查分为四层及七层,使用我们的示例进行解释:

  1. server node1 172.16.1.25:5672 check inter 2000 rise 30 fall 15

四、On-line Maintenance

首先要说明,修改完设置文件后reload服务的话,当前的连接会断开并重新创建。因而使用重载的方法重新读取新的设置文件并不是一个很好的做法。因为“Haproxy”能够监听一个套接字,并经由该套接字接收维护指令,所以我们能够向该套接字对服务进行操作而无需重载服务。

  1. # 首先我们需要在全局设置部分增加如下的内容:
  2. [root@Node-A3 ~]# vim /etc/haproxy/haproxy.cfg
  3. global
  4. stats socket /var/run/haproxy.sock mode 600 level admin
  5. stats timeout 2m

Use socat

我们使用“socat”进行相关的维护操作。

  1. # Install socat:
  2. [root@Node-A3 ~]# yum install -y socat
  3. # View the help message:
  4. [root@Node-A3 ~]# echo "help"|socat stdio /var/run/haproxy.sock
  5. Unknown command. Please enter one of the following commands only :
  6. clear counters : clear max statistics counters (add 'all' for all counters)
  7. clear table : remove an entry from a table
  8. help : this message
  9. prompt : toggle interactive mode with prompt
  10. quit : disconnect
  11. show info : report information about the running process
  12. show pools : report information about the memory pools usage
  13. show stat : report counters for each proxy and server
  14. show errors : report last request and response errors for each proxy
  15. show sess [id] : report the list of current sessions or dump this session
  16. show table [id]: report table usage stats or dump this table's contents'
  17. get weight : report a server's current weight'
  18. set weight : change a server's weight'
  19. set server : change a server's state or weight'
  20. set table [id] : update or create a table entry's data'
  21. set timeout : change a timeout setting
  22. set maxconn : change a maxconn setting
  23. set rate-limit : change a rate limiting value
  24. disable : put a server or frontend in maintenance mode
  25. enable : re-enable a server or frontend which is in maintenance mode
  26. shutdown : kill a session or a frontend (eg:to release listening ports)
  27. show acl [id] : report avalaible acls or dump an acl's contents'
  28. get acl : reports the patterns matching a sample for an ACL
  29. add acl : add acl entry
  30. del acl : delete acl entry
  31. clear acl <id> : clear the content of this acl
  32. show map [id] : report avalaible maps or dump a map's contents'
  33. get map : reports the keys and values matching a sample for a map
  34. set map : modify map entry
  35. add map : add map entry
  36. del map : delete map entry
  37. clear map <id> : clear the content of this map
  38. set ssl <stmt> : set statement for ssl
  1. # Show Haproxy info:
  2. [root@Node-A3 ~]# echo "show info"|socat stdio /var/run/haproxy.sock
  3. Name: HAProxy
  4. Version: 1.5.15
  5. Release_date: 2015/11/01
  6. ...
  1. # Set maximum number of connections:
  2. ---------------
  3. # Turn off the backend server node:
  4. # 将指定的后端服务器离线后监控页面将会把该服务器显示为棕色,即维护的状态。
  5. echo "disable server backend_www_cdmonkey_com/web-node1" | socat stdio /var/run/haproxy.sock
  6. # Turn on the backend server node:
  7. echo "enable server backend_www_cdmonkey_com/web-node1" | socat stdio /var/run/haproxy.sock

五、长连接

http://cbonte.github.io/haproxy-dconv/configuration-1.5.html#option%20http-keep-alive
http://striven.wicp.net/haproxy%E5%B7%A5%E4%BD%9C%E6%A8%A1%E5%BC%8F

默认的情况下,它运行于长连接模式下,而下面几个设定选项能够将其改变:

  1. option http-server-close
  2. # 对于某些服务器端不支持长连接的情况,利用该选项可使客户端到代理是长连接,而代理到服务器端是短连接。
  3. option httpclose
  4. # 强制使用短连接,使每个客户端或服务端于每次传输后,都会主动关闭连接,同下面的这个选项类似。
  5. option forceclose
  6. # 如果有服务器不正确的忽略掉头部信息,可使用该选项使服务端响应后主动关闭请求连接。
  7. # 该选项还能够及早的释放服务连接,而不必等到客户端的应答确认。
  8. option http-tunnel

当前端和后端的设定选项不同时,上面四个选项将优先于option http-keep-alive

  1. # 该选项能够保证在短连接情况之下,客户端能收到完成的数据包。
  2. option http-pretend-keepalive
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注