MySQL主主+Keepalived+LVS高可用(二):單點寫入
方案簡介
本方案採用keepalived中的sorry_server來實現寫入資料庫為單點的需求。本方案實現的功能是當網路有問題、mysql 有問題、 伺服器當機、 keepalived 服務停止後,伺服器能自動跳轉到備用機,當主伺服器服務啟動起來後會自動切換回來。
1) 安裝LVS
~]#rpm -ivh libnl-devel-1.1-14.el6.x86_64.rpm
~]#rpm -ivh popt-devel-1.13-7.el6.x86_64.rpm
~]#rpm -ivh popt-static-1.13-7.el6.x86_64.rpm
~]#wget
~]#tar -zxvf ipvsadm-1.26.tar.gz -C /opt/
~]#cd /opt/ipvsadm-1.26
~]#make && make install
2) 建立LVS指令碼
#Master A和B都執行此操作
~]# cat /etc/init.d/realserver
#!/bin/bash
VIP=192.168.56.111
. /etc/rc.d/init.d/functions
case "$1" in
start)
/sbin/ifconfig lo down
/sbin/ifconfig lo up
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
/sbin/sysctl -p >/dev/null 2>&1
/sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
echo "LVS-DR real server starts successfully."
;;
stop)
/sbin/ifconfig lo:0 down
/sbin/route del $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 "LVS-DR real server stopped."
;;
status)
isLoOn=`/sbin/ifconfig lo:0 | grep "$VIP"`
isRoOn=`/bin/netstat -rn | grep "$VIP"`
if [ "$isLoOn" == "" -a "$isRoOn" == "" ]; then
echo "LVS-DR real server has to run yet."
else
echo "LVS-DR real server is running."
fi
exit 3
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac
exit 0
3) KeepAlived配置
Master A
~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
! Configuration File for keepalived
global_defs {
notification_email {
jixiang.yu@trekiz.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MySQL_HA
}
vrrp_instance MySQL-HA{
state MASTER
interface eth1
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.56.111
}
}
virtual_server 192.168.56.111 3306{
delay_loop 3
lb_algo rr
lb_kind DR
persistence_timeout 20
protocol TCP
sorry_server 192.168.56.102 3306
real_server 192.168.56.101 3306{
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
Master B
~]# cat /etc/keepalived/keepalived.conf
global_defs {
notification_email {
jixiang.yu@trekiz.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MySQL_HA
}
vrrp_instance MySQL-HA{
state BACKUP
interface eth1
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.56.111
}
}
virtual_server 192.168.56.111 3306{
delay_loop 3
lb_algo rr
lb_kind DR
persistence_timeout 20
protocol TCP
sorry_server 192.168.56.102 3306
real_server 192.168.56.101 3306{
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
4) realserver配置
#A和B一樣
~]# cat /etc/init.d/realserver
#!/bin/bash
VIP=192.168.56.111
. /etc/rc.d/init.d/functions
case "$1" in
start)
/sbin/ifconfig lo down
/sbin/ifconfig lo up
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
/sbin/sysctl -p >/dev/null 2>&1
/sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
echo "LVS-DR real server starts successfully."
;;
stop)
/sbin/ifconfig lo:0 down
/sbin/route del $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 "LVS-DR real server stopped."
;;
status)
isLoOn=`/sbin/ifconfig lo:0 | grep "$VIP"`
isRoOn=`/bin/netstat -rn | grep "$VIP"`
if [ "$isLoOn" == "" -a "$isRoOn" == "" ]; then
echo "LVS-DR real server has to run yet."
else
echo "LVS-DR real server is running."
fi
exit 3
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac
exit 0
5) 啟動realserver和KeepAlived服務
#A和B都分別啟動
~]# /etc/init.d/realserver start
LVS-DR real server starts successfully.
~]# /etc/init.d/keepalived start
Starting keepalived:
Master A~]# ipvsadm -ln
~]# 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.56.111:3306 rr persistent 20
-> 192.168.56.101:3306 Local 3 0 0
Master B~]# 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.56.111:3306 rr persistent 20
-> 192.168.56.101:3306 Route 3 0 0
並將 keepalived 和 realserver 的啟動指令碼加入到 rc.local 自啟動中:
echo “/etc/rc.d/init.d/realserver.sh
start” >> /etc/rc.local
echo “/etc/rc.d/init.d/keepalived start” >> /etc/rc.local
6) Failover測試
1. 停止Master A上的mysql, MySQL自動切換到sorry_server,使用如下命令檢視:
~]# 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.56.111:3306 rr persistent 20
-> 192.168.56.102:3306 Route 1 1 0
2. 停止Master上的keepalived,VIP飄到backup(Master B)上,如:
3. 啟動Master A上的mysql, MySQL服務會切換到Master(101)上
4. 啟動Master A上的keepalived,VIP切換回Master(101)
5. 重啟Master上的mysql,先切換到backup(102),再切換回101
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27000195/viewspace-1364714/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL主主+Keepalived+LVS高可用(三):單點寫入、讀負載均衡MySql負載
- MySQL主主複製+slave+MMM實現高可用(二)MySql
- MySQL高可用(二)主備延時如何解決?MySql
- MySQL進階:主主複製+Keepalived高可用MySql
- MySQL主主複製+MMM實現高可用(一)MySql
- MySQL主主複製+Keepalived打造高可用MySQL叢集MySql
- MySQL主主模式+Keepalived高可用MySql模式
- mysql5.6主主複製及keepalived 高可用MySql
- mysql主主複製+keepalived 打造高可用mysql叢集薦MySql
- [Mysql高可用]——雙主互備+keepalivedMySql
- MySQL 高可用架構:主從備份及讀寫分離MySql架構
- MySQL 高可用性—keepalived+mysql雙主MySql
- MHA+MySQL主從配置實現MySQL高可用MySql
- 【Mysql】MySQL 主主複製 + LVS + Keepalived 實現 MySQL 高可用性MySql
- mysql+keepalived 雙主熱備高可用MySql
- mysql mha 主從自動切換 高可用MySql
- 主機系統高可用
- MySQL叢集搭建(6)-雙主+keepalived高可用MySql
- MySQL8.0 高可用叢集化 · mysql-shell · mysql-router · docker · 單主多從MySqlDocker
- MySQL 高可用性之 Keepalived 雙主熱備MySql
- MYSQL5.6主從+keepalive高可用自動切換MySql
- 從MySQL雙主高可用架構,談戀愛關係。MySql架構
- Mysql+Keepalived雙主熱備高可用操作記錄MySql
- Redis高可用-主從,哨兵,叢集Redis
- 高可用Mysql架構_Mysql主從複製、Mysql雙主熱備、Mysql雙主雙從、Mysql讀寫分離(Mycat中介軟體)、Mysql分庫分表架構(Mycat中介軟體)的演變MySql架構
- MySQL主從原理, 高可用架構與高效能架構MySql架構
- MySQL高可用架構之Keepalived+主從架構部署MySql架構
- Haproxy+Keepalived高可用環境部署梳理(主主和主從模式)模式
- MySQL:雙主單寫 主庫偶爾出現大量延遲的原因MySql
- Redis高可用之戰:主從架構Redis架構
- Mysql 主-主簡單配置 --centos6.4MySqlCentOS
- MySQL資料庫各場景主從高可用架構實戰MySql資料庫架構
- MYSQL5.7.22 原始碼安裝 主從搭建 + KEEPALIVED高可用MySql原始碼
- Mycat 雙主雙從-負載均衡-高可用負載
- 雙機高可用、負載均衡、MySQL(讀寫分離、主從自動切換)架構設計負載MySql架構
- Mysql雙主熱備+LVS+Keepalived高可用操作記錄MySql
- 高可用Mysql架構_Mycat叢集部署(HAProxy + 兩臺Mycat+Mysql雙主雙從)MySql架構
- mysql主主同步MySql