如何查看haproxy的版本:keepalived主备与双主模式配置
如何查看haproxy的版本:keepalived主备与双主模式配置BACKUP节点与MASTER节点定义大致相同,只有BACKUP节点的角色,优先级需要修改,其他都不需要改动定义BACKUP节点Keepalived配置vim /etc/rsyslog.conf注意:上面必须要选择使用tcp或udp方式传输日志,如果不选择光定义日志路径是不生效的定义master节点的keepalived配置
正文haproxy Keepalived主备模式
主备节点设置
- 主备节点上各安装配置haproxy,配置内容且要相同
- global
- log 127.0.0.1 local2
- chroot /var/lib/haproxy
- pidfile /var/run/haproxy.pid
- maxconn 4000
- user haproxy
- group haproxy
- daemon
- defaults #defaults段默认值对frontend和backend和listen段生效
- mode http #运行模式为http
- log global
- option httplog
- option dontlognull
- option http-server-close
- option forwardfor except 127.0.0.0/8 #添加客户端真实ip地址
- option redispatch # #如果后端有服务器宕机,强制切换到正常服务器
- retries 3 #三次连接失败,则判断服务不可用
- timeout http-request 10s #请求超时时间
- timeout connect 10s #连接超时
- timeout client 1m #客户端超时
- timeout server 1m #服务端超时
- timeout http-keep-alive 10s #长连接超时时间
- timeout check 10s 检查超时时间
- maxconn 3000 每个进程最大连接数
- frontend web :80 #自定义一个web静态页面匹配前端
- acl url_html path_beg -i /
- acl url_html path_end -i .html
- use_backend website if url_html #如果符合匹配就使用website的后端主机
- frontend imgs :80 #自定义一个图片匹配前端
- acl url_img path_beg -i /
- acl url_img path_end -i .jpg .png .jpeg .gif
- use_backend img if url_img #如果符合匹配就使用img后端主机
- backend img #定义后端主机组 img
- server img1 192.168.214.135:80 check
- backend website #定义后端主机组website
- server html1 192.168.214.133:80 check
- server html2 192.168.214.135:80 check
haproxy开启日志记录
日志定义在haproxy的配置文件中已经说明了,haproxy使用rsyslog服务记录日志,需要在rsyslog日志服务中定义:
vim /etc/rsyslog.conf
- $ModLoad imudp #取消注释 ,使用udp传输日志
- $UDPServerRun 514 #取消注释 ,使用udp传输日志
- local2.* /var/log/haproxy.log #按照配置文件里提示写即可
注意:上面必须要选择使用tcp或udp方式传输日志,如果不选择光定义日志路径是不生效的
定义master节点的keepalived配置
- global_defs {
- notification_email {
- acassen@firewall.loc
- failover@firewall.loc
- sysadmin@firewall.loc
- }
- notification_email_from Alexandre.Cassen@firewall.loc
- smtp_server 192.168.214.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- vrrp_skip_check_adv_addr
- vrrp_strict
- vrrp_garp_interval 0
- vrrp_gna_interval 0
- vrrp_iptables #禁止keepalived启动生成默认的iptables规则
- vrrp_mcast_group4 224.17.17.17 #定义主备节点通过组播地址进行通告状态
- }
- vrrp_script chk_down {
- script "/bin/bash -c '[[ -f /etc/keepalived/down ]]' && exit 1 || exit 0"
- interval 1
- weight -10
- }
- vrrp_script chk_haproxy { #调用外部的辅助脚本进行资源监控 并根据监控的结果状态能实现优先动态调整
- script "/usr/bin/killall -0 haproxy && exit 0 || exit 1"
- #script指令:先定义一个执行脚本,如果脚本执行结果状态为0则不操作后续步奏,如果状态为非0,则执行相应的操作
- interval 1 #每秒检查执行一次
- weight -10 #如果脚本执行结果为非0 ,则keepalived的优先级减去10
- fall 2 #如果连续两次检测为错误状态则认为服务部可用
- rise 1 #检测一次成功就认为服务正常
- }
- vrrp_instance VI_1 { #配置虚拟路由实例
- state MASTER #定义该节点为MASTER节点
- interface ens33 #定义VIP绑定的物理网卡
- virtual_router_id 55 #设置虚路由拟路由id,同一集群的节点群必须相同
- priority 100 #设定优先级
- advert_int 1 #设定master与backup之间vrrp通告的时间间隔,单位是秒
- # nopreempt #设定keepalived的切换模式,默认是抢占failover,这里是非抢占,没有启用
- authentication { #定义验证方式与密码
- auth_type PASS
- auth_pass 12345678 #密码最长8位
- }
- virtual_ipaddress { #定义虚拟路由IP 也是对外接收请求的ip
- 192.168.214.100
- }
- track_script { #用于追踪脚本执行状态,定义在vrrp_instance段中
- chk_down
- chk_haproxy
- }
- }
定义BACKUP节点Keepalived配置
BACKUP节点与MASTER节点定义大致相同,只有BACKUP节点的角色,优先级需要修改,其他都不需要改动
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- acassen@firewall.loc
- failover@firewall.loc
- sysadmin@firewall.loc
- }
- notification_email_from Alexandre.Cassen@firewall.loc
- smtp_server 192.168.214.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- vrrp_skip_check_adv_addr
- vrrp_strict
- vrrp_garp_interval 0
- vrrp_gna_interval 0
- vrrp_iptables
- vrrp_mcast_group4 224.17.17.17
- }
- vrrp_script chk_down {
- script "/bin/bash -c '[[ -f /etc/keepalived/down ]]' && exit 1 || exit 0"
- interval 1
- weight -10
- }
- vrrp_script chk_haproxy {
- script "/usr/bin/killall -0 haproxy && exit 0 || exit 1"
- interval 1
- weight -10
- fall 2
- rise 1
- }
- vrrp_instance VI_1 {
- state BACKUP
- interface ens33
- virtual_router_id 55
- priority 95
- advert_int 1
- # nopreempt
- authentication {
- auth_type PASS
- auth_pass 12345678
- }
- virtual_ipaddress {
- 192.168.214.100
- }
- track_script {
- chk_down
- chk_haproxy
- }
- }
双主模式配置
在上面配置的基础上,只要在各节点新建一个vrrp_instance VI_2 实例即可,在原来的MASTER节点定义第二个实例的为BACKUP角色,在原来的BACKUP节点再添加一个新的实例为MASTER,设置另一个虚拟路由实例的VIP为:192.168.214.200
vrrp_instance VI_2配置
最终在上面原来的MASTER节点配置如下(添加了最后一段vrrp_instance VI_2的定义)
- [root@node-1 ~]# vim /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- acassen@firewall.loc
- failover@firewall.loc
- sysadmin@firewall.loc
- }
- notification_email_from Alexandre.Cassen@firewall.loc
- smtp_server 192.168.214.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- vrrp_skip_check_adv_addr
- vrrp_strict
- vrrp_garp_interval 0
- vrrp_gna_interval 0
- vrrp_iptables
- vrrp_mcast_group4 224.17.17.17
- }
- vrrp_script chk_down { #定义vrrp_instance VI_1实例的检测文件
- script "/bin/bash -c '[[ -f /etc/keepalived/down ]]' && exit 1 || exit 0"
- interval 1
- weight -10
- }
- vrrp_script chk_node2 { #定义vrrp_instance VI_2实例的检测文件
- script "/bin/bash -c '[[ -f /etc/keepalived/nodedown ]]' && exit 1 || exit 0"
- interval 1
- weight -10
- }
- vrrp_script chk_haproxy {
- script "/usr/bin/killall -0 haproxy && exit 0 || exit 1"
- interval 1
- weight -10
- fall 2
- rise 1
- }
- #高可用组1
- vrrp_instance VI_1 {
- state MASTER
- interface ens33
- virtual_router_id 55
- priority 100
- advert_int 1
- # nopreempt
- authentication {
- auth_type PASS
- auth_pass 12345678
- }
- virtual_ipaddress {
- 192.168.214.100
- }
- track_script {
- chk_down
- chk_haproxy
- }
- }
- #高可用组2
- vrrp_instance VI_2 {
- state BACKUP
- interface ens33
- virtual_router_id 33
- priority 95
- advert_int 1
- # nopreempt
- authentication {
- auth_type PASS
- auth_pass 87654321
- }
- virtual_ipaddress {
- 192.168.214.200
- }
- track_script {
- chk_node2
- chk_haproxy
- }
- }
最终在上面原来的BACKUP节点配置如下(添加了最后一段vrrp_instance VI_2的定义)
- [root@node-2 ~]# vim /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- acassen@firewall.loc
- failover@firewall.loc
- sysadmin@firewall.loc
- }
- notification_email_from Alexandre.Cassen@firewall.loc
- smtp_server 192.168.214.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- vrrp_skip_check_adv_addr
- vrrp_strict
- vrrp_garp_interval 0
- vrrp_gna_interval 0
- vrrp_iptables
- vrrp_mcast_group4 224.17.17.17
- }
- vrrp_script chk_down {
- script "/bin/bash -c '[[ -f /etc/keepalived/down ]]' && exit 1 || exit 0"
- interval 1
- weight -10
- }
- vrrp_script chk_node2 {
- script "/bin/bash -c '[[ -f /etc/keepalived/nodedown ]]' && exit 1 || exit 0"
- interval 1
- weight -10
- }
- vrrp_script chk_haproxy {
- script "/usr/bin/killall -0 haproxy && exit 0 || exit 1"
- interval 1
- weight -10
- fall 2
- rise 1
- }
- vrrp_instance VI_1 {
- state BACKUP
- interface ens33
- virtual_router_id 55
- priority 95
- advert_int 1
- # nopreempt
- authentication {
- auth_type PASS
- auth_pass 12345678
- }
- virtual_ipaddress {
- 192.168.214.100
- }
- track_script {
- chk_down
- chk_haproxy
- }
- }
- vrrp_instance VI_2 {
- state MASTER
- interface ens33
- virtual_router_id 33
- priority 100
- advert_int 1
- # nopreempt
- authentication {
- auth_type PASS
- auth_pass 87654321
- }
- virtual_ipaddress {
- 192.168.214.200
- }
- track_script {
- chk_node2
- chk_haproxy
- }
- }
双主节点配置时要注意的地方
配置配置文件中有如下两段检测内容:
- vrrp_script chk_down {
- script "/bin/bash -c '[[ -f /etc/keepalived/down ]]' && exit 1 || exit 0"
- interval 1
- weight -10
- }
- vrrp_script chk_node2 {
- script "/bin/bash -c '[[ -f /etc/keepalived/nodedown ]]' && exit 1 || exit 0"
- interval 1
- weight -10
- }
这两段配置的作用是通过判断指定的路径下有无指定的文件来进行调整keepalived的优先级,从而可以实现不关闭服务就可以随时调整高可用的负载节点主备状态切换,这样可以方便服务的配置修改等操作。
上面的两段配置分别是针对两个 vrrp_instance 实例的配置,让两个不同的节点分别去检测不同的文件名,如果同时去检测同一个文件,会造成混乱。
另外一个就是,在主备节点设置优先级的时候,要确保当MASTER节点降级后的优先级要比BACKUP的优先级低,否则,VIP是无法进行漂移的。
上面的配置实验都正常,包括图片与页面的分离负载,主备节点的切换,和双主模式下的各主备节点切换