部署 HAProxy 服务

1 准备工作

1.1 环境信息

  • HAProxy 服务器信息如下:
  1. 192.168.100.100

1.2 安装依赖

  1. yum -y install epel-release

2 安装配置 HAProxy

2.1 安装 HAProxy

  1. yum install -y haproxy

2.2 配置 HAProxy

  1. # 打开 HAProxy 的配置文件
  2. vi /etc/haproxy/haproxy.cfg
  1. global
  2. # to have these messages end up in /var/log/haproxy.log you will
  3. # need to:
  4. #
  5. # 1) configure syslog to accept network log events. This is done
  6. # by adding the '-r' option to the SYSLOGD_OPTIONS in
  7. # /etc/sysconfig/syslog
  8. #
  9. # 2) configure local2 events to go to the /var/log/haproxy.log
  10. # file. A line like the following can be added to
  11. # /etc/sysconfig/syslog
  12. #
  13. # local2.* /var/log/haproxy.log
  14. #
  15. log 127.0.0.1 local2
  16. chroot /var/lib/haproxy
  17. pidfile /var/run/haproxy.pid
  18. maxconn 4000
  19. user haproxy
  20. group haproxy
  21. daemon
  22. # turn on stats unix socket
  23. stats socket /var/lib/haproxy/stats
  24. #---------------------------------------------------------------------
  25. # common defaults that all the 'listen' and 'backend' sections will
  26. # use if not designated in their block
  27. #---------------------------------------------------------------------
  28. defaults
  29. log global
  30. option dontlognull
  31. option redispatch
  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 3000
  41. listen stats
  42. bind *:8080
  43. mode http
  44. stats enable
  45. stats uri /haproxy # 监控页面, 请自行修改. 访问地址为 http://192.168.100.100:8080/haproxy
  46. stats refresh 5s
  47. stats realm haproxy-status
  48. stats auth admin:KXOeyNgDeTdpeu9q # 账户密码, 请自行修改. 访问 http://192.168.100.100:8080/haproxy 会要求输入
  49. #---------------------------------------------------------------------
  50. # check 检活参数说明
  51. # inter 间隔时间, 单位: 毫秒
  52. # rise 连续成功的次数, 单位: 次
  53. # fall 连续失败的次数, 单位: 次
  54. # 例: inter 2s rise 2 fall 3
  55. # 表示 2 秒检查一次状态, 连续成功 2 次服务正常, 连续失败 3 次服务异常
  56. #
  57. # server 服务参数说明
  58. # server 192.168.100.21 192.168.100.21:80 weight 1 cookie web01
  59. # 第一个 192.168.100.21 做为页面展示的标识, 可以修改为其他任意字符串
  60. # 第二个 192.168.100.21:80 是实际的后端服务端口
  61. # weight 为权重, 多节点时安装权重进行负载均衡
  62. # cookie 用户侧的 cookie 会包含此标识, 便于区分当前访问的后端节点
  63. # 例: server db01 192.168.100.21:3306 weight 1 cookie db_01
  64. #---------------------------------------------------------------------
  65. listen jms-web
  66. bind *:80 # 监听 80 端口
  67. mode http
  68. # redirect scheme https if !{ ssl_fc } # 重定向到 https
  69. # bind *:443 ssl crt /opt/ssl.pem # https 设置
  70. option httpchk GET /api/health/ # Core 检活接口
  71. stick-table type ip size 200k expire 30m
  72. stick on src
  73. balance leastconn
  74. server 192.168.100.21 192.168.100.21:80 weight 1 cookie web01 check inter 2s rise 2 fall 3 # JumpServer 服务器
  75. server 192.168.100.22 192.168.100.22:80 weight 1 cookie web02 check inter 2s rise 2 fall 3
  76. server 192.168.100.23 192.168.100.23:80 weight 1 cookie web03 check inter 2s rise 2 fall 3
  77. server 192.168.100.24 192.168.100.24:80 weight 1 cookie web03 check inter 2s rise 2 fall 3
  78. listen jms-ssh
  79. bind *:2222
  80. mode tcp
  81. option tcp-check
  82. fullconn 500
  83. balance source
  84. server 192.168.100.21 192.168.100.21:2222 weight 1 check inter 2s rise 2 fall 3 send-proxy
  85. server 192.168.100.22 192.168.100.22:2222 weight 1 check inter 2s rise 2 fall 3 send-proxy
  86. server 192.168.100.23 192.168.100.23:2222 weight 1 check inter 2s rise 2 fall 3 send-proxy
  87. server 192.168.100.24 192.168.100.24:2222 weight 1 check inter 2s rise 2 fall 3 send-proxy
  88. listen jms-koko
  89. mode http
  90. option httpclose
  91. option forwardfor
  92. option httpchk GET /koko/health/ HTTP/1.1\r\nHost:\ 192.168.100.100 # KoKo 检活接口, host 填写 HAProxy 的 ip 地址
  93. cookie SERVERID insert indirect
  94. hash-type consistent
  95. fullconn 500
  96. balance leastconn
  97. server 192.168.100.21 192.168.100.21:80 weight 1 cookie web01 check inter 2s rise 2 fall 3
  98. server 192.168.100.22 192.168.100.22:80 weight 1 cookie web02 check inter 2s rise 2 fall 3
  99. server 192.168.100.23 192.168.100.23:80 weight 1 cookie web03 check inter 2s rise 2 fall 3
  100. server 192.168.100.24 192.168.100.24:80 weight 1 cookie web03 check inter 2s rise 2 fall 3
  101. listen jms-lion
  102. mode http
  103. option httpclose
  104. option forwardfor
  105. option httpchk GET /lion/health/ HTTP/1.1\r\nHost:\ 192.168.100.100 # Lion 检活接口, host 填写 HAProxy 的 ip 地址
  106. cookie SERVERID insert indirect
  107. hash-type consistent
  108. fullconn 500
  109. balance leastconn
  110. server 192.168.100.21 192.168.100.21:80 weight 1 cookie web01 check inter 2s rise 2 fall 3
  111. server 192.168.100.22 192.168.100.22:80 weight 1 cookie web02 check inter 2s rise 2 fall 3
  112. server 192.168.100.23 192.168.100.23:80 weight 1 cookie web03 check inter 2s rise 2 fall 3
  113. server 192.168.100.24 192.168.100.24:80 weight 1 cookie web03 check inter 2s rise 2 fall 3
  114. listen jms-magnus
  115. bind *:30000
  116. mode tcp
  117. option tcp-check
  118. fullconn 500
  119. balance source
  120. server 192.168.100.21 192.168.100.21:30000 weight 1 check inter 2s rise 2 fall 3 send-proxy
  121. server 192.168.100.22 192.168.100.22:30000 weight 1 check inter 2s rise 2 fall 3 send-proxy
  122. server 192.168.100.23 192.168.100.23:30000 weight 1 check inter 2s rise 2 fall 3 send-proxy
  123. server 192.168.100.24 192.168.100.24:30000 weight 1 check inter 2s rise 2 fall 3 send-proxy

2.3 配置 SELinux

  1. setsebool -P haproxy_connect_any 1

2.4 启动 HAProxy

  1. systemctl enable haproxy
  2. systemctl start haproxy

3 配置防火墙

  1. firewall-cmd --permanent --zone=public --add-port=80/tcp
  2. firewall-cmd --permanent --zone=public --add-port=443/tcp
  3. firewall-cmd --permanent --zone=public --add-port=2222/tcp
  4. firewall-cmd --permanent --zone=public --add-port=33060/tcp
  5. firewall-cmd --permanent --zone=public --add-port=33061/tcp
  6. firewall-cmd --reload