一、介紹html
Keeaplived主要有兩種應用場景,一個是經過配置keepalived結合ipvs作到負載均衡(LVS+Keepalived),有此需求者可參考以往博文:http://lizhenliang.blog.51cto.com/7876557/1343734。另外一個是經過自身健康檢查、資源接管功能作高可用(雙機熱備),實現故障轉移。mysql
如下內容主要針對Keepalived+MySQL雙主實現雙機熱備爲根據,主要講解keepalived的狀態轉換通知功能,利用此功能可有效增強對MySQL數據庫監控。此文再也不講述Keepalived+MySQL雙主部署過程,有需求者可參考以往博文:http://lizhenliang.blog.51cto.com/7876557/1362313linux
二、keepalived主要做用nginx
keepalived採用VRRP(virtual router redundancy protocol),虛擬路由冗餘協議,以軟件的形式實現服務器熱備功能。一般狀況下是將兩臺linux服務器組成一個熱備組(master-backup),同一時間熱備組內只有一臺主服務器(master)提供服務,同時master會虛擬出一個共用IP地址(VIP),這個VIP只存在master上並對外提供服務。若是keepalived檢測到master宕機或服務故障,備服務器(backup)會自動接管VIP成爲master,keepalived並將master從熱備組移除,當master恢復後,會自動加入到熱備組,默認再搶佔成爲master,起到故障轉移功能。sql
三、工做在三層、四層和七層原理shell
Layer3:工做在三層時,keepalived會按期向熱備組中的服務器發送一個ICMP數據包,來判斷某臺服務器是否故障,若是故障則將這臺服務器從熱備組移除。數據庫
Layer4:工做在四層時,keepalived以TCP端口的狀態判斷服務器是否故障,好比檢測mysql 3306端口,若是故障則將這臺服務器從熱備組移除。bash
示例: ! Configuration File for keepalived global_defs { notification_email { example@163.com } notification_email_from example@example.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MYSQL_HA } vrrp_instance VI_1 { state BACKUP interface eth1 virtual_router_id 50 nopreempt #當主down時,備接管,主恢復,不自動接管 priority 100 advert_int 1 authentication { auth_type PASS ahth_pass 123 } virtual_ipaddress { 192.168.1.200 #虛擬IP地址 } } virtual_server 192.168.1.200 3306 { delay_loop 6 # lb_algo rr # lb_kind NAT persistence_timeout 50 protocol TCP real_server 192.168.1.201 3306 { #監控本機3306端口 weight 1 notify_down /etc/keepalived/kill_keepalived.sh #檢測3306端口爲down狀態就執行此腳本(只有keepalived關閉,VIP才漂移 ) TCP_CHECK { #健康狀態檢測方式,可針對業務需求調整(TTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK) connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
Layer7:工做在七層時,keepalived根據用戶設定的策略判斷服務器上的程序是否正常運行,若是故障則將這臺服務器從熱備組移除。服務器
示例: ! Configuration File for keepalived global_defs { notification_email { example@163.com } notification_email_from example@example.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MYSQL_HA } vrrp_script check_nginx { script /etc/keepalived/check_nginx.sh #檢測腳本 interval 2 #執行間隔時間 } vrrp_instance VI_1 { state BACKUP interface eth1 virtual_router_id 50 nopreempt #當主down時,備接管,主恢復,不自動接管 priority 100 advert_int 1 authentication { auth_type PASS ahth_pass 123 } virtual_ipaddress { 192.168.1.200 #虛擬IP地址 } track_script { #在實例中引用腳本 check_nginx } }
腳本內容以下: # cat /etc/keepalived/check_nginx.sh Count1=`netstat -antp |grep -v grep |grep nginx |wc -l` if [ $Count1 -eq 0 ]; then /usr/local/nginx/sbin/nginx sleep 2 Count2=`netstat -antp |grep -v grep |grep nginx |wc -l` if [ $Count2 -eq 0 ]; then service keepalived stop else exit 0 fi else exit 0 fi
四、健康狀態檢測方式負載均衡
4.1 HTTP服務狀態檢測
HTTP_GET或SSL_GET { url { path /index.html #檢測url,可寫多個 digest 24326582a86bee478bac72d5af25089e #檢測效驗碼 #digest效驗碼獲取方法:genhash -s IP -p 80 -u http://IP/index.html status_code 200 #檢測返回http狀態碼 } connect_port 80 #鏈接端口 connect_timeout 3 #鏈接超時時間 nb_get_retry 3 #重試次數 delay_before_retry 2 #鏈接間隔時間 }
4.2 TCP端口狀態檢測(使用TCP端口服務基本上均可以使用)
TCP_CHECK { connect_port 80 #健康檢測端口,默認爲real_server後跟端口 connect_timeout 5 nb_get_retry 3 delay_before_retry 3 }
4.3 郵件服務器SMTP檢測
SMTP_CHECK { #健康檢測郵件服務器smtp host { connect_ip connect_port } connect_timeout 5 retry 2 delay_before_retry 3 hello_name "mail.domain.com" }
4.4 用戶自定義腳本檢測real_server服務狀態
MISC_CHECK { misc_path /script.sh #指定外部程序或腳本位置 misc_timeout 3 #執行腳本超時時間 !misc_dynamic #不動態調整服務器權重(weight),若是啓用將經過退出狀態碼動態調整real_server權重值 }
五、狀態轉換通知功能
keepalived主配置郵件通知功能,默認當real_server宕機或者恢復時纔會發出郵件。有時咱們更想知道keepalived的主服務器故障切換後,VIP是否順利漂移到備服務器,MySQL服務器是否正常?那寫個監控腳本吧,能夠,但不必,由於keepalived具有狀態檢測功能,因此咱們直接使用就好了。
主配置默認郵件通知配置模板以下: global_defs # Block id { notification_email # To: { admin@example1.com ... } # From: from address that will be in header notification_email_from admin@example.com smtp_server 127.0.0.1 # IP smtp_connect_timeout 30 # integer, seconds router_id my_hostname # string identifying the machine, # (doesn't have to be hostname). enable_traps # enable SNMP traps }
5.1 實例狀態通知
a) notify_master :節點變爲master時執行
b) notify_backup : 節點變爲backup時執行
c) notify_fault : 節點變爲故障時執行
5.2 虛擬服務器檢測通知
a) notify_up : 虛擬服務器up時執行
b) notify_down : 虛擬服務器down時執行
示例: ! Configuration File for keepalived global_defs { notification_email { example@163.com } notification_email_from example@example.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MYSQL_HA } vrrp_instance VI_1 { state BACKUP interface eth1 virtual_router_id 50 nopreempt #當主down時,備接管,主恢復,不自動接管 priority 100 advert_int 1 authentication { auth_type PASS ahth_pass 123 } virtual_ipaddress { 192.168.1.200 } notify_master /etc/keepalived/to_master.sh notify_backup /etc/keepalived/to_backup.sh notify_fault /etc/keepalived/to_fault.sh } virtual_server 192.168.1.200 3306 { delay_loop 6 persistence_timeout 50 protocol TCP real_server 192.168.1.201 3306 { weight 1 notify_up /etc/keepalived/mysql_up.sh notify_down /etc/keepalived/mysql_down.sh TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
狀態參數後能夠是bash命令,也能夠是shell腳本,內容根據本身需求定義,以上示例中所涉及狀態腳本以下:
1) 當服務器改變爲主時執行此腳本
# cat to_master.sh #!/bin/bash Date=$(date +%F" "%T) IP=$(ifconfig eth0 |grep "inet addr" |cut -d":" -f2 |awk '{print $1}') Mail="baojingtongzhi@163.com" echo "$Date $IP change to master." |mail -s "Master-Backup Change Status" $Mail
2) 當服務器改變爲備時執行此腳本
# cat to_backup.sh #!/bin/bash Date=$(date +%F" "%T) IP=$(ifconfig eth0 |grep "inet addr" |cut -d":" -f2 |awk '{print $1}') Mail="baojingtongzhi@163.com" echo "$Date $IP change to backup." |mail -s "Master-Backup Change Status" $Mail
3) 當服務器改變爲故障時執行此腳本
# cat to_fault.sh #!/bin/bash Date=$(date +%F" "%T) IP=$(ifconfig eth0 |grep "inet addr" |cut -d":" -f2 |awk '{print $1}') Mail="baojingtongzhi@163.com" echo "$Date $IP change to fault." |mail -s "Master-Backup Change Status" $Mail
4) 當檢測TCP端口3306爲不可用時,執行此腳本,殺死keepalived,實現切換
# cat mysql_down.sh #!/bin/bash Date=$(date +%F" "%T) IP=$(ifconfig eth0 |grep "inet addr" |cut -d":" -f2 |awk '{print $1}') Mail="baojingtongzhi@163.com" pkill keepalived echo "$Date $IP The mysql service failure,kill keepalived." |mail -s "Master-Backup MySQL Monitor" $Mail
5) 當檢測TCP端口3306可用時,執行此腳本
# cat mysql_up.sh #!/bin/bash Date=$(date +%F" "%T) IP=$(ifconfig eth0 |grep "inet addr" |cut -d":" -f2 |awk '{print $1}') Mail="baojingtongzhi@163.com" echo "$Date $IP The mysql service is recovery." |mail -s "Master-Backup MySQL Monitor" $Mail