keepalived(二)vip漂移

十五十六發表於2018-09-14

實驗

內容:測試vip能否正常切換

架構圖

這裡寫圖片描述

環境

ip 系統 安裝的軟體
192.168.253.158 CentOS7 keepalived
192.168.253.128 CentOS7 keepalived

第一步:在192.168.253.128主機上安裝keepalived和發郵件的工具

yum install mailx -y
yum install keepalived -y

第二步:在192.168.253.128主機上修改配置檔案/etc/keepalived/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 192.168.253.128
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 15
    priority 95
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.168.253.190 dev ens33
    }
    notify_master "/etc/keepalived/notify.sh master" ##這個指令碼稍後會建立
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/nofify.sh fault"
}

第三步:建立keepalived狀態發生變化時觸發的指令碼/etc/keepalived/nofify.sh

#!/bin/bash
#發郵件通知root,keepalived狀態發生改變
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

第四步:在192.168.253.158上安裝keepalived,將192.168.253.128的指令碼與配置檔案傳送到192.168.253.158上
192.168.253.158端操作

yum install keepalived -y
yum install mailx -y

192.168.253.128端操作

scp /etc/keepalived/keepalived.conf  root@192.168.253.158:/etc/keepalived/keepalived.conf
scp /etc/keepalived/notify.sh  root@192.168.253.158:/etc/keepalived/notify.sh

第五步:在192.168.253.158上修改配置檔案

[root@localhost ~]# cat /etc/keepalived/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 192.168.253.158 ##修改為當前主機的主機名,這裡直接用ip了
}

vrrp_instance VI_1 {
    state BACKUP  ##設定為BACKUP節點
    interface ens33 
    virtual_router_id 15 
    priority 90   ##優先順序
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.168.253.190 dev ens33
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/nofify.sh fault"
}

第六步:測試
我們先啟動192.168.253.158備用節點的keepalived

systemctl  start keepalived 

看一下vip是否在192.168.253.158主機上
這裡寫圖片描述

這裡寫圖片描述
這裡可以看出192.168.253.158正在廣播自己的心跳資訊。

這時候我們吧192.168.253.128啟動起來

systemctl  start keepalived

這裡寫圖片描述
這裡可以看到通告的人變成192.168.253.128了

這裡寫圖片描述
vip這時候也飄過來了。說明沒問題。

我們再看一下192.168.253.128主機上指令碼是否成功觸發了
這裡寫圖片描述
收到郵件,成功

相關文章