Centos7 安装 Keepalived

目标: Keeplaived 简单模拟测试一下Nginx 故障切换
前言:C7 默认的 1.3.5 似乎有点问题,改装 keepalived-2.0.7


1:安装 Nginx 和确认 (略)
2:安装配置 Keepalived
3:Nginx 健康脚本
4:Maix 邮件报警

 


 

2:安装配置 Keepalived

2.1 安装 cheese 源
# wget http://www.nosuchhost.net/~cheese/fedora/packages/epel-7/x86_64/cheese-release-7-1.noarch.rpm
2.2 rpm -ivh cheese-release-7-1.noarch.rpm
2.3 yum install keepalived                                                    
======================================================================
 Package                      Arch    Version                         Repository    Size
======================================================================
Installing:                                                          
 keepalived                   x86_64  2.0.7-1.el7                     cheese       417 k
Installing for dependencies:                                         
 libnl                        x86_64  1.1.4-3.el7                     base         128 k
 lm_sensors-libs              x86_64  3.4.0-8.20160601gitf9185e5.el7  base          42 k
 net-snmp-agent-libs          x86_64  1:5.7.2-48.el7_8.1              updates      707 k
 net-snmp-libs                x86_64  1:5.7.2-48.el7_8.1              updates      751 k
                                                                     
Transaction Summary                                                  
======================================================================

2.4 配置
# vi /etc/keepalived/keepalived.conf
主节点配置
########## Master ##################
global_defs {
   notification_email {
     acassen@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.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_instance VI_1 {
    state MASTER         # 主节点
    interface ens192      # 网卡名称 !
    virtual_router_id 51
    priority 100         # 优先级高
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.5.40    # 虚拟IP
    }
}
########## 余下内容可先删除 ###########

备用节点配置
########## Backup ##################
global_defs {
   notification_email {
     acassen@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.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_instance VI_1 {
    state Backup         # 备用节点
    interface ens192   # 网卡名称 !
    virtual_router_id 51
    priority 90             # 优先级低
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.5.40    # 虚拟IP
    }
}
########## 余下内容可先删除 ########


2.5 服务启动
# systemctl start keepalived (主/备顺序启动)
# tailf /var/log/message  (可监控LOG)

41 systemd: Starting LVS and VRRP High Availability Monitor...
41 Keepalived[1332]: Starting Keepalived v2.0.7 (08/23,2018)
41 Keepalived[1332]: Running on Linux 3.10.0-1127.13.1.el7.x86_64 #1 SMP Tue Jun 23 (built for Linux 3.10.0)
41 Keepalived[1332]: Command line: '/usr/sbin/keepalived' '-D'
41 Keepalived[1332]: Opening file '/etc/keepalived/keepalived.conf'.
41 Keepalived[1332]: (Line 13) number '0' outside range [1e-06, 4294]
41 Keepalived[1332]: (Line 13) vrrp_garp_interval '0' is invalid
41 Keepalived[1332]: (Line 14) number '0' outside range [1e-06, 4294]
41 Keepalived[1332]: (Line 14) vrrp_gna_interval '0' is invalid
41 systemd: Started LVS and VRRP High Availability Monitor.
41 Keepalived[1333]: Starting VRRP child process, pid=1334
41 Keepalived_vrrp[1334]: Registering Kernel netlink reflector
41 Keepalived_vrrp[1334]: Registering Kernel netlink command channel
41 Keepalived_vrrp[1334]: Opening file '/etc/keepalived/keepalived.conf'.
41 Keepalived_vrrp[1334]: Assigned address 192.168.5.41 for interface ens192
41 Keepalived_vrrp[1334]: Registering gratuitous ARP shared channel
41 Keepalived_vrrp[1334]: (VI_1) removing VIPs.
41 Keepalived_vrrp[1334]: (VI_1) Entering BACKUP STATE (init)
41 Keepalived_vrrp[1334]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(8,9)]
41 Keepalived_vrrp[1334]: (VI_1) Receive advertisement timeout
41 Keepalived_vrrp[1334]: (VI_1) Entering MASTER STATE
41 Keepalived_vrrp[1334]: (VI_1) setting VIPs.
41 Keepalived_vrrp[1334]: Sending gratuitous ARP on ens192 for 192.168.5.40
41 Keepalived_vrrp[1334]: (VI_1) Sending/queueing gratuitous ARPs on ens192 for 192.168.5.40
41 Keepalived_vrrp[1334]: Sending gratuitous ARP on ens192 for 192.168.5.40

※  number '0' outside range 可以改成 0.001


# ip addr  (主节点虚拟ip OK )
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:5f:11:11 brd ff:ff:ff:ff:ff:ff
    inet 192.168.5.41/24 brd 192.168.5.255 scope global ens192
       valid_lft forever preferred_lft forever
    inet 192.168.5.40/32 scope global ens192
       valid_lft forever preferred_lft forever
       
# ip addr (备份节点 无虚拟ip)
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:22:22:22 brd ff:ff:ff:ff:ff:ff
    inet 192.168.5.42/24 brd 192.168.5.255 scope global ens192
       valid_lft forever preferred_lft forever
       
2.6 访问测试
C:\Users\liu>ping 192.168.5.40
正在 Ping 192.168.5.40 具有 32 字节的数据:
来自 192.168.5.40 的回复: 字节=32 时间=8ms TTL=63
来自 192.168.5.40 的回复: 字节=32 时间=8ms TTL=63
64 bytes from 192.168.5.40: icmp_seq=1 ttl=64 time=0.047 ms

web 服务应该也能正常访问(主节点提供)

2.7 手动切换测试
# systemctl stop keepalived (主节点 停服务)
# tailf /var/log/message  (备用节点LOG显示已经接管)
42 Keepalived_vrrp[1570]: (VI_1) Backup received priority 0 advertisement
42 Keepalived_vrrp[1570]: (VI_1) Receive advertisement timeout
42 Keepalived_vrrp[1570]: (VI_1) Entering MASTER STATE
42 Keepalived_vrrp[1570]: (VI_1) setting VIPs.
42 Keepalived_vrrp[1570]: Sending gratuitous ARP on ens192 for 192.168.5.40
42 Keepalived_vrrp[1570]: (VI_1) Sending/queueing gratuitous ARPs on ens192 for 192.168.5.40
42 Keepalived_vrrp[1570]: Sending gratuitous ARP on ens192 for 192.168.5.40

web 服务应该也能正常访问(备用节点提供)
如果主节点恢复后会重新接管服务(根据配置)

3:Nginx 健康脚本

3.1 编辑 Nginx 监控Shell(使用脚本自动检测和切换服务,注意脚本运行权限)
# vi /etc/keepalived/nginx_health.sh

#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
    /usr/sbin/nginx
    sleep 2
    counter=$(ps -C nginx --no-heading|wc -l)
    if [ "${counter}" = "0" ]; then
       systemctl stop keepalived
    fi
fi

3.2 keepalived 配置文件中添加监控项
########## Master ##################
global_defs {
   notification_email {
     acassen@firewall.loc   # mail 故障报警
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.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_script nginx_check {
        script "/etc/keepalived/nginx_health.sh"  # nginx 检测脚本路径
        interval 2 # 检测间隔
        weight -20 # 如果条件成立权重 -20 注意数字
}

vrrp_instance VI_1 {
    state MASTER         # 主节点
    interface eth0         # 网卡名称 !
    virtual_router_id 51
    priority 100         # 优先级高
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }   
    track_script {
         nginx_check # Nginx监控服务
    }       
    virtual_ipaddress {
        192.168.5.40    # 虚拟IP
    }
}
########## 余下内容可先删除 ########

4:Mailx 邮件报警

4.1 安装mailx和配置(略)

注意25端口是否能够发送

4.2 编辑邮件监控Shell(使用脚本自动发送服务切换邮件,注意脚本运行权限)

# vi /etc/keepalived/mail.sh

contact='admin@cnblog.xyz'
notify() {
    mailsubject="$(hostname) to be $1, vip Trans"
    mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
    echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
    notify master
    ;;
backup)
    notify backup
    ;;
fault)
    notify fault
    ;;
*)
    echo "Usage: $(basename $0) {master|backup|fault}"
    exit 1
    ;;
esac

4.3 手动测试一下邮件脚本

[root@ nginx-A1] # sh /etc/keepalived/mail.sh backup
正常情况下应该能收到邮件

4.4 keepalived 配置文件中添加配置

...    
vrrp_script nginx_check {
        script "/etc/keepalived/nginx_health.sh"
        interval 2
        weight -20
}
    notify_master "/etc/keepalived/mail.sh master"   
    notify_backup "/etc/keepalived/mail.sh backup"
    notify_fault "/etc/keepalived/mail.sh fault"
}

主备切换时候正常情况下应该收到2封邮件。

posted @ 2020-07-16 18:42  tamatama  阅读(1901)  评论(0编辑  收藏  举报
GO TOP