环境:CentOS 6.5 x64
官方网站:
http://www.redis.io/
http://www.redis.cn
http://www.redis.cn/topics/cluster-tutorial.html
http://www.redis.cn/topics/sentinel.html
http://redis.io/topics/sentinel-clients
参考文档:
https://github.com/LittlePeng/redis-monitor
https://github.com/twitter/twemproxy
http://blog.csdn.net/pi9nc/article/details/17735653
http://redis.readthedocs.org/en/latest/topic/sentinel.html
以redis-2.8.18为例
单机redis
一.安装编译依赖库
[root@master ~]# yum -y install gcc gcc-c++ make
tcl-devel
二.下载并安装
[root@master ~]# wget
http://download.redis.io/releases/redis-2.8.18.tar.gz
[root@master ~]# tar -xvf redis-2.8.18.tar.gz
-C /usr/local/src
[root@master ~]# cd /usr/local/src/redis-2.8.18
[root@master redis-2.8.18]# ls
00-RELEASENOTES COPYING
Makefile
redis.conf
sentinel.conf utils
BUGS
deps
MANIFESTO
runtest
src
CONTRIBUTING
INSTALL
README
runtest-sentinel tests
[root@master redis-2.8.18]# make && make
install
[root@master redis-2.8.18]# which redis-server
/usr/local/bin/redis-server
三.配置redis
sysV管控脚本
[root@master redis-2.8.18]# cd utils/
[root@master utils]# ls
build-static-symbols.tcl
mkrelease.sh
redis-sha1.rb
generate-command-help.rb
redis-copy.rb
speed-regression.tcl
hyperloglog
redis_init_script
whatisdoing.sh
install_server.sh
redis_init_script.tpl
[root@master utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis
server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name
[/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name
[/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance
[/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path
[/usr/local/bin/redis-server]
Selected config:
Port
: 6379
Config file :
/etc/redis/6379.conf
Log
file
: /var/log/redis_6379.log
Data
dir
: /var/lib/redis/6379
Executable
: /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
默认的话,一路回车即可。init脚本会自动生成
[root@master
utils]# mv /etc/init.d/redis_6379
/etc/init.d/redis
[root@master utils]#
/etc/init.d/redis
Please use start, stop, restart or status as first argument
[root@master utils]# /etc/init.d/redis status
Redis is running (4796)
[root@master utils]# /etc/init.d/redis restart
Stopping ...
Redis stopped
Starting Redis server...
[root@master utils]# chkconfig --add redis
[root@master utils]# chkconfig redis on
[root@master utils]# chkconfig --list redis
redis_6379
0:off
1:off
2:on
3:on
4:on
5:on
6:off
[root@master utils]# netstat -tunlp|grep redis
tcp
0
0 0.0.0.0:6379
0.0.0.0:*
LISTEN
4812/redis-server *
可以看到,redis己成功运行并监听在6379端口上。
redis-sentinel
集群监控
Redis-sentinel是Redis官方提供的实例监控管理、通知和实例失效备援服务,是Redis集群的管理工具,用来监控其他redis
instance的工作情况并且进行故障恢复,来提高集群的高可用性。
一.安装同上
二.
配置redis-sentinel
sysV管控脚本
[root@master redis]# cd
/usr/local/src/redis-2.8.18/utils/
[root@master utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis
server
Please select the redis port for this instance: [6379] 26379
Please select the redis config file name [/etc/redis/26379.conf]
/etc/redis/sentinel.conf
Please select the redis log file name [/var/log/redis_26379.log]
/var/log/redis_sentinel.log
Please select the data directory for this instance
[/var/lib/redis/26379] /var/lib/redis/sentinel
Please select the redis executable path
[/usr/local/bin/redis-server]
Selected config:
Port
: 26379
Config file :
/etc/redis/sentinel.conf
Log
file
: /var/log/redis/sentinel.log
Data
dir
: /var/lib/redis/sentinel
Executable
: /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/26379.conf => /etc/init.d/redis_26379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
三.修改配置文件/etc/redis/sentinel.conf
[root@slave
redis]# /etc/init.d/redis_26379 stop
Stopping ...
Redis stopped
[root@master ~]# cd /etc/redis/
[root@master redis]# cp -fv
/usr/local/src/redis-2.8.18/sentinel.conf .
cp: overwrite `./sentinel.conf'? y
`/usr/local/src/redis-2.8.18/sentinel.conf' ->
`./sentinel.conf'
[root@master redis]# mv /etc/init.d/redis_26379
/etc/init.d/redis_sentinel
[root@master redis]# vim
/etc/init.d/redis_sentinel
PIDFILE=/var/run/redis.pid
$EXEC $CONF --sentinel
echo "Redis started"
redis-sentinel集群模式需要在管控脚本吕加入--sentinel参数,如上,
redis
是修改的,
--sentinel
echo "Redis started"是添加的。
补充:
也可以通过redis-sentinel命令来启动服务,效果一样
[root@master
~]# cp -fv /usr/local/src/redis-2.8.18/src/redis-sentinel
/usr/local/bin/
`/usr/local/src/redis-2.8.18/src/redis-sentinel' ->
`/usr/local/bin/redis-sentinel'
[root@master ~]# vim /etc/init.d/redis_sentinel
EXEC=/usr/local/bin/redis-sentinel
PIDFILE=/var/run/redis.pid
echo "Redis
started"
[root@master
redis]# /etc/init.d/redis_sentinel restart
/var/run/redis_26379.pid does not exist, process is not
running
Starting Redis server...
[1880] 18 Dec 17:18:57.606 * Increased maximum number of open files
to 10032 (it was originally set to 1024).
[1880] 18 Dec 17:18:57.608 # +monitor master mymaster 127.0.0.1 6379 quorum 2
daemonize yes #在配置文件/etc/redis/sentinel.conf中加入该参数可以后台运行。
加入开机启动项
[root@master ~]# chkconfig --add redis_sentinel
[root@master ~]# chkconfig redis_sentinel on
[root@master ~]# chkconfig --list redis_sentinel
redis_sentinel
0:off
1:off
2:on
3:on
4:on
5:on
6:off
[root@master ~]# netstat -tunlp|grep redis
tcp
0
0 0.0.0.0:26379
0.0.0.0:*
LISTEN
1898/redis-server *
配置集群
Sentinel:
192.168.8.80
Master: 192.168.8.81
Slave: 192.168.8.82
Master
redis配置文件,使用默认的配置文件就可以了
Slave
redis配置文件,需要指定Master主机
slaveof 192.168.8.81
6379
Stopping ...
Redis stopped
Starting Redis server...
slave修改配置文件后需要重启服务才能和master建立主从关系。
查看master节点信息
[root@slave redis]# redis-cli -h 192.168.8.81 info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.8.82,port=6379,state=online,offset=29,lag=1
master_repl_offset:29
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:28
查看slave节点信息
[root@slave redis]# redis-cli -h 192.168.8.82 info Replication
# Replication
role:slave
master_host:192.168.8.81
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:85
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
可以看到,master和slave已经成功建立主从关系。
配置集群监控sentinel
[root@sentinel redis]# vim sentinel.conf
#daemonize yes
port 26379
logfile /var/log/redis/sentinel.log
sentinel monitor mymaster 192.168.8.81 6379 1
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 900000
sentinel parallel-syncs mymaster 2
#sentinel monitor mymaster 192.168.8.71 6379 1
#sentinel down-after-milliseconds mymaster 5000
#sentinel failover-timeout mymaster 900000
#sentinel parallel-syncs mymaster 2
提示:可以监控多个主从
[root@sentinel redis]# /etc/init.d/redis_sentinel restart
/var/run/redis.pid does not exist, process is not running
Starting Redis server...
[25497] 20 Dec 13:33:04.377 * Increased maximum number of open files to 10032 (it was originally set to 1024).
[25497] 20 Dec 13:33:04.379 # +monitor master mymaster 192.168.8.81 6379 quorum 1
[25497] 20 Dec 13:33:04.382 * +slave slave 192.168.8.82:6379 192.168.8.82 6379 @ mymaster 192.168.8.81 6379
可以看到sentinel己成功监控到master和slave
补充:安全设置
可以置客户端访问密码
Master配置文件中加入
requirepass test123
Slave配置文件中加入
requirepass test123
masterauth test123
Redis-sentinel配置文件中加入
sentinel auth-pass mymaster test123
[root@sentinel ~]# redis-cli -h 192.168.8.80 -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=192.168.8.81:6379,slaves=1,sentinels=1
或者
[root@sentinel ~]# redis-cli -p 26379
127.0.0.1:26379> PING
PONG
127.0.0.1:26379> SENTINEL masters
1)
127.0.0.1:26379> SENTINEL slaves mymaster
1)
127.0.0.1:26379> SENTINEL get-master-addr-by-name mymaster
1) "192.168.8.81"
2) "6379"
故障群集转移测试
[root@sentinel ~]# redis-cli -h 192.168.8.81 -p 6379 shutdown
关闭master节点的redis服务,查看sentinel监控日志
[25497] 20 Dec 13:48:51.510 # +sdown master mymaster 192.168.8.81 6379
[25497] 20 Dec 13:48:51.510 # +odown master mymaster 192.168.8.81 6379 #quorum 1/1
[25497] 20 Dec 13:48:51.510 # +new-epoch 3
[25497] 20 Dec 13:48:51.510 # +try-failover master mymaster 192.168.8.81 6379
[25497] 20 Dec 13:48:51.565 # +vote-for-leader c1a5ea6985be2f345f1439fd
[25497] 20 Dec 13:48:51.565 # +elected-leader master mymaster 192.168.8.81 6379
[25497] 20 Dec 13:48:51.565 # +failover-state-select-slave master mymaster 192.168.8.81 6379
[25497] 20 Dec 13:48:51.632 # +selected-slave slave 192.168.8.82:6379 192.168.8.82 6379 @ mymaster 192.168.8.81 6379
[25497] 20 Dec 13:48:51.632 * +failover-state-send-slaveof-noone slave 192.168.8.82:6379 192.168.8.82 6379 @ mymaster 192.168.8.81 6379
[25497] 20 Dec 13:48:51.684 * +failover-state-wait-promotion slave 192.168.8.82:6379 192.168.8.82 6379 @ mymaster 192.168.8.81 6379
[25497] 20 Dec 13:48:52.615 # +promoted-slave slave 192.168.8.82:6379 192.168.8.82 6379 @ mymaster 192.168.8.81 6379
[25497] 20 Dec 13:48:52.615 # +failover-state-reconf-slaves master mymaster 192.168.8.81 6379
[25497] 20 Dec 13:48:52.676 # +failover-end master mymaster 192.168.8.81 6379
[25497] 20 Dec 13:48:52.676 # +switch-master mymaster 192.168.8.81 6379 192.168.8.82 6379
[25497] 20 Dec 13:48:52.676 * +slave slave 192.168.8.81:6379 192.168.8.81 6379 @ mymaster 192.168.8.82 6379
[25497] 20 Dec 13:48:57.712 # +sdown slave 192.168.8.81:6379 192.168.8.81 6379 @ mymaster 192.168.8.82 6379
可以看到,master己成功切换至192.168.8.82,相应地192.168.8.81则成为192.168.8.82的slave
[root@sentinel ~]# redis-cli -h 192.168.8.80 -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=192.168.8.82:6379,slaves=1,sentinels=1
当手动重启192.168.8.81时,sentinel将其convert为slave
[root@master ~]# /etc/init.d/redis start
Starting Redis server...
[25497] 20 Dec 13:58:42.556 # -sdown slave 192.168.8.81:6379 192.168.8.81 6379 @ mymaster 192.168.8.82 6379
[25497] 20 Dec 13:58:52.557 * +convert-to-slave slave 192.168.8.81:6379 192.168.8.81 6379 @ mymaster 192.168.8.82 6379