1. 相关软件
redis: 开源高性能键值存储nosql
keepalived: 开源高可用软件
2. 测试环境
redis服务器两台,采用虚拟机来测试
操作系统是: centos5.4
主(master): 192.168.136.128
备份(backup): 192.168.136.129
实现目标: 主服务宕机或服务停止,系统可以继续对外提供服务,实现高可用,同时数据不丢失
为了实现高可用,采用高可用软件keepalived实现,所以 对外服务的ip地址使用keepalived服务产生的虚拟IP
假设虚拟ip为: 192.168.136.100
就是说对外服务的ip地址是192.168.136.100
3. 搭建步骤:
1. 安装redis
wget http://redis.googlecode.com/files/redis-2.0.0-rc4.tar.gz
tar zxvf redis-2.0.0-rc4.tar.gz
cd redis-2.0.0-rc4
make
cp redis.conf /etc/ 这个文件是redis启动的配置文件
cp redis-benchmark redis-cli redis-server /usr/bin/
redis-server /etc/redis.conf
redis-cli
注意修改配置:daemonize yes
2. 安装高可用软件 keepalived
wget http://www.keepalived.org/software/keepalived-1.1.20.tar.gz
tar zxvf keepalived-1.1.20.tar.gz
cd keepalived-1.1.20
./configure --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-x86_64/
make && make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
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.136.100 info
redis-cli -h 192.168.136.128 info
redis-cli -h 192.168.136.129 info
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.132.100 INFO
连接成功,Slave也连接上来了。
role:master
slave0:192.168.132.129,6379,online
6.尝试插入一些数据:
redis-cli -h 192.168.132.100 SET Hello Redis
OK
从VIP读取数据
redis-cli -h 192.168.132.100 GET Hello
"Redis"
从Master读取数据
redis-cli -h 192.168.132.128 GET Hello
"Redis"
从Slave读取数据
redis-cli -h 192.168.132.129 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://download.csdn.net/detail/5iasp/4979240