keepalived实现主从、主主架构

1.主从配置:

准备2个节点:dr1:192.168.32.128;dr2:192.168.32.132

#安装keepalived
[root@dr1 ~]# yum install -y keepalived
#同步时间到阿里云
[root@dr1 ~]# ntpdate ntp1.aliyun.com
#在dr1上编辑配置文件
[root@dr1 keepalived]# vim keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id dr1
        vrrp_mcast_group4 224.1.105.33
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 33
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.32.99 dev ens33 label ens33:0
    }
}
#同理安装编辑dr2
[root@dr2 keepalived]# vim keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id dr2
        vrrp_mcast_group4 224.1.105.33
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 33
    priority 96
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.32.99 dev ens33 label ens33:0
    }
}

在dr2上启动keepalived

[root@dr2 keepalived]# systemctl start keepalived
[root@dr2 keepalived]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2018-12-25 15:32:09 CST; 10s ago
  Process: 7309 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 7310 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─7310 /usr/sbin/keepalived -D
           ├─7311 /usr/sbin/keepalived -D
           └─7312 /usr/sbin/keepalived -D

Dec 25 15:32:09 dr2 Keepalived_healthcheckers[7311]: Registering Kernel netlink command channel
Dec 25 15:32:09 dr2 Keepalived_healthcheckers[7311]: Opening file '/etc/keepalived/keepalived.conf'.
Dec 25 15:32:09 dr2 Keepalived_healthcheckers[7311]: Configuration is using : 7661 Bytes
Dec 25 15:32:09 dr2 Keepalived_healthcheckers[7311]: Using LinkWatch kernel netlink reflector...
Dec 25 15:32:13 dr2 Keepalived_vrrp[7312]: VRRP_Instance(VI_1) Transition to MASTER STATE
Dec 25 15:32:14 dr2 Keepalived_vrrp[7312]: VRRP_Instance(VI_1) Entering MASTER STATE
Dec 25 15:32:14 dr2 Keepalived_vrrp[7312]: VRRP_Instance(VI_1) setting protocol VIPs.
Dec 25 15:32:14 dr2 Keepalived_vrrp[7312]: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens33 for 192.168.32.99
Dec 25 15:32:14 dr2 Keepalived_healthcheckers[7311]: Netlink reflector reports IP 192.168.32.99 added
Dec 25 15:32:19 dr2 Keepalived_vrrp[7312]: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens33 for 192.168.32.99
#抓包测试
[root@dr2 keepalived]# tcpdump -i ens33 -nn host 224.1.105.33
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 65535 bytes
15:34:18.613944 IP 192.168.32.132 > 224.1.105.33: VRRPv2, Advertisement, vrid 33, prio 96, authtype simple, intvl 1s, length 20
15:34:19.614996 IP 192.168.32.132 > 224.1.105.33: VRRPv2, Advertisement, vrid 33, prio 96, authtype simple, intvl 1s, length 20

在dr1上启动keepalived

[root@dr1 keepalived]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2018-12-25 15:35:12 CST; 19s ago
  Process: 11609 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 11610 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─11610 /usr/sbin/keepalived -D
           ├─11611 /usr/sbin/keepalived -D
           └─11612 /usr/sbin/keepalived -D

Dec 25 15:35:12 dr1 Keepalived_healthcheckers[11611]: Opening file '/etc/keepalived/keepalived.conf'.
Dec 25 15:35:12 dr1 Keepalived_healthcheckers[11611]: Configuration is using : 7663 Bytes
Dec 25 15:35:12 dr1 Keepalived_healthcheckers[11611]: Using LinkWatch kernel netlink reflector...
Dec 25 15:35:13 dr1 Keepalived_vrrp[11612]: VRRP_Instance(VI_1) Transition to MASTER STATE
Dec 25 15:35:13 dr1 Keepalived_vrrp[11612]: VRRP_Instance(VI_1) Received lower prio advert, forcing new election
Dec 25 15:35:14 dr1 Keepalived_vrrp[11612]: VRRP_Instance(VI_1) Entering MASTER STATE
Dec 25 15:35:14 dr1 Keepalived_vrrp[11612]: VRRP_Instance(VI_1) setting protocol VIPs.
Dec 25 15:35:14 dr1 Keepalived_vrrp[11612]: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens33 for 192.168.32.99
Dec 25 15:35:14 dr1 Keepalived_healthcheckers[11611]: Netlink reflector reports IP 192.168.32.99 added
Dec 25 15:35:19 dr1 Keepalived_vrrp[11612]: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens33 for 192.168.32.99
#可看到dr1取代了dr2变成了master状态
#抓包测试
[root@dr1 keepalived]# tcpdump -i ens33 -nn host 224.1.105.33
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 65535 bytes
15:37:19.393988 IP 192.168.32.128 > 224.1.105.33: VRRPv2, Advertisement, vrid 33, prio 100, authtype simple, intvl 1s, length 20
15:37:20.395631 IP 192.168.32.128 > 224.1.105.33: VRRPv2, Advertisement, vrid 33, prio 100, authtype simple, intvl 1s, length 20

2. 双主模式配置

#在dr1节点上修改文件keepalived.conf,在最后面添加以下内容
vrrp_instance VI_2 {
    state BACKUP
    interface ens33
    virtual_router_id 34
    priority 96
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 2222
    }   
    virtual_ipaddress {
        192.168.32.98 dev ens33 label ens33:0
    }   
}   
#在dr2节点上同理添加以下内容
vrrp_instance VI_2 {
    state MASTER
    interface ens33
    virtual_router_id 34
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 2222
    }   
    virtual_ipaddress {
        192.168.32.98 dev ens33 label ens33:0
    }   
}   
#dr1停止服务
[root@dr1 keepalived]# systemctl stop keepalived
#dr2重启服务
[root@dr2 keepalived]# systemctl restart keepalived
#查看到dr2对于实例1,2都是master状态
[root@dr2 keepalived]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2018-12-25 15:44:12 CST; 26s ago
  Process: 7441 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 7442 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─7442 /usr/sbin/keepalived -D
           ├─7443 /usr/sbin/keepalived -D
           └─7444 /usr/sbin/keepalived -D

Dec 25 15:44:14 dr2 Keepalived_vrrp[7444]: VRRP_Instance(VI_2) setting protocol VIPs.
Dec 25 15:44:14 dr2 Keepalived_vrrp[7444]: VRRP_Instance(VI_2) Sending gratuitous ARPs on ens33 for 192.168.32.98
Dec 25 15:44:14 dr2 Keepalived_healthcheckers[7443]: Netlink reflector reports IP 192.168.32.98 added
Dec 25 15:44:16 dr2 Keepalived_vrrp[7444]: VRRP_Instance(VI_1) Transition to MASTER STATE
Dec 25 15:44:17 dr2 Keepalived_vrrp[7444]: VRRP_Instance(VI_1) Entering MASTER STATE
Dec 25 15:44:17 dr2 Keepalived_vrrp[7444]: VRRP_Instance(VI_1) setting protocol VIPs.
Dec 25 15:44:17 dr2 Keepalived_vrrp[7444]: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens33 for 192.168.32.99
Dec 25 15:44:17 dr2 Keepalived_healthcheckers[7443]: Netlink reflector reports IP 192.168.32.99 added
Dec 25 15:44:19 dr2 Keepalived_vrrp[7444]: VRRP_Instance(VI_2) Sending gratuitous ARPs on ens33 for 192.168.32.98
Dec 25 15:44:22 dr2 Keepalived_vrrp[7444]: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens33 for 192.168.32.99
#dr1启动服务
[root@dr1 keepalived]# systemctl start keepalived
#可以看到dr1对于实例1为master状态,对实例2为backup状态
[root@dr1 keepalived]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2018-12-25 15:45:35 CST; 6s ago
  Process: 11716 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 11717 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─11717 /usr/sbin/keepalived -D
           ├─11718 /usr/sbin/keepalived -D
           └─11719 /usr/sbin/keepalived -D

Dec 25 15:45:35 dr1 Keepalived_vrrp[11719]: Using LinkWatch kernel netlink reflector...
Dec 25 15:45:35 dr1 Keepalived_vrrp[11719]: VRRP_Instance(VI_2) Entering BACKUP STATE
Dec 25 15:45:35 dr1 Keepalived_vrrp[11719]: VRRP sockpool: [ifindex(2), proto(112), u...)]
Dec 25 15:45:35 dr1 Keepalived_vrrp[11719]: VRRP_Instance(VI_1) Transition to MASTER STATE
Dec 25 15:45:35 dr1 Keepalived_vrrp[11719]: VRRP_Instance(VI_1) Received lower prio a...on
Dec 25 15:45:36 dr1 Keepalived_vrrp[11719]: VRRP_Instance(VI_1) Entering MASTER STATE
Dec 25 15:45:36 dr1 Keepalived_vrrp[11719]: VRRP_Instance(VI_1) setting protocol VIPs.
Dec 25 15:45:36 dr1 Keepalived_vrrp[11719]: VRRP_Instance(VI_1) Sending gratuitous AR...99
Dec 25 15:45:36 dr1 Keepalived_healthcheckers[11718]: Netlink reflector reports IP 192...d
Dec 25 15:45:41 dr1 Keepalived_vrrp[11719]: VRRP_Instance(VI_1) Sending gratuitous AR...99
Hint: Some lines were ellipsized, use -l to show in full.

3. 通知脚本使用方式

#编辑通知脚本
[root@dr1 ~]# cd /etc/keepalived/
[root@dr1 keepalived]# vim notify.sh 
#!/bin/bash
contact='root@localhost'
notify () {
        local mailsubject="$(hostname) to be $1 vip floating"
        local 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
#在dr1,dr2节点keepalived.conf中的vrrp实例中添加如下内容
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 33
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.32.99 dev ens33 label ens33:0
    }
        notify_master "/etc/keepalived/notify.sh master"  #添加
        notify_backup "/etc/keepalived/notify.sh backup"  #添加
        notify_fault "/etc/keepalived/notify.sh fault"     #添加
}

你可能感兴趣的:(keepalived实现主从、主主架构)