㈠ 实战环境
服务器名· | 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 包】 安装步骤及优缺点比较
⑵ MySQL主主同步配置
这也请查阅我之前的文章哈:MySQL 主主同步配置
⑶ 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
- global_defs {
- notification_email {
- linwaterbin@gmail.com
- }
- notification_email_from bychjzh@gmail.com
- 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
- 停止后:
- [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
- 成功!!
- 顶
- 5
- 踩
- 0