redis+keepalived 高可用搭建实践
系统环境Centos6.3
拓扑图如下:
一、安装keepalived(每台都安装)
1.安装兼容包
# yum -y install openssl openssl-devel ipvsadm
2.下载并安装keepalived
# wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
# tar zxvf keepalived-1.2.7.tar.gz
# cd keepalived-12.7 && make && make install
3.redis_master keepalived配置
# 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/sbin/keepalived /usr/sbin/
# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
# chkconfig --add keepalived
# vi /etc/keepalived/keepalived.conf(redis_master 1)
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server smtp.126.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 52
priority 100 #redis_master 2 改为90
advert_int 1
nopreempt #不抢占ip,只在master 1上设置
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.240.89
}
}
4.redis_slave keepalived配置 (redis_slave 1)
# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server smtp.126.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER # redis_slave 2 改为 BACKUP
interface eth0
virtual_router_id 53
priority 100 #redis_slave 2 改为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.240.90
}
}
5.启动keepalived,并查看
# service keepalived start
[root@redis_master1 ~]# ip a|grep inet
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
inet 192.168.240.78/24 brd 192.168.240.255 scope global eth0
inet 192.168.240.89/32 scope global eth0
inet6 fe80::250:56ff:fe98:6a56/64 scope link
[root@redis_slave1 ~]# ip a|grep inet
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
inet 192.168.240.80/24 brd 192.168.240.255 scope global eth0
inet 192.168.240.90/32 scope global eth0
inet6 fe80::250:56ff:fe98:7c0f/64 scope link
二、安装redis(每台都安装)
1.下载并安装redis
# wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz
# tar zxvf redis-2.6.14.tar.gz
# cd redis-2.6.14
# make && make install
# mkdir -p /etc/redis/
2 配置redis_master
# vi /etc/redis/6501.conf (redis_master 1)
daemonize yes
port 6501
timeout 0
tcp-keepalive 0
loglevel notice
logfile /var/log/redis_6501.log
databases 8
save 900 1
save 300 10
save 60 10000
# slaveof 192.168.240.78 6501 这行在redis_master 2 上面添加
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis/6501
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
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
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
3.配置redis_slave (两台配置一样)
# vi /etc/redis/6501.conf
daemonize yes
pidfile /var/run/redis_6501.pid
port 6501
timeout 0
tcp-keepalive 0
loglevel notice
logfile /var/log/redis_6501.log
databases 8
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis/6501
slaveof 192.168.240.89 6501
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 100
maxclients 20000
appendonly no
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
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
4.编写redis启动脚本
# vi /etc/init.d/redis
#!/bin/sh
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig: - 85 15
# description: Redis is a persistent key-value database
# processname: redis-server
# config: /etc/redis.conf
# config: /etc/sysconfig/redis
# pidfile: /var/run/redis.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
redis="/usr/local/bin/redis-server"
prog=$(basename $redis)
REDIS_CONF_FILE="/etc/redis/6501.conf"
[ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis
lockfile=/var/lock/subsys/redis
start() {
[ -x $redis ] || exit 5
[ -f $REDIS_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $redis $REDIS_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
start
}
reload() {
echo -n $"Reloading $prog: "
killproc $redis -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
# chmod u+x /etc/init.d/redis
# chkconfig --add redis
# service redis start
5.编写redis_master1监控脚本
# vi check_Redis.sh (并在两台redis_slaves上面运行)
#!/bin/sh
while :
do
redis-cli -p 6501 info &> /dev/null
if [ $? -ne 0 ] ;then
sleep 2
redis-cli -p 6501 info &> /dev/null
if [ $? -ne 0 ] ;then
service keepalived stop
fi
fi
sleep 10
done
# nohup check_Redis.sh & #后台运行
6.编写redis_master 2 监控脚本
# vi check_Redis.sh
#!/bin/sh
while :
do
redis-cli -p 6501 info &> /dev/null
if [ $? -ne 0 ] ;then
sleep 2
redis-cli -p 6501 info &> /dev/null
if [ $? -ne 0 ] ;then
service keepalived stop
fi
else
ip a|grep 192.168.240.89
if [ $? -eq 0 ] ;then
sed -i 's/^slaveof/#&/' /etc/redis/6501.conf
if [ $? -eq 0 ] ;then
service redis stop
sleep 1
service redis start
fi
fi
fi
sleep 10
done
# nohup check_Redis.sh & #后台运行