keepalive
- 类似3/4/7层交换机制的软件,也就是平时说的第三层、第四层、第七层交换
- 作用是检测web服务器的状态,如果有一台web服务器、mysql服务器宕机、或工作出现故障,keepalived检测到问题后,会将有故障的web服务器或者mysql服务器从系统中剔除
- 当服务工作正常后,keepalived自动将web、mysql服务加入到当前的服务群中
- 切换过程全部自动完成,不需要人工干涉
- 人工任务只是修复有故障的web和mysql服务
keepalived安装配置
tar zxf keepalived-1.2.1.tar.gz cd keepalived-1.2.1 &&./configure --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686 &&make && make install DIR=/usr/local/
cp $DIR/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ cp $DIR/etc/sysconfig/keepalived /etc/sysconfig/ && mkdir -p /etc/keepalived cp $DIR/sbin/keepalived /usr/sbin/
修改master配置 vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { notification_email { [email protected]139.com } notification_email_from [email protected]139.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } # VIP1 vrrp_instance VI_1 { state MASTER interface eth0 lvs_sync_daemon_inteface eth0 virtual_router_id 151 priority 100 advert_int 5 nopreempt authentication { auth_type PASS auth_pass 2222 } virtual_ipaddress { 192.168.33.100 } } virtual_server 192.168.33.100 3306 { delay_loop 6 lb_algo wrr lb_kind DR persistence_timeout 60 protocol TCP real_server 192.168.33.10 3306 { weight 100 notify_down /data/sh/mysql.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } }
修改slave配置
- 只需要将realserver的IP改成real_server 192.168.33.11
- 优先级从100改成90
脚本内容
pkill keepalived
查看ipvs模块
modprobe ipvs
加载ipvs模块
modprobe ip_vs
lsmod|grep ip_vs
结论
- 使用keepalived的抢占模式
- 主DB挂了,切到backup上
- 主DB恢复了,自动切回master上---很可能出现DB问题,数据写了slave上,又写了master上
- 如果主上面的keepalived挂了(虽然几率小),master的db没挂,也切换到slave上,万一异步数据没同步(几率也低),数据也会不一致
- 这种方式有使用场景么?