1.环境
centos:6.5
redis:2.8.17
redis主:10.15.151.8
redis从:10.15.151.9
keepalive vip:10.15.151.10
实现切换逻辑如下:A B两台机器
1. A 、B 依次启动,A作为主、B为从
2.主A 挂掉,B接管业务,作为主
3.A 起来,作为从SLAVEOF B
4.B 挂掉,A 切回主
将一台全部作为主,即可实现主从,可做读写分离
2.下载redis
可以从这里下载合适的redis版本:http://download.redis.io/releases/
3.安装
useradd redis
su - redis
tar -zxvf redis-2.8.17.tar.gz
cd redis-2.8.17
make
安装完成。如果在生产环境上可以接着执行以下命令进行验证:
make test
cd src && make test
make[1]: Entering directory `/home/redis/redis-2.8.17/src'
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] Error 1
make[1]: Leaving directory `/home/redis/redis-2.8.17/src'
make: *** [test] Error 2
这是因为缺少tcl包,需要安装
su -
yum install tcl -y
exit
make test
需要验证一会,最后显示:
All tests passed without errors!
验证成功。
4.配置
主:
vi /etc/redis.conf
daemonize yes
pidfile "/home/redis/redis.pid"
logfile /home/redis/redis.log
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
#logfile ""
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/home/redis"
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 100
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
# Generated by CONFIG REWRITE
从:
vi /etc/redis.conf
daemonize yes
pidfile "/home/redis/redis.pid"
logfile /home/redis/redis.log
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
#logfile ""
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/home/redis"
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 100
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
# Generated by CONFIG REWRITE
slaveof 10.15.151.8 6379
5.启动:
主从都用同样的命令启动:
./redis-2.8.17/src/redis-server /etc/redis.conf
6.验证
./redis-2.8.17/src/redis-cli
7.安装keepalived
yum install keepalived
8.配置keepalived
主:
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id redis
}
vrrp_script chk_redis
{
script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379"
interval 2
timeout 2
fall 3
}
vrrp_instance redis {
state BACKUP # 主也配置为SLAVE
interface wlan0}
virtual_router_id 50
priority 150
nopreempt # 不抢占,注意加上
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.15.151.10
}
track_script {
chk_redis
}
notify_master "/etc/keepalived/scripts/redis_master_slave.sh 127.0.0.1 10.15.151.8 6379"
notify_backup "/etc/keepalived/scripts/redis_backup_slave.sh 127.0.0.1 10.15.151.8 6379"
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
}
从:
! Configuration File for keepalived
global_defs {
router_id redis
}
vrrp_script chk_redis
{
script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379"
interval 2
timeout 2
fall 3
}
vrrp_instance redis {
state BACKUP
interface eth0
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.15.151.10
}
track_script {
chk_redis
}
notify_master "/etc/keepalived/scripts/redis_master_slave.sh 127.0.0.1 10.15.151.9 6379"
notify_backup "/etc/keepalived/scripts/redis_backup_slave.sh 127.0.0.1 10.15.151.9 6379"
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
}
9.编写各脚本
vi /etc/keepalived/scripts/redis_master_slave.sh
#!/bin/bash
REDISCLI="/home/redis/redis-2.8.17/src/redis-cli -h $1 -p $3"
LOGFILE="/home/redis/redis-2.8.17/logs/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF NO ONE cmd ... " >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
echo "SLAVEOF $2 cmd can't excute ... " >> $LOGFILE
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
vi /etc/keepalived/scripts/redis_backup_slave.sh
#!/bin/bash
REDISCLI="/home/redis/redis-2.8.17/src/redis-cli -h $1 -p $3"
LOGFILE="/home/redis/redis-2.8.17/logs/keepalived-redis-state.log"
echo "[BACKUP]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE 2>&1
$REDISCLI SLAVEOF $2 $3 >> $LOGFILE
sleep 100 #延迟10秒以后待数据同步完成后再取消同步状态
exit(0)
vi /etc/keepalived/scripts/redis_fault.sh
#!/bin/bash
LOGFILE=/home/redis/redis-2.8.17/logs/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
vi /etc/keepalived/scripts/redis_stop.sh
#!/bin/bash
LOGFILE=/home/redis/redis-2.8.17/logs/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
vi /etc/keepalived/scripts/redis_check.sh
#!/bin/bash
ALIVE=`/home/redis/redis-2.8.17/src/redis-cli -h $1 -p $2 PING`
LOGFILE="/home/redis/redis-2.8.17/logs/keepalived-redis-check.log"
echo "[CHECK]" >> $LOGFILE
date >> $LOGFILE
if [ $ALIVE == "PONG" ]; then :
echo "Success: redis-cli -h $1 -p $2 PING $ALIVE" >> $LOGFILE 2>&1
exit 0
else
echo "Failed:redis-cli -h $1 -p $2 PING $ALIVE " >> $LOGFILE 2>&1
exit 1
fi
给脚本都加上可执行权限:
(这点很重要,最开始由于这不没做,运行后一直报错 "VRRP_Instance(redis) Now in FAULT state")
$ sudo chmod +x /etc/keepalived/scripts/*.sh
10.启动keepalive
#keepalived -D
日志在syslog,有什么问题可以看日志:
tail -f /var/log/message
11.验证
使用vip登录redis:
./redis-2.8.17/src/redis-cli -h 10.15.151.10 info
停掉主redis:
./redis-2.8.17/src/redis-cli -h 10.15.151.8
10.15.151.8:6379> shutdown
./redis-2.8.17/src/redis-cli -h 10.15.151.10 info 依然可用。
487

被折叠的 条评论
为什么被折叠?



