歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

CENTOS中如何利用Keepalived構建雙主MySQL+雙機熱備

系統環境: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 {

[email protected]

}

##當主、備份設備發生改變時,通過郵件通知

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.

以上腳本是為了在測試環境實現VIP切換功能,生產環境建議使用如下腳本

---------------------------------

#!/bin/bash

# 環境變量

PATH=/bin:/usr/bin:/sbin:/usr/sbin; export PATH

# 暫停5秒執行,防止數據庫人工正常重啟

sleep 5

# mysql_id(存活=1 死掉=0)

mysql_id=`ps -C mysqld --noheader |wc -l`

# 判斷mysql_id若死掉,則重啟mysql一次,若仍然無法啟動mysql則殺掉keepaliaved進程實現VIP切換

if [ $mysql_id -eq 0 ];then

/etc/init.d/mysqld restart

sleep 5

if [ $mysql_id -eq 0 ];then

/etc/init.d/keepalived stop

fi

fi

---------------------------------

啟動keepalived

# /etc/init.d/keepalived start

查看連接狀態

# ps -aux | grep keepalived

返回:

---------------

root 1387 1 0 21:13 ? 00:00:00 keepalived -D

root 1390 1387 0 21:13 ? 00:00:00 keepalived -D

root 1391 1387 0 21:13 ? 00:00:00 keepalived -D

root 1976 1911 0 21:16 pts/0 00:00:00 grep keepalived

--------------------

測試

查看VIP信息

# ip addr

--------------------------

............

inet 192.168.7.201/24 brd 192.168.7.255 scope global eth0

inet 192.168.7.253/32 scope global eth0

inet6 fe80::20c:29ff:feb2:9199/64 scope link

valid_lft forever preferred_lft forever

--------------------------

注:如果出現主從在切換VIP時,主的VIP未自動關閉,從的正常開啟,導致都獲取到VIP,造成互相沖突的情況,可手動輸入如下命令刪除一方的VIP地址

# ip addr del "虛擬ip" dev eth0

但此方法只是一個臨時的解決方案,當手動刪除VIP後,重啟keepalived服務會無法正常開啟vip,需要重啟服務器方能恢復。

查看vrrp通信

# tcpdump vrrp

-------------------------

.....

18:10:56.365730 IP 192.168.7.201 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 52, prio 40, authtype simple, intvl 1s, length 20

18:10:57.366825 IP 192.168.7.201 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 52, prio 40, authtype simple, intvl 1s, length 20

18:10:58.367914 IP 192.168.7.201 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 52, prio 40, authtype simple, intvl 1s, length 20

-------------------------

注:如果主keepalived掛掉,這裡的日志會切換到備keeplived的信息

找一台局域網PC,然後去ping MySQL的VIP,這時候MySQL的VIP是可以ping的通的

 

停止MySQL服務,看keepalived健康檢查程序是否會觸發我們編寫的腳本,去kill掉keepalived進程

# service mysqld stop

# ps -aux | grep keepalived

無返回結果

2.在MySQL-master2:192.168.7.249服務器上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 {

[email protected]

}

notification_email_from [email protected]

smtp_server stmp.126.com

smtp_connect_timeout 30

router_id MySQL-ha

}

vrrp_instance VI_1{

# 兩台配置此處均是BACKUP

state BACKUP

# 注意網卡接口

interface eth0

virtual_router_id 51

# 優先級,另一台改為100

priority 90

advert_int 1

# 不主動搶占資源

nopreempt

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

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.249 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

啟動keepalived

# /etc/init.d/keepalived start

查看連接狀態

# ps -aux | grep keepalived

返回:

---------------

root 1387 1 0 21:13 ? 00:00:00 keepalived -D

root 1390 1387 0 21:13 ? 00:00:00 keepalived -D

root 1391 1387 0 21:13 ? 00:00:00 keepalived -D

root 1976 1911 0 21:16 pts/0 00:00:00 grep keepalived

--------------------

至此配置完成,網站後台只需要配置MySQL-VIP:192.168.7.253虛擬地址即可

這裡實際連接的master數據庫,以哪台master先開mysql服務為准。

當master1:192.168.7.201 mysql端口擋掉或者服務器異常關閉,keepalived會自動跳轉到master2:192.168.7.249.

因為兩台數據庫的數據時同步的,用戶訪問的是MySQL-VIP:192.168.7.253虛擬地址,所以網站數據連接會無縫透明轉接到master2服務器,實現雙機熱備+數據同步功能。保證網站數據庫的實時可用性。

注:當某一台master服務器掛掉恢復後,需同時打開MYSQL服務和keepalived服務,保證另一台服務器如果掛掉會無縫轉接。

所以建議mysql與keeplived設置服務開機啟動。

# chkconfig mysqld on

# chkconfig keepalived on

----------大功告成------------

本文出自 “一路向北” 博客,請務必保留此出處http://showerlee.blog.51cto.com/2047005/1188549

Copyright © Linux教程網 All Rights Reserved