快捷搜索:  汽车  科技

如何查看haproxy的版本:keepalived主备与双主模式配置

如何查看haproxy的版本:keepalived主备与双主模式配置BACKUP节点与MASTER节点定义大致相同,只有BACKUP节点的角色,优先级需要修改,其他都不需要改动定义BACKUP节点Keepalived配置vim /etc/rsyslog.conf注意:上面必须要选择使用tcp或udp方式传输日志,如果不选择光定义日志路径是不生效的定义master节点的keepalived配置

正文

haproxy Keepalived主备模式

如何查看haproxy的版本:keepalived主备与双主模式配置(1)

主备节点设置

  • 主备节点上各安装配置haproxy,配置内容且要相同

  1. global
  2. log 127.0.0.1 local2
  3. chroot /var/lib/haproxy
  4. pidfile /var/run/haproxy.pid
  5. maxconn 4000
  6. user haproxy
  7. group haproxy
  8. daemon
  9. defaults #defaults段默认值对frontend和backend和listen段生效
  10. mode http #运行模式为http
  11. log global
  12. option httplog
  13. option dontlognull
  14. option http-server-close
  15. option forwardfor except 127.0.0.0/8 #添加客户端真实ip地址
  16. option redispatch # #如果后端有服务器宕机,强制切换到正常服务器
  17. retries 3 #三次连接失败,则判断服务不可用
  18. timeout http-request 10s #请求超时时间
  19. timeout connect 10s #连接超时
  20. timeout client 1m #客户端超时
  21. timeout server 1m #服务端超时
  22. timeout http-keep-alive 10s #长连接超时时间
  23. timeout check 10s 检查超时时间
  24. maxconn 3000 每个进程最大连接数
  25. frontend web :80 #自定义一个web静态页面匹配前端
  26. acl url_html path_beg -i /
  27. acl url_html path_end -i .html
  28. use_backend website if url_html #如果符合匹配就使用website的后端主机
  29. frontend imgs :80 #自定义一个图片匹配前端
  30. acl url_img path_beg -i /
  31. acl url_img path_end -i .jpg .png .jpeg .gif
  32. use_backend img if url_img #如果符合匹配就使用img后端主机
  33. backend img #定义后端主机组 img
  34. server img1 192.168.214.135:80 check
  35. backend website #定义后端主机组website
  36. server html1 192.168.214.133:80 check
  37. server html2 192.168.214.135:80 check

haproxy开启日志记录

日志定义在haproxy的配置文件中已经说明了,haproxy使用rsyslog服务记录日志,需要在rsyslog日志服务中定义:

vim /etc/rsyslog.conf


  1. $ModLoad imudp #取消注释 ,使用udp传输日志
  2. $UDPServerRun 514 #取消注释 ,使用udp传输日志
  3. local2.* /var/log/haproxy.log #按照配置文件里提示写即可

注意:上面必须要选择使用tcp或udp方式传输日志,如果不选择光定义日志路径是不生效的

定义master节点的keepalived配置


  1. global_defs {
  2. notification_email {
  3. acassen@firewall.loc
  4. failover@firewall.loc
  5. sysadmin@firewall.loc
  6. }
  7. notification_email_from Alexandre.Cassen@firewall.loc
  8. smtp_server 192.168.214.1
  9. smtp_connect_timeout 30
  10. router_id LVS_DEVEL
  11. vrrp_skip_check_adv_addr
  12. vrrp_strict
  13. vrrp_garp_interval 0
  14. vrrp_gna_interval 0
  15. vrrp_iptables #禁止keepalived启动生成默认的iptables规则
  16. vrrp_mcast_group4 224.17.17.17 #定义主备节点通过组播地址进行通告状态
  17. }
  18. vrrp_script chk_down {
  19. script "/bin/bash -c '[[ -f /etc/keepalived/down ]]' && exit 1 || exit 0"
  20. interval 1
  21. weight -10
  22. }
  23. vrrp_script chk_haproxy { #调用外部的辅助脚本进行资源监控 并根据监控的结果状态能实现优先动态调整
  24. script "/usr/bin/killall -0 haproxy && exit 0 || exit 1"
  25. #script指令:先定义一个执行脚本,如果脚本执行结果状态为0则不操作后续步奏,如果状态为非0,则执行相应的操作
  26. interval 1 #每秒检查执行一次
  27. weight -10 #如果脚本执行结果为非0 ,则keepalived的优先级减去10
  28. fall 2 #如果连续两次检测为错误状态则认为服务部可用
  29. rise 1 #检测一次成功就认为服务正常
  30. }
  31. vrrp_instance VI_1 { #配置虚拟路由实例
  32. state MASTER #定义该节点为MASTER节点
  33. interface ens33 #定义VIP绑定的物理网卡
  34. virtual_router_id 55 #设置虚路由拟路由id,同一集群的节点群必须相同
  35. priority 100 #设定优先级
  36. advert_int 1 #设定master与backup之间vrrp通告的时间间隔,单位是秒
  37. # nopreempt #设定keepalived的切换模式,默认是抢占failover,这里是非抢占,没有启用
  38. authentication { #定义验证方式与密码
  39. auth_type PASS
  40. auth_pass 12345678 #密码最长8位
  41. }
  42. virtual_ipaddress { #定义虚拟路由IP 也是对外接收请求的ip
  43. 192.168.214.100
  44. }
  45. track_script { #用于追踪脚本执行状态,定义在vrrp_instance段中
  46. chk_down
  47. chk_haproxy
  48. }
  49. }

定义BACKUP节点Keepalived配置

BACKUP节点与MASTER节点定义大致相同,只有BACKUP节点的角色,优先级需要修改,其他都不需要改动


  1. ! Configuration File for keepalived
  2. global_defs {
  3. notification_email {
  4. acassen@firewall.loc
  5. failover@firewall.loc
  6. sysadmin@firewall.loc
  7. }
  8. notification_email_from Alexandre.Cassen@firewall.loc
  9. smtp_server 192.168.214.1
  10. smtp_connect_timeout 30
  11. router_id LVS_DEVEL
  12. vrrp_skip_check_adv_addr
  13. vrrp_strict
  14. vrrp_garp_interval 0
  15. vrrp_gna_interval 0
  16. vrrp_iptables
  17. vrrp_mcast_group4 224.17.17.17
  18. }
  19. vrrp_script chk_down {
  20. script "/bin/bash -c '[[ -f /etc/keepalived/down ]]' && exit 1 || exit 0"
  21. interval 1
  22. weight -10
  23. }
  24. vrrp_script chk_haproxy {
  25. script "/usr/bin/killall -0 haproxy && exit 0 || exit 1"
  26. interval 1
  27. weight -10
  28. fall 2
  29. rise 1
  30. }
  31. vrrp_instance VI_1 {
  32. state BACKUP
  33. interface ens33
  34. virtual_router_id 55
  35. priority 95
  36. advert_int 1
  37. # nopreempt
  38. authentication {
  39. auth_type PASS
  40. auth_pass 12345678
  41. }
  42. virtual_ipaddress {
  43. 192.168.214.100
  44. }
  45. track_script {
  46. chk_down
  47. chk_haproxy
  48. }
  49. }

双主模式配置

在上面配置的基础上,只要在各节点新建一个vrrp_instance VI_2 实例即可,在原来的MASTER节点定义第二个实例的为BACKUP角色,在原来的BACKUP节点再添加一个新的实例为MASTER,设置另一个虚拟路由实例的VIP为:192.168.214.200

vrrp_instance VI_2配置

最终在上面原来的MASTER节点配置如下(添加了最后一段vrrp_instance VI_2的定义)


  1. [root@node-1 ~]# vim /etc/keepalived/keepalived.conf
  2. ! Configuration File for keepalived
  3. global_defs {
  4. notification_email {
  5. acassen@firewall.loc
  6. failover@firewall.loc
  7. sysadmin@firewall.loc
  8. }
  9. notification_email_from Alexandre.Cassen@firewall.loc
  10. smtp_server 192.168.214.1
  11. smtp_connect_timeout 30
  12. router_id LVS_DEVEL
  13. vrrp_skip_check_adv_addr
  14. vrrp_strict
  15. vrrp_garp_interval 0
  16. vrrp_gna_interval 0
  17. vrrp_iptables
  18. vrrp_mcast_group4 224.17.17.17
  19. }
  20. vrrp_script chk_down { #定义vrrp_instance VI_1实例的检测文件
  21. script "/bin/bash -c '[[ -f /etc/keepalived/down ]]' && exit 1 || exit 0"
  22. interval 1
  23. weight -10
  24. }
  25. vrrp_script chk_node2 { #定义vrrp_instance VI_2实例的检测文件
  26. script "/bin/bash -c '[[ -f /etc/keepalived/nodedown ]]' && exit 1 || exit 0"
  27. interval 1
  28. weight -10
  29. }
  30. vrrp_script chk_haproxy {
  31. script "/usr/bin/killall -0 haproxy && exit 0 || exit 1"
  32. interval 1
  33. weight -10
  34. fall 2
  35. rise 1
  36. }
  37. #高可用组1
  38. vrrp_instance VI_1 {
  39. state MASTER
  40. interface ens33
  41. virtual_router_id 55
  42. priority 100
  43. advert_int 1
  44. # nopreempt
  45. authentication {
  46. auth_type PASS
  47. auth_pass 12345678
  48. }
  49. virtual_ipaddress {
  50. 192.168.214.100
  51. }
  52. track_script {
  53. chk_down
  54. chk_haproxy
  55. }
  56. }
  57. #高可用组2
  58. vrrp_instance VI_2 {
  59. state BACKUP
  60. interface ens33
  61. virtual_router_id 33
  62. priority 95
  63. advert_int 1
  64. # nopreempt
  65. authentication {
  66. auth_type PASS
  67. auth_pass 87654321
  68. }
  69. virtual_ipaddress {
  70. 192.168.214.200
  71. }
  72. track_script {
  73. chk_node2
  74. chk_haproxy
  75. }
  76. }

最终在上面原来的BACKUP节点配置如下(添加了最后一段vrrp_instance VI_2的定义)


  1. [root@node-2 ~]# vim /etc/keepalived/keepalived.conf
  2. ! Configuration File for keepalived
  3. global_defs {
  4. notification_email {
  5. acassen@firewall.loc
  6. failover@firewall.loc
  7. sysadmin@firewall.loc
  8. }
  9. notification_email_from Alexandre.Cassen@firewall.loc
  10. smtp_server 192.168.214.1
  11. smtp_connect_timeout 30
  12. router_id LVS_DEVEL
  13. vrrp_skip_check_adv_addr
  14. vrrp_strict
  15. vrrp_garp_interval 0
  16. vrrp_gna_interval 0
  17. vrrp_iptables
  18. vrrp_mcast_group4 224.17.17.17
  19. }
  20. vrrp_script chk_down {
  21. script "/bin/bash -c '[[ -f /etc/keepalived/down ]]' && exit 1 || exit 0"
  22. interval 1
  23. weight -10
  24. }
  25. vrrp_script chk_node2 {
  26. script "/bin/bash -c '[[ -f /etc/keepalived/nodedown ]]' && exit 1 || exit 0"
  27. interval 1
  28. weight -10
  29. }
  30. vrrp_script chk_haproxy {
  31. script "/usr/bin/killall -0 haproxy && exit 0 || exit 1"
  32. interval 1
  33. weight -10
  34. fall 2
  35. rise 1
  36. }
  37. vrrp_instance VI_1 {
  38. state BACKUP
  39. interface ens33
  40. virtual_router_id 55
  41. priority 95
  42. advert_int 1
  43. # nopreempt
  44. authentication {
  45. auth_type PASS
  46. auth_pass 12345678
  47. }
  48. virtual_ipaddress {
  49. 192.168.214.100
  50. }
  51. track_script {
  52. chk_down
  53. chk_haproxy
  54. }
  55. }
  56. vrrp_instance VI_2 {
  57. state MASTER
  58. interface ens33
  59. virtual_router_id 33
  60. priority 100
  61. advert_int 1
  62. # nopreempt
  63. authentication {
  64. auth_type PASS
  65. auth_pass 87654321
  66. }
  67. virtual_ipaddress {
  68. 192.168.214.200
  69. }
  70. track_script {
  71. chk_node2
  72. chk_haproxy
  73. }
  74. }

双主节点配置时要注意的地方

配置配置文件中有如下两段检测内容:


  1. vrrp_script chk_down {
  2. script "/bin/bash -c '[[ -f /etc/keepalived/down ]]' && exit 1 || exit 0"
  3. interval 1
  4. weight -10
  5. }
  6. vrrp_script chk_node2 {
  7. script "/bin/bash -c '[[ -f /etc/keepalived/nodedown ]]' && exit 1 || exit 0"
  8. interval 1
  9. weight -10
  10. }

这两段配置的作用是通过判断指定的路径下有无指定的文件来进行调整keepalived的优先级,从而可以实现不关闭服务就可以随时调整高可用的负载节点主备状态切换,这样可以方便服务的配置修改等操作。

上面的两段配置分别是针对两个 vrrp_instance 实例的配置,让两个不同的节点分别去检测不同的文件名,如果同时去检测同一个文件,会造成混乱。

另外一个就是,在主备节点设置优先级的时候,要确保当MASTER节点降级后的优先级要比BACKUP的优先级低,否则,VIP是无法进行漂移的。

上面的配置实验都正常,包括图片与页面的分离负载,主备节点的切换,和双主模式下的各主备节点切换

猜您喜欢: