這裡介紹如何配合前者實現Keepalived雙機熱備
系統環境:CentOS 6.3 x64
MySQL版本:mysql-5.6.10
Keepalived版本:keepalived-1.2.7
MySQL-VIP:192.168.7.253
MySQL-master1:192.168.7.201
MySQL-master2:192.168.7.249
首先關閉iptables和SELINUX
# service iptables stop
# setenforce 0
# vi /etc/sysconfig/selinux
---------------
SELINUX=disabled
---------------
注: 若線上需要開啟iptables,需加一條規則使keepalived的vrrp通行
# iptables -A INPUT -p vrrp -j ACCEPT
1.在MySQL-master1:192.168.7.201服務器上keepalived安裝及配置
編譯安裝,實際以本機kernel版本為准
# wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
# tar zxvf keepalived-1.2.7.tar.gz
# cd keepalived-1.2.7
# ./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-
279.el6.x86_64
# make && make install
設置keepalived開機啟動腳本
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# chkconfig keepalived on
新建一個配置文件,默認keepalived啟動會去/etc/keepalived目錄下尋找配置文件
# mkdir /etc/keepalived
# vi /etc/keepalived/keepalived.conf
------------------
global_defs {
notification_email {
}
#當主、備份設備發生改變時,通過郵件通知
notification_email_from [email protected]
smtp_server stmp.126.com
smtp_connect_timeout 30
router_id MySQL-ha
}
vrrp_instance VI_1{
# 在初始化狀態下定義為主設備
state BACKUP
# 注意網卡接口
interface eth0
virtual_router_id 51
# 優先級,另一台改為90
priority 100
advert_int 1
# 不主動搶占資源
nopreempt
authentication {
# 認證方式,可以是PASS或AH兩種認證方式
auth_type PASS
# 認證密碼
auth_pass 1111
}
virtual_ipaddress {
# 虛擬IP地址,隨著state的變化而增加刪除
192.168.7.253
}
}
virtual_server 192.168.7.253 3306 {
# 每個2秒檢查一次real_server狀態
delay_loop 2
# LVS算法
lb_algo wrr
# LVS模式
lb_kind DR
# 會話保持時間
persistence_timeout 60
protocol TCP
real_server 192.168.7.201 3306 {
# 權重
weight 3
# 檢測到服務down後執行的腳本
notify_down /etc/rc.d/keepalived.sh
TCP_CHECK {
# 連接超時時間
connect_timeout 10
# 重連次數
nb_get_retry 3
# 重連間隔時間
delay_before_retry 3
# 健康檢查端口
connect_port 3306
}
}
}
----------------------
編寫檢測服務down後所要執行的腳本
# vi /etc/rc.d/keepalived.sh
-------------
#!/bin/sh
/etc/init.d/keepalived stop
-------------
# chmod +x /etc/rc.d/keepalived.sh
注:此腳本是上面配置文件notify_down選項所用到的,keepalived使用notify_down選項來檢查
real_server的服務狀態。
當發現real_server服務故障時,便觸發此腳本.
我們可以看到,腳本就一個命令:
通過pkill keepalived強制殺死keepalived進程,從而實現了MySQL故障自動轉移.
另外,我們不用擔心兩個MySQL會同時提供數據更新操作,因為每台MySQL上的keepalived的配置裡面只
有本機MySQL的IP+VIP,而不是兩台MySQL的IP+VIP.
首頁 1 2 末頁