转自:
测试redis+keepalived实现简单的主备切换 - Try My Best 尽力而为 - ITeye技术网站
http://raising.iteye.com/blog/2311757
keepalived是一个是集群管理中保证集群高可用的一个服务软件,它具备心跳检测的功能。运用在redis上,则是具备主备切换的控制功能。
keepalived的主备切换方案的设计思想如下:
当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby;
当 Master 挂掉,Slave 正常时, Slave接管服务,同时关闭主从复制功能;
当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,于此同时Slave等待Master同步数据完成之后,恢复Slave身份。
然后依次循环。
下面就来大概讲下实现过程:
环境准备
主:10.15.34.21
备:10.15.34.22
VIP(虚拟IP):10.15.34.23,10.15.34.24
0.在主机和备机上分别创建用户部署redis和keepalived,如用户路径:/home/cachedb
1.在主机和备机上安装redis(过程略);
2.在主机和备机上安装keepalived(过程略);
3.修改主机和备机上的keepalived的配置文件keepalived.conf(可将原有的配置文件备份成keepalived.conf.bak)
1)主机的keepalived配置:
- ! Configuration File for keepalived
- global_defs {
- router_id LVS_REDIS_TEST_21
- }
- vrrp_script chk_redis_1 {
- script "/home/cachedb/redis/scripts/redis_check.sh 10.15.34.21 38001"
- interval 10
- timeout 2
- fall 3
- }
- vrrp_script chk_redis_2 {
- script "/home/cachedb/redis/scripts/redis_check.sh 10.15.34.21 38002"
- interval 10
- timeout 2
- fall 3
- }
- vrrp_instance VI_1 {
- state BACKUP
- interface eth0
- virtual_router_id 40
- priority 150
- nopreempt
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- track_script {
- chk_redis_1
- }
- virtual_ipaddress {
- 10.15.34.23
- }
- notify_master "/home/cachedb/redis/scripts/redis_master.sh 10.15.34.21"
- notify_backup "/home/cachedb/redis/scripts/redis_backup.sh 10.15.34.21 38001 10.15.34.22 38001"
- notify_fault "/home/cachedb/redis/scripts/redis_fault.sh"
- notify_stop "/home/cachedb/redis/scripts/redis_stop.sh"
- }
- vrrp_instance VI_2 {
- state BACKUP
- interface eth0
- virtual_router_id 41
- priority 150
- nopreempt
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- track_script {
- chk_redis_2
- }
- virtual_ipaddress {
- 10.15.34.54
- }
- notify_master "/home/cachedb/redis/scripts/redis_master.sh 10.15.34.21 38002"
- notify_backup "/home/cachedb/redis/scripts/redis_backup.sh 10.15.34.21 38002 10.15.34.22 38002"
- notify_fault "/home/cachedb/redis/scripts/redis_fault.sh"
- notify_stop "/home/cachedb/redis/scripts/redis_stop.sh"
- }
2)备机的keepalived配置:
- ! Configuration File for keepalived
- global_defs {
- router_id LVS_REDIS_TEST
- }
- vrrp_script chk_redis_1 {
- script "/home/cachedb/redis/scripts/redis_check.sh 10.15.34.22 38001"
- interval 10
- timeout 2
- fall 3
- }
- vrrp_script chk_redis_2 {
- script "/home/<span style="line-height: 1.5;">cachedb</span><span style="font-size: 1em; line-height: 1.5;">/redis/scripts/redis_check.sh 10.15.34.22 38002"</span>
- interval 10
- timeout 2
- fall 3
- }
- vrrp_instance VI_1 {
- state BACKUP
- interface eth0
- virtual_router_id 40
- priority 100
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- track_script {
- chk_redis_1
- }
- virtual_ipaddress {
- 10.15.34.33
- }
- notify_master "/home/<span style="line-height: 1.5;">cachedb</span><span style="font-size: 1em; line-height: 1.5;">/redis/scripts/redis_master.sh 10.15.34.22 38001"</span>
- notify_backup "/home/<span style="line-height: 1.5;">cachedb</span><span style="font-size: 1em; line-height: 1.5;">/redis/scripts/redis_backup.sh 10.15.34.22 38001 10.15.34.21 38001"</span>
- notify_fault "/home/<span style="line-height: 1.5;">cachedb</span><span style="font-size: 1em; line-height: 1.5;">/redis/scripts/redis_fault.sh"</span>
- notify_stop "/home/<span style="line-height: 1.5;">cachedb</span><span style="font-size: 1em; line-height: 1.5;">/redis/scripts/redis_stop.sh"</span>
- }
- vrrp_instance VI_2 {
- state BACKUP
- interface eth0
- virtual_router_id 41
- priority 100
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- track_script {
- chk_redis_2
- }
- virtual_ipaddress {
- 10.15.34.34
- }
- notify_master "/home/<span style="line-height: 1.5;">cachedb</span><span style="font-size: 1em; line-height: 1.5;">/redis/scripts/redis_master.sh 10.15.34.22 38002"</span>
- notify_backup "/home/<span style="line-height: 1.5;">cachedb</span><span style="font-size: 1em; line-height: 1.5;">/redis/scripts/redis_backup.sh 10.15.34.22 38002 10.15.34.21 38002"</span>
- notify_fault "/home/<span style="line-height: 1.5;">cachedb</span><span style="font-size: 1em; line-height: 1.5;">/redis/scripts/redis_fault.sh"</span>
- notify_stop "/home/<span style="line-height: 1.5;">cachedb</span><span style="font-size: 1em; line-height: 1.5;">/redis/scripts/redis_stop.sh"</span>
- }
注意:a. 备机的keepalived配置的priority要比主机的低; b. 备机不能有nopreempt属性;
c. virtual_router_id 这个主备配置的要对应一致,并且在局域网要唯一,注意使用默认的ID如51时要check改ID有木有被占用,若有就换一个。
配置完以后:
<!--[if !supportLists]-->1)<!--[endif]-->启动主机的redis服务,再在root下启动keepalived服务;
<!--[if !supportLists]-->2)<!--[endif]-->启动备机的redis服务,再在root下启动keepalived服务;
<!--[if !supportLists]-->3)<!--[endif]-->查看主机的角色:
./redis-cli -h 10.15.34.21 -p 38001
查看备机的角色:
./redis-cli -h 10.15.34.22 -p 38001
查看主机的redis-state的log日志:(Log日志的位置由redis_*.sh脚本文件里指定)
再查看备机的redis-state的log日志:
4)尝试kill掉主机的redis服务,再在备机上查看role角色,得到:
可以看出它变成master了,而与此同时,它的redis-state.log日志如下:
1)我们不妨在备机上用root用户查看VIP的漂移情况:(虚拟IP的漂移需要一小段时间,不是立刻~)
ip a
在备机上可以发现虚拟IP的漂移情况。
<!--[if !supportLists]-->1)<!--[endif]-->而如果再重启之前master上的redis服务,会发现它的角色是slave了,这说明,master和salve发生了角色互换。
<!--[if !supportLists]-->2)<!--[endif]-->可以再尝试把备机上的redis服务kill掉,会发现主机上的角色会恢复成master;可以在主机上使用root用户来运行 ip a命令,会发现虚拟IP被漂移到主机上了。