Redis简介:
Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。从2010年3月15日起,Redis的开发工作由VMware主持。
redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set –有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。
设计思路:
redis 主和 redis从 正常同步;
redis主keepalived 启动,执行redis_master.sh脚本,vip在自己身上;
redis 从keepalived 启动,执行redis_slave.sh脚本;
redis主服务关闭,则redis主执行redis_fault.sh脚本, vip落在redis 从上,redis从执行redis_master.sh脚本,变成主;
redis主服务开启,则redis主执行redis_slave.sh脚本变成从,vip还在redis 从上,还是主;
redis从服务关闭,则redis从执行redis_fault.sh脚本, vip落在redis主上,redis主执行redis_master.sh脚本,变成主;
redis从服务开启,则redis从执行redis_slave.sh脚本变成从, vip还在redis主上,redis主继续做主,此时为redis初试主从状态;
环境:
192.168.1.100 redis
192.168.1.101 redis-slave
192.168.1.111 VIP
一、安装keepalived
(redis和redis-slave两台服务器都安装)
yum install ipvsadm -y
yum install gcc kernel-devel openssl-devel popt-devel libnl-devel -y
tar xf keepalived-1.2.7.tar.gz
cd keepalived-1.2.7
./configure --prefix=/ --with-kernel-
dir=/usr/src/kernels/2.6.32-279.el6.i686/&&make&&make install
chkconfig --add keepalive
chkconfig --add keepalived
chkconfig keepalived on
二、安装redis
(redis和redis-slave两台服务器都安装)
wget http://download.redis.io/releases/redis-2.8.13.tar.gz
tar xf redis-2.8.13.tar.gz
cd redis-2.8.13
make
make install
cd src && make test
错误解决:
wget http://downloads.sourceforge.net/tcl/tcl8.6.4-src.tar.gz
mv tcl8.6.4-src.tar.gz /opt/
cd /opt/
tar xf tcl8.6.4-src.tar.gz
cd tcl8.6.4
cd unix/
./configure
make&&make install
cd redis-2.8.13
make test
redis-slve只需注意一点
[[email protected] bin]# pwd
/usr/local/bin
[[email protected] bin]# vim redis.conf
116 slaveof 192.168.1.100 6379
三、通过Keepalived实现Redis Failover自动故障切换
修改Master和Slave的/etc/hosts文件
[[email protected] rc.d]# cat /etc/hosts
192.168.1.100 redis
192.168.1.101 redis-slave
Master上是
[[email protected] rc.d]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=redis
Slave上是
[[email protected] linux]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=redis-slave
修改完主机名重启机器生效
默认安装完成keepalived之后是没有配置文件的,因此我们需要手动创建:
首先,在Master上创建如下配置文件:
[[email protected] linux]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
Y[email protected]
}
notification_email_from [email protected]
router_id redis
}
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"
interval 1
}
vrrp_instance mes_Redis {
state MASTER
interface eth0
garp_master_delay 10
smtp_alert
virtual_router_id 3
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.111
}
track_script {
chk_redis
}
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_slave.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
}
然后,在Slave上创建如下配置文件:
[[email protected] linux]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
Y[email protected]
}
notification_email_from [email protected]
router_id redis-salve
}
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"
interval 1
}
vrrp_instance mes_Redis {
state BACKUP
interface eth0
garp_master_delay 10
smtp_alert
virtual_router_id 3
priority 90
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.111
}
track_script {
chk_redis
}
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_slave.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
}
在Master和Slave上创建监控Redis的脚本
mkdir /etc/keepalived/scripts
vim /etc/keepalived/scripts/redis_check.sh
#!/bin/bash
ALIVE=`/usr/local/bin/redis-cli PING`
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE
exit 0
else
echo $ALIVE
exit 1
fi
编写以下负责运作的关键脚本:
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_slave.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
因为Keepalived在转换状态时会依照状态来呼叫:
当进入Master状态时会呼叫notify_master
当进入Backup状态时会呼叫notify_backup
当发现异常情况时进入Fault状态呼叫notify_fault
当Keepalived程序终止时则呼叫notify_stop
首先,在Redis Master上创建notity_master与notify_backup脚本:
vim /etc/keepalived/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 192.168.1.101 6379 >> $LOGFILE 2>&1
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
vim /etc/keepalived/scripts/redis_slave.sh
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 192.168.1.101 6379 >> $LOGFILE 2>&1
接着,在Redis Slave上创建notity_master与notify_backup脚本:
vim /etc/keepalived/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 192.168.1.100 6379 >> $LOGFILE 2>&1
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
vim /etc/keepalived/scripts/redis_slave.sh
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 192.168.1.100 6379 >> $LOGFILE 2>&1
然后在Master与Slave创建如下相同的脚本:
vim /etc/keepalived/scripts/redis_fault.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
vim /etc/keepalived/scripts/redis_stop.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
在Master与Slave给脚本都加上可执行权限:
chmod +x /etc/keepalived/scripts/*.sh
四、脚本创建完成以后,我们开始按照如下流程进行测试:
1.启动Master上的Redis
[[email protected] bin]# pwd
/usr/local/bin
[[email protected] bin]# ./redis-server redis.conf
2.启动Slave上的Redis
[[email protected] bin]# pwd
/usr/local/bin
[[email protected] bin]# ./redis-server redis.conf
3.启动Master上的Keepalived
/etc/init.d/keepalived start
4.启动Slave上的Keepalived
/etc/init.d/keepalived start
5.尝试通过VIP连接Redis:
[[email protected] bin]#pwd
/usr/local/bin
[[email protected] bin]# ./redis-cli -h 192.168.1.111 info
role:master
slave0:192.168.1.101,6379,online
连接成功,Slave也连接上来了
6.尝试插入一些数据:
[[email protected] bin]# ./redis-cli -h 192.168.1.111 SET Hello Redis
从VIP读取数据
[[email protected] bin]# ./redis-cli -h 192.168.1.111 GET Hello
"Redis"
从Master读取数据
[[email protected] bin]# ./redis-cli -h 192.168.100 GET Hello
"Redis"
从Slave读取数据
[[email protected] bin]# ./redis-cli -h 192.168.1.100 GET Hello
"Redis"
下面,模拟故障产生:
将Master上的Redis进程杀死:
[[email protected] bin]# ./redis-cli shutdown
查看Master上的Keepalived日志
[[email protected] scripts]# tail /var/log/keepalived-redis-state.log
[fault]
Thu Sep 27 08:29:01 CST 2012
同时Slave上的日志显示:
[[email protected] scripts]# tail /var/log/keepalived-redis-state.log
[master]
Thu Nov 15 12:06:04 CST 2012
Being master....
Run SLAVEOF cmd ...
OK
Run SLAVEOF NO ONE cmd ...
OK
然后我们可以发现,Slave已经接管服务,并且担任Master的角色了。
./redis-cli -h 192.168.1.111 info
./redis-cli -h 192.168.1.101 info
role:master
然后我们恢复Master的Redis进程
主变成slave
然后把101redis停掉
100恢复主的角色,在把101redis开启
恢复100是主,101是备
自动切换成功!
redis-cli -h 192.168.12.12 远程登录redis客户端
redis 127.0.0.1:6379> ping 测试连接是否存活
PONG
redis 127.0.0.1:6379> echo wilian 在命令行打印一些内容
"wilian"
redis 127.0.0.1:6379> select 1 选择数据库。Redis数据库编号从0~15,我们可以选择任意一个数据库来进行数据的存取
OK
redis 127.0.0.1:6379[1]> dbsize 返回当前数据库中key的数目。
(integer) 0
redis 127.0.0.1:6379[1]> select 0
OK
redis 127.0.0.1:6379> dbsize
(integer) 20
redis 127.0.0.1:6379> info 获取服务器的信息和统计
redis_version:2.4.5
redis_git_sha1:00000000
redis_git_dirty:0
arch_bits:32
multiplexing_api:winsock2
process_id:5956
uptime_in_seconds:100358
uptime_in_days:1
lru_clock:1957468
used_cpu_sys:4.70
used_cpu_user:4.99
used_cpu_sys_children:0.00
used_cpu_user_children:0.00
connected_clients:1
connected_slaves:0
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
used_memory:684452
used_memory_human:668.41K
used_memory_rss:684452
used_memory_peak:684424
used_memory_peak_human:668.38K
mem_fragmentation_ratio:1.00
mem_allocator:libc
loading:0
aof_enabled:0
changes_since_last_save:0
bgsave_in_progress:0
last_save_time:1340763023
bgrewriteaof_in_progress:0
total_connections_received:1
total_commands_processed:199
expired_keys:3
evicted_keys:0
keyspace_hits:163
keyspace_misses:35
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
vm_enabled:0
role:master
db0:keys=20,expires=0
redis 127.0.0.1:6379> monitor 实时转储收到的请求。
OK
1340780669.110663 "monitor"
redis 127.0.0.1:6379> config get dir 获取服务器配置信息
1) "dir"
2) "D:\\mysoft\\redis-2.0.2"
flushdb 删除当前选择数据库中的所有key
flushall 删除所有数据库中的所有key