zoukankan      html  css  js  c++  java
  • 测试redis+keepalived实现简单的主备切换【转载】

     

    转自: 

    测试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配置:

    Xml代码  
    1. ! Configuration File for keepalived  
    2.   
    3. global_defs {  
    4.    router_id LVS_REDIS_TEST_21  
    5. }  
    6.   
    7. vrrp_script chk_redis_1 {  
    8.     script "/home/cachedb/redis/scripts/redis_check.sh 10.15.34.21 38001"  
    9.     interval 10  
    10.     timeout 2  
    11.     fall 3  
    12. }  
    13. vrrp_script chk_redis_2 {  
    14.     script "/home/cachedb/redis/scripts/redis_check.sh 10.15.34.21 38002"  
    15.     interval 10  
    16.     timeout 2  
    17.     fall 3  
    18. }  
    19.   
    20.   
    21. vrrp_instance VI_1 {  
    22.     state BACKUP  
    23.     interface eth0  
    24.     virtual_router_id 40  
    25.     priority 150  
    26.     nopreempt  
    27.     advert_int 1  
    28.     authentication {  
    29.         auth_type PASS  
    30.         auth_pass 1111  
    31.     }  
    32.     track_script {  
    33.         chk_redis_1  
    34.     }  
    35.     virtual_ipaddress {  
    36.         10.15.34.23   
    37.     }  
    38.     notify_master "/home/cachedb/redis/scripts/redis_master.sh 10.15.34.21"  
    39.     notify_backup "/home/cachedb/redis/scripts/redis_backup.sh 10.15.34.21 38001 10.15.34.22 38001"  
    40.     notify_fault  "/home/cachedb/redis/scripts/redis_fault.sh"  
    41.     notify_stop   "/home/cachedb/redis/scripts/redis_stop.sh"  
    42. }  
    43.   
    44. vrrp_instance VI_2 {  
    45.     state BACKUP  
    46.     interface eth0  
    47.     virtual_router_id 41  
    48.     priority 150  
    49.     nopreempt  
    50.     advert_int 1  
    51.     authentication {  
    52.         auth_type PASS  
    53.         auth_pass 1111  
    54.     }  
    55.     track_script {  
    56.         chk_redis_2  
    57.     }  
    58.     virtual_ipaddress {  
    59.         10.15.34.54  
    60.     }  
    61.     notify_master "/home/cachedb/redis/scripts/redis_master.sh 10.15.34.21 38002"  
    62.     notify_backup "/home/cachedb/redis/scripts/redis_backup.sh 10.15.34.21 38002 10.15.34.22 38002"  
    63.     notify_fault  "/home/cachedb/redis/scripts/redis_fault.sh"  
    64.     notify_stop   "/home/cachedb/redis/scripts/redis_stop.sh"  
    65. }  

     2)备机的keepalived配置:

    Xml代码  
    1. ! Configuration File for keepalived  
    2.   
    3. global_defs {  
    4.    router_id LVS_REDIS_TEST  
    5. }  
    6.   
    7. vrrp_script chk_redis_1 {  
    8.     script "/home/cachedb/redis/scripts/redis_check.sh 10.15.34.22 38001"  
    9.     interval 10  
    10.     timeout 2  
    11.     fall 3  
    12. }  
    13. vrrp_script chk_redis_2 {  
    14.     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>  
    15.     interval 10  
    16.     timeout 2  
    17.     fall 3  
    18. }  
    19.   
    20.   
    21. vrrp_instance VI_1 {  
    22.     state BACKUP  
    23.     interface eth0  
    24.     virtual_router_id 40  
    25.     priority 100  
    26.     advert_int 1  
    27.     authentication {  
    28.         auth_type PASS  
    29.         auth_pass 1111  
    30.     }  
    31.     track_script {  
    32.         chk_redis_1  
    33.     }  
    34.     virtual_ipaddress {  
    35.         10.15.34.33   
    36.     }  
    37.     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>  
    38.     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>  
    39.     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>  
    40.     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>  
    41. }  
    42.   
    43. vrrp_instance VI_2 {  
    44.     state BACKUP  
    45.     interface eth0  
    46.     virtual_router_id 41  
    47.     priority 100  
    48.     advert_int 1  
    49.     authentication {  
    50.         auth_type PASS  
    51.         auth_pass 1111  
    52.     }  
    53.     track_script {  
    54.         chk_redis_2  
    55.     }  
    56.     virtual_ipaddress {  
    57.         10.15.34.34  
    58.     }  
    59.     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>  
    60.     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>  
    61.     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>  
    62.     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>  
    63. }  

     注意: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被漂移到主机上了。

  • 相关阅读:
    AtCoder Beginner Contest 167
    AtCoder Beginner Contest 166
    AtCoder Beginner Contest 165
    AtCoder Beginner Contest 164
    AtCoder Beginner Contest 163
    AtCoder Beginner Contest 162
    AtCoder Beginner Contest 161
    AtCoder Beginner Contest 160
    AtCoder Beginner Contest 159
    自定义Mybatis自动生成代码规则
  • 原文地址:https://www.cnblogs.com/paul8339/p/5694049.html
Copyright © 2011-2022 走看看