MySQL + KeepAlived + LVS單點寫入主主同步高可用架構實驗 ㈠ 實戰環境 服務器名· IP OS MySQL odd.example.com 192.168.1.116 RHEL-5.8 5.5.16 even.example.com 192.168.1.115 RHEL-5.8 5.5.16 ㈡ 方案優缺點 優點 ● 安裝配置簡單, 實現方便,高可用效率好,可以根據服務與系統的可用性多方面進行切換 ● 可以將寫 VIP 和讀 VIP 分別進行設置,為讀寫分離做准備 ● 可以在後面添加多個從服務器,並做到負載均衡 缺點 ● 在啟動或者恢復後會立即替換掉定義的 sorry_server 因此如果要實現指定條件替換或者不替換需要通過其他方式實現,比如:臨時更改MySQL的端口等 ● 擴展不是很方便 ● 切換需要 1s 左右的時間 ㈢ 方案適用場景 這個方案適用於只有兩台數據庫服務器並且還沒有實現數據庫的讀寫分離的情況、讀和寫都配置 VIP 這個方案能夠便於單台數據庫的管理維護以及切換工作 比如進行大表的表結構更改、數據庫的升級等都是非常方便的 ㈣ 方案架構圖 ㈤ 部署步驟 ⑴ MySQL 安裝 請查閱我之前的一篇文章: MySQL 5.5 三大安裝方式:【二進制、源代碼編譯和RPM包】安裝步驟及優缺點比較 http://www.BkJia.com/database/201304/202705.html ⑵ MySQL主主同步配置 這也請查閱我之前的文章哈:MySQL主主同步配置 http://www.BkJia.com/database/201305/207858.html ⑶ LVS 安裝 在ODD EVEN: # wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz # 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: # wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz # tar -zxv -f keepalived-1.1.19.tar.gz # cd keepalived-1.1.19 # ./configure --prefix=/usr/local/keepalived # make # make install # 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 [plain] global_defs { notification_email { [email protected] } notification_email_from [email protected] 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 上 [plain] #!/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 [plain] 停止前: [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 停止後: [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.115:3306 Route 1 0 0 成功!!