1、测试环境信息
服务器 | IP |
---|---|
redis-master | 192.168.10.49 |
redis-slave | 192.168.10.50 |
VirtualIP | 192.168.10.51 |
2、安装依赖软件
$ yum install -y openssl openssl-devel kernel kernel-devel gcc wget tcl libnl-devel
3、安装keepalived
$ wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz
$ tar -zxvf keepalived-1.2.13.tar.gz
$ cd keepalived-1.2.13
$ ./configure --prefix=/usr/local/keepalived/ --with-kernel-dir=/usr/src/kernels/2.6.32-642.1.1.el6.x86_64 --sysconf=/etc
$ make && make install
##上面的内核参数,通过`uname -r`查看
$ service keepalived status
4、安装redis
$ tar -zxvf redis-2.8.17.tar.gz
$ cd redis-2.8.17
$ make && make test && make install
$ mkdir -p/usr/local/redis/{bin,var,log,data,etc}
$ cp ./src/{redis-benchmark,redis-cli,redis-server,redis-check-dump,redis-check-aof} /usr/local/redis/bin/
$ cp /usr/local/redis/bin/* /usr/local/bin/
5、安装 netcat
$ wget http://sourceforge.net/projects/netcat/files/netcat/0.7.1/netcat-0.7.1.tar.gz
$ tar -zxvf netcat-0.7.1.tar.gz
$ ./configure
$ make && make install
6、配置文件修改
6.1 Master配置
先配置好Redis,主从复制成功之后再行配置Keepalived。
- Master-Redis配置
daemonize yes
pidfile /var/run/redis.pid
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/usr/local/redis/log/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /usr/local/redis/data
slave-serve-stale-data yes
slave-read-only no
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
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
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
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
- Master-Keepalived配置
global_defs {
router_id redis1
##要与BACKUP不一致
}
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"
interval 2
timeout 2
fall 3
}
vrrp_instance redis {
state MASTER
interface eth0
virtual_router_id 52
##要与BACKUP一致
advert_int 1
priority 99
authentication {
auth_type PASS
auth_pass redis
}
track_script {
chk_redis
}
virtual_ipaddress {
192.168.10.51
}
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_backup.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redsi_stop.sh
##记着给脚本附权限
}
- Master-keepalived脚本redis_master.sh:
#!/bin/bash
REDISCLI="/usr/local/redis/bin/redis-cli"
LOGFILE="/usr/local/redis/var/keepalived-redis-state.log"
echo "[master]">> $LOGFILE
date >> $LOGFILE
echo "Being master....">> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ...">> $LOGFILE
$REDISCLI SLAVEOF 192.168.10.50 6379 >> $LOGFILE 2>&1
sleep 10
echo "Run SLAVEOF NO ONE cmd ...">> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
- Master-keepalived脚本redis_backup.sh:
#!/bin/bash
REDISCLI="/usr/local/redis/bin/redis-cli"
LOGFILE="/usr/local/redis/var/keepalived-redis-state.log"
echo "[backup]">> $LOGFILE
date >> $LOGFILE
echo "Being slave....">> $LOGFILE 2>&1
sleep 15
echo "Run SLAVEOF cmd ...">> $LOGFILE
$REDISCLI SLAVEOF 192.168.10.50 6379 >> $LOGFILE 2>&1
6.2 BACKUP配置
- Backup-Redis配置
$ vim /usr/local/redis/etc/redis.conf
daemonize yes
pidfile /var/run/redis.pid
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/usr/local/redis/log/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /usr/local/redis/data
slave-serve-stale-data yes
slave-read-only no
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
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
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
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
SLAVEOF 192.168.10.49 6379
- Backup-Keepalived配置
global_defs {
router_id redis
}
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"
interval 2
timeout 2
fall 3
}
vrrp_instance redis {
state BACKUP
interface eth0
virtual_router_id 52
advert_int 1
priority 50
authentication {
auth_type PASS
auth_pass redis
}
track_script {
chk_redis
}
virtual_ipaddress {
192.168.10.51
}
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_backup.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redsi_stop.sh
}
- BACKUP-keepalived脚本redis_master.sh:
#!/bin/bash
REDISCLI="/usr/local/redis/bin/redis-cli"
LOGFILE="/usr/local/redis/var/keepalived-redis-state.log"
echo "[master]">> $LOGFILE
date >> $LOGFILE
echo "Being master....">> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ...">> $LOGFILE
$REDISCLI SLAVEOF 192.168.10.49 6379 >> $LOGFILE 2>&1
sleep 10
echo "Run SLAVEOF NO ONE cmd ...">> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
- BACKUP-keepalived脚本redis_backup.sh:
#!/bin/bash
REDISCLI="/usr/local/redis/bin/redis-cli"
LOGFILE="/usr/local/redis/var/keepalived-redis-state.log"
echo "[backup]">> $LOGFILE
date >> $LOGFILE
echo "Being slave....">> $LOGFILE 2>&1
sleep 15
echo "Run SLAVEOF cmd ...">> $LOGFILE
$REDISCLI SLAVEOF 192.168.10.49 6379 >> $LOGFILE 2>&1
6.3 公共脚本设置
- redis_check.sh
$ vim /etc/keepalived/scripts/redis_check.sh
#!/bin/bash
ALIVE=`/usr/local/redis/bin/redis-cli PING`
if [ "$ALIVE" == "PONG" ] ;then
echo $ALIVE
exit 0
else
echo $ALIVE
exit 1
fi
- redis_fault.sh
$ vim /etc/keepalived/scripts/redis_fault.sh
echo "[faule]">> $LOGFILE
date >> $LOGFILE
- redis_stop.sh
$ vim /etc/keepalived/scripts/redis_stop.sh
echo "[stop]">> $LOGFILE
date >> $LOGFILE