文档章节

Keepalived实现Redis双机高可用

泡海椒
 泡海椒
发布于 2016/04/09 10:37
字数 2193
阅读 100
收藏 8

文章标题:采用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脚本可能有不可见符号不能正常执行。



      
 


   

本文转载自: http://heylinux.com/archives/1942.html

上一篇: scala函数式编程
下一篇: Memory Analyzer安装
泡海椒
粉丝 11
博文 282
码字总数 288344
作品 0
成都
程序员
私信 提问
使用keepalived打造redis双机热备

redis是非常实用的优秀nosql开源产品,在高并发的服务器环境中,可以作为缓存和队列使用.美中不足的是,到现在为止,redis并没有推出自己的集群方案,对一个完善的生产环境,负载均衡,高可用...

凯文加内特
2015/10/10
589
0
采用Keepalived实现Redis双机高可用

相关软件 redis: 开源高性能键值存储nosql keepalived: 开源高可用软件 2. 测试环境 redis服务器两台,采用虚拟机来测试 操作系统是: centos5.4 主(master): 192.168.136.128 备份(backup...

泡海椒
2016/04/04
35
0
postgresql 高可用集群搭建资料

多种方案,含zookeeper方案 :PostgreSQL 流行 HA 方案 1. 自己实现高可用集群 有实例 有讲解!postgresql 高可用集群搭建 :PostgreSQLHAwithprimarystandby_2vip 2. pgpool 实现高可用集群...

刘阳0292
2017/10/12
0
0
服务器集群之keepalived配置高可用(资料)

Linux集群概述 • 根据功能划分为两大类:高可用和负载均衡 • 高可用集群通常为两台服务器,一台工作,另外一台作为冗余,当提供服务的机器宕机,冗余将接替继续提供服务 • 实现高可用的开...

寰宇01
2018/05/24
279
0
lvs+keepalived 高可用负载均衡模式分析

1. 前言 在《keepalived 双机热备》这篇文章中,我写了利用keepalived 这个开源软件实现高可用的功能,以及keepalived 高可用所使用的协议——利用vrrp 协议,在高可用网络组内广播自己的优先...

hello_cjq
2018/03/30
0
0

没有更多内容

加载失败,请刷新页面

加载更多

【AngularJS学习笔记】02 小杂烩及学习总结

本文转载于:专业的前端网站☞【AngularJS学习笔记】02 小杂烩及学习总结 表格示例 <div ng-app="myApp" ng-controller="customersCtrl"> <table> <tr ng-repeat="x in names | orderBy ......

前端老手
昨天
13
0
Linux 内核的五大创新

在科技行业,创新这个词几乎和革命一样到处泛滥,所以很难将那些夸张的东西与真正令人振奋的东西区分开来。Linux内核被称为创新,但它又被称为现代计算中最大的奇迹,一个微观世界中的庞然大...

阮鹏
昨天
9
0
【Medium 万赞好文】ViewModel 和 LIveData:模式 + 反模式

原文作者: Jose Alcérreca 原文地址: ViewModels and LiveData: Patterns + AntiPatterns 译者:秉心说 View 和 ViewModel 分配责任 理想情况下,ViewModel 应该对 Android 世界一无所知。...

秉心说
昨天
15
0
重学计算机组成原理(十二) - 异常和中断

1 概览 完好的程序都满足以下特征 自动运行 我们的程序和指令都是一条条顺序执行,不需要通过键盘或者网络给这个程序任何输入 正常运行 没有遇到计算溢出之类的程序错误。 不过,现实的软件世...

JavaEdge
昨天
11
0
程序设计基础(C)第06讲例程

1summing.c /* summing.c -- 根据用户键入的整数求和 */#include <stdio.h>int main(void){ long num; long sum = 0L; /* 把sum 初始化为0 */ int status; p......

树人大学数字媒体吴凡
昨天
12
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部