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"     #添加
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,294评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,493评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,790评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,595评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,718评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,906评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,053评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,797评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,250评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,570评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,711评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,388评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,018评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,796评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,023评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,461评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,595评论 2 350

推荐阅读更多精彩内容