标签:
keepalived官方有中文文档:LVS + Keepalived Chinese application doc - March 16, 2010。
keepalived 实现VRRP协议,从路由级别实现VIP切换,可以完全避免类似heartbeat 脑裂问题。可以很nice的实现主从、主备、互备方案,尤其是无状态业务,有状态业务就需要额外花些功夫了。
既然mysql 可以使用keepalived很好的做到主从切换,redis自然也就有样学样就成了。
redis主从实现完全没有mysql成熟,仅仅是可用而已,按照作者规划相应改进需要等3.0以后了。经过测试主从也不是那么完全不靠谱。主要问题在于同步连接断开之后需要重新全量同步,如果频繁进行会对主服务带来很大性能影响。 但现实中主从机器往往要求放在一个机柜同一台交换设备下,网络闪断情况极低;再者主从同步在同步数量量大情况下,需要将缓存区调得足够大,不然也容易造成连接断开。
实现切换逻辑如下:A B两台机器
1. A 、B 依次启动,A作为主、B为从
2.主A 挂掉,B接管业务,作为主
3.A 起来,作为从SLAVEOF B
4.B 挂掉,A 切回主
将一台全部作为主,即可实现主从,可做读写分离;也可以一台机器上多个实例一半主、一半从,实现互备份,两机同时接管业务,一台宕机后业务都集中在一台上。
在keepalived 有两个角色:Master(一个)、Backup(多个),如果设置一个为Master,但Master挂了后再起来,必然再次业务又一次切换,这对于有状态服务是不可接受的。解决方案就是两台机器都设置为Backup,而且优先级高的Backup设置为nopreemt 不抢占。
安装比较简单:
需要依赖包:openssl-devel(ubuntu 中为 libssl-dev),popt-devel (ubuntu中为libpopt-dev),redheat中都已经有了。
配置文件默认路径:/etc/keepalived/keepalived.conf 也可以手动指定路径,不过要注意的是手动指定需要使用绝对路径。
使用keepalived -D 运行,即可启动3个守护进程:一个父进程,一个check健康检查,一个Vrrp,-D将日志写入/var/log/message
配置如下:
主:
! Configuration File for keepalived global_defs { router_id test } vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 7001" interval 2 timeout 2 fall 3 } vrrp_script chk_redis2 { script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 7002" interval 2 timeout 2 fall 3 } vrrp_instance redis { state BACKUP # 主也配置为SLAVE interface wlan0} virtual_router_id 51 priority 150 nopreempt # 不抢占,注意加上 advert_int 1 authentication { auth_type PASS auth_pass lulu } virtual_ipaddress { 192.168.1.144 } track_script { chk_redis } notify_master "/etc/keepalived/scripts/redis_master_slave.sh 127.0.0.1 192.168.1.126 7001" notify_backup "/etc/keepalived/scripts/redis_backup_slave.sh 127.0.0.1 192.168.1.126 7001" notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh } vrrp_instance redis2 { state BACKUP interface wlan0 } virtual_router_id 50 priority 150 advert_int 1 nopreempt authentication { auth_type PASS auth_pass lulu } virtual_ipaddress { 192.168.1.145 } track_script { chk_redis2 } notify_master "/etc/keepalived/scripts/redis_master_slave.sh 127.0.0.1 192.168.1.126 7002" notify_backup "/etc/keepalived/scripts/redis_backup_slave.sh 127.0.0.1 192.168.1.126 7002" notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh }
备:
! Configuration File for keepalived global_defs { router_id test } vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 7001" interval 2 timeout 2 fall 3 } vrrp_script chk_redis2 { script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 7002" interval 2 timeout 2 fall 3 } vrrp_instance redis { state BACKUP interface wlan0 } virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass test } virtual_ipaddress { 192.168.1.144 } track_script { chk_redis } notify_master "/etc/keepalived/scripts/redis_master_slave.sh 127.0.0.1 192.168.1.126 7001" notify_backup "/etc/keepalived/scripts/redis_backup_slave.sh 127.0.0.1 192.168.1.126 7001" notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh } vrrp_instance redis2 { state BACKUP interface wlan0 } virtual_router_id 50 advert_int 1 priority 100 authentication { auth_type PASS auth_pass test } virtual_ipaddress { 192.168.1.145 } track_script { chk_redis2 } notify_master "/etc/keepalived/scripts/redis_master_slave.sh 127.0.0.1 192.168.1.126 7002" notify_backup "/etc/keepalived/scripts/redis_backup_slave.sh 127.0.0.1 192.168.1.126 7002" notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh }
其他脚本见附件:http://files.cnblogs.com/lulu/scripts.rar
标签:
原文地址:http://my.oschina.net/ydsakyclguozi/blog/515051