Keepalived实现Redis双机高可用

文章标题:采用Keepalived实现Redis双机高可用
1. 相关软件
关闭防火墙[root@localhost ~]# service iptables stop
[root@localhost soft]# uname -a
Linux localhost.localdomain 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
2. 准备

    主(master):  192.168.88.146
    备份(backup): 192.168.88.147
    实现目标: 主服务宕机或服务停止,系统可以继续对外提供服务,实现高可用,同时数据不丢失
    为了实现高可用,采用高可用软件keepalived实现,所以 对外服务的ip地址使用keepalived服务产生的虚拟IP
    假设虚拟ip为: 192.168.88.22
    就是说对外服务的ip地址是192.168.88.22

3.  搭建步骤:

      1. 安装redis
    
    wget http://redis.googlecode.com/files/redis-2.0.0-rc4.tar.gz
[root@localhost soft]# tar zxvf redis-2.0.0-rc4.tar.gz
[root@localhost soft]# cd redis-2.0.0-rc4
[root@localhost redis-2.0.0-rc4]# make
[root@localhost redis-2.0.0-rc4]# cp redis.conf /etc/  ###注意修改配置daemonize yes
[root@localhost redis-2.0.0-rc4]# cp redis-benchmark redis-cli redis-server /usr/bin/
[root@localhost redis-2.0.0-rc4]# redis-server /etc/redis.conf########slave要配置slaveof  masterip port
[root@localhost redis-2.0.0-rc4]# redis-cli
redis>



      2. 安装高可用软件 keepalived
wget http://www.keepalived.org/software/keepalived-1.1.20.tar.gz
ot@localhost soft]# tar zxvf keepalived-1.1.20.tar.gz
[root@localhost soft]# cd keepalived-1.1.20
[root@localhost keepalived-1.1.20]# ./configure --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-x86_64/
[root@localhost keepalived-1.1.20]# make && make install
cp /usr/local/etc/rc.d/init.d/keepalived  /etc/rc.d/init.d/
[root@localhost keepalived-1.1.20]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@localhost keepalived-1.1.20]#mkdir /etc/keepalived

[root@localhost scripts]# ls #########https://github.com/paohaijiao/keepalive-redis
redis_backup.sh  redis_check_2.sh  redis_fault.sh  redis_master.sh  test.sh
redis_check1.sh  redis_check.sh    redis_jc.sh     redis_stop.sh
[root@localhost keepalived-1.1.20]#cp /usr/local/etc/keepalived/keepalived.conf  /etc/keepalived/
    
    vrrp_script chk_redis {
                script "/etc/keepalived/scripts/redis_check.sh"   ###监控脚本
                interval 2                                        ###监控时间
}
vrrp_instance VI_1 {
        state MASTER                            ###设置为MASTER
        interface eth1                          ###监控网卡
        virtual_router_id 51
         nopreempt #####从不设置
        priority 101                            ###权重值
        authentication {
                     auth_type PASS             ###加密
                     auth_pass redis            ###密码
        }
        track_script {
               chk_redis                       ###执行上面定义的chk_redis
        }
        virtual_ipaddress {
             192.168.88.22                        ###VIP
        }
        notify_master /etc/keepalived/scripts/redis_master.sh #################$REDISCLI 对端的ip  >>
        notify_backup /etc/keepalived/scripts/redis_backup.sh #################$REDISCLI 对端的ip  >>
        notify_fault  /etc/keepalived/scripts/redis_fault.sh
        notify_stop   /etc/keepalived/scripts/redis_stop.sh
}


    cp /usr/local/sbin/keepalived  /usr/sbin
    /etc/init.d/keepalived start

         

4. 关于配置文件:

   redis: 注意配置主备服务器都为master.
       配置文件路径:/etc/redis.conf
      keepalived:

    配置文件路径: /etc/keepalived/keepalived.conf


5. 相关的命令:


keepalived相关命令
/etc/init.d/keepalived start

service keepalived status
service keepalived stop
service keepalived start
redis-cli shutdown  
quit:关闭连接(connection)


redis-cli -h 192.168.88.22
redis-cli -h 192.168.88.146
redis-cli -h 192.168.88.147

chmod +x /etc/keepalived/scripts/*.sh

redis-server /etc/redis.conf
redis-cli

dos2unix test.sh
dos2unix *.sh
ps -ef | grep redis

ip add


tailf /var/log/keepalived-redis-state.log


6. 辅助shell脚本

    
         /etc/keepalived/scripts/redis_master.sh
         /etc/keepalived/scripts/redis_backup.sh
         /etc/keepalived/scripts/redis_fault.sh
         /etc/keepalived/scripts/redis_stop.sh
        
        
  

7. 测试流程


脚本创建完成以后,我们开始按照如下流程进行测试:

1.启动Master上的Redis

 /etc/init.d/redis start

2.启动Slave上的Redis

 /etc/init.d/redis start

3.启动Master上的Keepalived

 /etc/init.d/keepalived start

4.启动Slave上的Keepalived
  /etc/init.d/keepalived start

5.尝试通过VIP连接Redis:

 redis-cli -h 192.168.88.22

连接成功,Slave也连接上来了。

6.尝试插入一些数据:
 redis-cli -h 192.168.88.146 SET Hello Redis
 OK

从VIP读取数据
 redis-cli -h 192.168.88.22 GET Hello

"Redis"

从Master读取数据
 redis-cli -h 192.168.88.146 GET Hello

"Redis"

从Slave读取数据

  redis-cli -h 192.168.88.147 GET Hello
"Redis"

下面,模拟故障产生:
将Master上的Redis进程杀死:

 killall -9 redis-server

查看Master上的Keepalived日志
 tailf /var/log/keepalived-redis-state.log
[fault]
Thu Sep 27 08:29:01 CST 2012

同时Slave上的日志显示:
 tailf /var/log/keepalived-redis-state.log
[master]
Fri Sep 28 14:14:09 CST 2012
Being master....
Run SLAVEOF cmd ...
OK
Run SLAVEOF NO ONE cmd ...
OK

然后我们可以发现,Slave已经接管服务,并且担任Master的角色了。
 redis-cli -h 192.168.132.100 INFO
 redis-cli -h 192.168.132.128 INFO
 role:master

然后我们恢复Master的Redis进程
 /etc/init.d/redis start

查看Master上的Keepalived日志
 tailf /var/log/keepalived-redis-state.log
[master]
Thu Sep 27 08:31:33 CST 2012
Being master....
Run SLAVEOF cmd ...
OK
Run SLAVEOF NO ONE cmd ...
OK

同时Slave上的日志显示:
 tailf /var/log/keepalived-redis-state.log
[backup]
Fri Sep 28 14:16:37 CST 2012
Being slave....
Run SLAVEOF cmd ...
OK

可以发现目前的Master已经再次恢复了Master的角色,故障切换以及自动恢复都成功了。


9.  java版本测试代码


package com.yanek.redis;

import redis.clients.jedis.Jedis;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        
          Jedis jedis = new Jedis("192.168.132.100");
          jedis.set("id", "10000");
          String id = jedis.get("id");
          jedis.set("name", "javaboy2012");
          String name = jedis.get("name");
          System.out.println("id="+id);
          System.out.println("name="+name);
          System.out.println("name="+jedis.get("ray"));
    }

}



10. 可能遇到相关问题和处理办法:

  1. 如果两机器不能连通,请注意关掉防火墙: service iptables stop

    2. windows上编写的shell脚本,要注意通过dos2unix 命令转换,否则shell脚本可能有不可见符号不能正常执行。


7. 参考文档

  http://heylinux.com/archives/1942.html

      
 


   
文章标题:采用Keepalived实现Redis双机高可用
1. 相关软件
关闭防火墙[root@localhost ~]# service iptables stop
[root@localhost soft]# uname -a
Linux localhost.localdomain 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
2. 准备

    主(master):  192.168.88.146
    备份(backup): 192.168.88.147
    实现目标: 主服务宕机或服务停止,系统可以继续对外提供服务,实现高可用,同时数据不丢失
    为了实现高可用,采用高可用软件keepalived实现,所以 对外服务的ip地址使用keepalived服务产生的虚拟IP
    假设虚拟ip为: 192.168.88.22
    就是说对外服务的ip地址是192.168.88.22

3.  搭建步骤:

      1. 安装redis
    
    wget http://redis.googlecode.com/files/redis-2.0.0-rc4.tar.gz
[root@localhost soft]# tar zxvf redis-2.0.0-rc4.tar.gz
[root@localhost soft]# cd redis-2.0.0-rc4
[root@localhost redis-2.0.0-rc4]# make
[root@localhost redis-2.0.0-rc4]# cp redis.conf /etc/  ###注意修改配置daemonize yes
[root@localhost redis-2.0.0-rc4]# cp redis-benchmark redis-cli redis-server /usr/bin/
[root@localhost redis-2.0.0-rc4]# redis-server /etc/redis.conf########slave要配置slaveof  masterip port
[root@localhost redis-2.0.0-rc4]# redis-cli
redis>



      2. 安装高可用软件 keepalived
wget http://www.keepalived.org/software/keepalived-1.1.20.tar.gz
ot@localhost soft]# tar zxvf keepalived-1.1.20.tar.gz
[root@localhost soft]# cd keepalived-1.1.20
[root@localhost keepalived-1.1.20]# ./configure --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-x86_64/
[root@localhost keepalived-1.1.20]# make && make install
cp /usr/local/etc/rc.d/init.d/keepalived  /etc/rc.d/init.d/
[root@localhost keepalived-1.1.20]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@localhost keepalived-1.1.20]#mkdir /etc/keepalived

[root@localhost scripts]# ls #########https://github.com/paohaijiao/keepalive-redis
redis_backup.sh  redis_check_2.sh  redis_fault.sh  redis_master.sh  test.sh
redis_check1.sh  redis_check.sh    redis_jc.sh     redis_stop.sh
[root@localhost keepalived-1.1.20]#cp /usr/local/etc/keepalived/keepalived.conf  /etc/keepalived/
    
    vrrp_script chk_redis {
                script "/etc/keepalived/scripts/redis_check.sh"   ###监控脚本
                interval 2                                        ###监控时间
}
vrrp_instance VI_1 {
        state MASTER                            ###设置为MASTER
        interface eth1                          ###监控网卡
        virtual_router_id 51
         nopreempt #####从不设置
        priority 101                            ###权重值
        authentication {
                     auth_type PASS             ###加密
                     auth_pass redis            ###密码
        }
        track_script {
               chk_redis                       ###执行上面定义的chk_redis
        }
        virtual_ipaddress {
             192.168.88.22                        ###VIP
        }
        notify_master /etc/keepalived/scripts/redis_master.sh #################$REDISCLI 对端的ip  >>
        notify_backup /etc/keepalived/scripts/redis_backup.sh #################$REDISCLI 对端的ip  >>
        notify_fault  /etc/keepalived/scripts/redis_fault.sh
        notify_stop   /etc/keepalived/scripts/redis_stop.sh
}


    cp /usr/local/sbin/keepalived  /usr/sbin
    /etc/init.d/keepalived start

         

4. 关于配置文件:

   redis: 注意配置主备服务器都为master.
       配置文件路径:/etc/redis.conf
      keepalived:

    配置文件路径: /etc/keepalived/keepalived.conf


5. 相关的命令:


keepalived相关命令
/etc/init.d/keepalived start

service keepalived status
service keepalived stop
service keepalived start
redis-cli shutdown  
quit:关闭连接(connection)


redis-cli -h 192.168.88.22
redis-cli -h 192.168.88.146
redis-cli -h 192.168.88.147

chmod +x /etc/keepalived/scripts/*.sh

redis-server /etc/redis.conf
redis-cli

dos2unix test.sh
dos2unix *.sh
ps -ef | grep redis

ip add


tailf /var/log/keepalived-redis-state.log


6. 辅助shell脚本

    
         /etc/keepalived/scripts/redis_master.sh
         /etc/keepalived/scripts/redis_backup.sh
         /etc/keepalived/scripts/redis_fault.sh
         /etc/keepalived/scripts/redis_stop.sh
        
        
  

7. 测试流程


脚本创建完成以后,我们开始按照如下流程进行测试:

1.启动Master上的Redis

 /etc/init.d/redis start

2.启动Slave上的Redis

 /etc/init.d/redis start

3.启动Master上的Keepalived

 /etc/init.d/keepalived start

4.启动Slave上的Keepalived
  /etc/init.d/keepalived start

5.尝试通过VIP连接Redis:

 redis-cli -h 192.168.88.22

连接成功,Slave也连接上来了。

6.尝试插入一些数据:
 redis-cli -h 192.168.88.146 SET Hello Redis
 OK

从VIP读取数据
 redis-cli -h 192.168.88.22 GET Hello

"Redis"

从Master读取数据
 redis-cli -h 192.168.88.146 GET Hello

"Redis"

从Slave读取数据

  redis-cli -h 192.168.88.147 GET Hello
"Redis"

下面,模拟故障产生:
将Master上的Redis进程杀死:

 killall -9 redis-server

查看Master上的Keepalived日志
 tailf /var/log/keepalived-redis-state.log
[fault]
Thu Sep 27 08:29:01 CST 2012

同时Slave上的日志显示:
 tailf /var/log/keepalived-redis-state.log
[master]
Fri Sep 28 14:14:09 CST 2012
Being master....
Run SLAVEOF cmd ...
OK
Run SLAVEOF NO ONE cmd ...
OK

然后我们可以发现,Slave已经接管服务,并且担任Master的角色了。
 redis-cli -h 192.168.132.100 INFO
 redis-cli -h 192.168.132.128 INFO
 role:master

然后我们恢复Master的Redis进程
 /etc/init.d/redis start

查看Master上的Keepalived日志
 tailf /var/log/keepalived-redis-state.log
[master]
Thu Sep 27 08:31:33 CST 2012
Being master....
Run SLAVEOF cmd ...
OK
Run SLAVEOF NO ONE cmd ...
OK

同时Slave上的日志显示:
 tailf /var/log/keepalived-redis-state.log
[backup]
Fri Sep 28 14:16:37 CST 2012
Being slave....
Run SLAVEOF cmd ...
OK

可以发现目前的Master已经再次恢复了Master的角色,故障切换以及自动恢复都成功了。


9.  java版本测试代码


package com.yanek.redis;

import redis.clients.jedis.Jedis;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        
          Jedis jedis = new Jedis("192.168.132.100");
          jedis.set("id", "10000");
          String id = jedis.get("id");
          jedis.set("name", "javaboy2012");
          String name = jedis.get("name");
          System.out.println("id="+id);
          System.out.println("name="+name);
          System.out.println("name="+jedis.get("ray"));
    }

}



10. 可能遇到相关问题和处理办法:

  1. 如果两机器不能连通,请注意关掉防火墙: service iptables stop

    2. windows上编写的shell脚本,要注意通过dos2unix 命令转换,否则shell脚本可能有不可见符号不能正常执行。



      
 


   

你可能感兴趣的:(Keepalived实现Redis双机高可用)