郵件通知須要先定義一個郵件發送的腳本,而後在虛擬IP的配置實例中添加相應的觸發機制來實現當keepalived主備發生變化時進行發送郵件,以通知運維人員的目的。linux
準備主機兩臺vim
server | hostname | ip |
---|---|---|
keepalived | s1 | 172.20.27.10 |
keepalived | s2 | 172.20.27.11 |
1.安裝mailxbash
[root@s1 ~]# yum install mailx -y
2.設置發件人的配置運維
[root@s1 ~]# vim /etc/mail.rc set from=438214186@qq.com set smtp=smtp.qq.com set smtp-auth-user=438214186@qq.com set smtp-auth-password=ugfwthlnfossbiif set smtp-auth=login set ssl-verify=ignore
3.編寫郵件通知腳本ide
[root@s1 ~]# vim /etc/keepalived/notify.sh #!/bin/bash contact='438214186@qq.com' notify() { mailsubject="$(hostname) to be $1, vip轉移" 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.在配置文件中調用腳本測試
[root@s1 ~]# vim /etc/keepalived/keepalived.conf root@mylinuxops.com } notification_email_from root@mylinuxops.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id s1.mylinuxops.com vrrp_skip_check_adv_addr #vrrp_strict vrrp_iptables vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 27 priority 100 advert_int 2 authentication { auth_type PASS auth_pass 1111 } unicast_src_ip 172.20.27.10 unicast_peer { 172.20.27.11 } virtual_ipaddress { 172.20.27.100 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" #當前節點轉爲失敗狀態時觸發的腳本 }
重啓服務rest
[root@s1 ~]# systemctl restart keepalived
s1節點配置完畢後,s2節點也執行相同的操做。code
當s1節點中止服務後,s2節點而發送郵件通知
router