<a target="_blank" href="http://blog.csdn.net/dba_waterbin/article/details/8898235">mysql + keepalived + lvs 單點寫入主主同步高可用架構</a>
㈠ 實戰環境
伺服器名·
ip
os
mysql
odd.example.com
192.168.1.116
rhel-5.8
5.5.16
even.example.com
192.168.1.115
㈡ 方案優缺點
優點
● 安裝配置簡單, 實作友善,高可用效率好,可以根據服務與系統的可用性多方面進行切換
● 可以将寫 vip 和讀 vip 分别進行設定,為讀寫分離做準備
● 可以在後面添加多個從伺服器,并做到負載均衡
缺點
● 在啟動或者恢複後會立即替換掉定義的 sorry_server
是以如果要實作指定條件替換或者不替換需要通過其他方式實作,比如:臨時更改mysql的端口等
● 擴充不是很友善
● 切換需要 1s 左右的時間
㈢ 方案适用場景
這個方案适用于隻有兩台資料庫伺服器并且還沒有實作資料庫的讀寫分離的情況、讀和寫都配置 vip
這個方案能夠便于單台資料庫的管理維護以及切換工作
比如進行大表的表結構更改、資料庫的更新等都是非常友善的
㈣ 方案架構圖
㈤ 部署步驟
⑴ mysql 安裝
⑵ mysql主主同步配置
⑶ lvs 安裝
在odd even:
# ln -s /usr/src/kernels/2.6.18-308.el5xen-i686/ /usr/src/linux
# tar -zxv -f ipvsadm-1.24.tar.gz
# cd ipvsadm-1.24
# make
# make install
⑷ keepalived安裝
在odd even:
# tar -zxv -f keepalived-1.1.19.tar.gz
# cd keepalived-1.1.19
# ./configure --prefix=/usr/local/keepalived
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
# mkdir /etc/keepalived
⑸ keepalived配置
在 odd
global_defs {
notification_email {
}
smtp_server smtp.gmail.com
smtp_connect_timeout 30
router_id lvs1
}
vrrp_sync_group test {
group {
loadbalance
vrrp_instance loadbalance {
state backup
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type pass
auth_pass 1111
virtual_ipaddress {
192.168.1.128 dev eth0 label eth0:1
virtual_server 192.168.1.128 3306 {
delay_loop 6
lb_algo rr
lb_kind dr
persistence_timeout 20
protocol tcp
sorry_server 192.168.1.115 3306
real_server 192.168.1.116 3306 {
weight 3
tcp_check {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
在even:
大部分都和 odd 一樣、隻有 2 處:
● state backup
● priority 150
⑹ odd even 的 realsever 配置
在 odd even 上
#!/bin/bash
# description: config realserver lo and apply noarp
sns_vip=192.168.1.128
/etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $sns_vip netmask 255.255.255.255 broadcast $sns_vip
/sbin/route add -host $sns_vip dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo "realserver start ok"
;;
stop)
ifconfig lo:0 down
route del $sns_vip >/dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "realserver stoped"
*)
echo "usage: $0 {start|stop}"
exit 1
esac
exit 0
⑺ 啟動 keepalived和 realsever
在 odd even
# /etc/rc.d/init.d/realserver.sh start
# /etc/rc.d/init.d/keepalived start
# echo “/etc/rc.d/init.d/realserver.sh start” >> /etc/rc.local
# echo “/etc/rc.d/init.d/keepalived start” >> /etc/rc.local
配置到這邊大概都已經差不多了、接下來看看測試
㈥ 簡單測試
停掉odd mysql server 、看看能否自動卻換到 sorry_server
停止前:
[root@odd keepalived]# ipvsadm -ln
ip virtual server version 1.2.1 (size=4096)
prot localaddress:port scheduler flags
-> remoteaddress:port forward weight activeconn inactconn
tcp 192.168.1.128:3306 rr persistent 20
-> 192.168.1.116:3306 local 3 0 0
停止:
[mysql@odd ~]$ mysqladmin shutdown
130508 00:53:50 mysqld_safe mysqld from pid file /home/mysql/mysql/data/odd.example.com.pid ended
[1]+ done mysqld_safe
停止後:
-> 192.168.1.115:3306 route 1 0 0
成功!!