redis的哨兵模式配置
Redis 的 Sentinel 系统用于管理多个 Redis 服务器(instance), 该系统执行以下三个任务:
•监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。
•提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。
•自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将失效主服务器的其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为复制新的主服务器; 当客户端试图连接失效的主服务器时, 集群也会向客户端返回新主服务器的地址, 使得集群可以使用新主服务器代替失效服务器。
我使用的redis版本为2.8
哨兵模式的最低配置:
一个主服务器(master):端口6001
两个从服务器(slaver):端口6002、6003
三个哨兵服务器(sentinel):端口26001、26002、26003
master的配置:
redis_6001.conf
# Redis configuration file example
daemonize yes
pidfile /var/run/redis_6001.pid
port 6001
timeout 0
tcp-keepalive 0
loglevel notice
logfile "../run/logs/log_master_6001.log"
databases 16
################################ SNAPSHOTTING #################################
save ""
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump_6001.rdb
dir ../run/data
################################# REPLICATION #################################
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 100
################################## SECURITY ###################################
requirepass 123456
################################### LIMITS ####################################
############################## APPEND ONLY MODE ###############################
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
################################ LUA SCRIPTING ###############################
lua-time-limit 5000
################################## SLOW LOG ###################################
slowlog-log-slower-than 10000
slowlog-max-len 128
############################# Event notification ##############################
notify-keyspace-events ""
############################### ADVANCED CONFIG ###############################
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
################################## INCLUDES ###################################
slaver1的配置:
redis_6002.conf
# Redis configuration file example
daemonize yes
pidfile /var/run/redis_6002.pid
port 6002
timeout 0
tcp-keepalive 0
loglevel notice
logfile "../run/logs/log_slaver1_6002.log"
databases 16
################################ SNAPSHOTTING #################################
save ""
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump_6002.rdb
dir ../run/data
################################# REPLICATION #################################
slaveof 127.0.0.1 6001
masterauth 123456
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 100
################################## SECURITY ###################################
requirepass 123456
################################### LIMITS ####################################
############################## APPEND ONLY MODE ###############################
appendonly yes
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
################################ LUA SCRIPTING ###############################
lua-time-limit 5000
################################## SLOW LOG ###################################
slowlog-log-slower-than 10000
slowlog-max-len 128
############################# Event notification ##############################
notify-keyspace-events ""
############################### ADVANCED CONFIG ###############################
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
################################## INCLUDES ###################################
slaver2的配置:
redis_6003.conf
将端口port改成6003,以及将日志和保存数据的文件名改成6003即可,其他跟slaver1的配置相同。
sentinel1的配置:
sentinel_26001.conf
# Example sentinel.conf
port 26001
dir ../run/tmp
sentinel monitor mymaster 127.0.0.1 6001 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel2和sentinel3的配置
跟sentinel1的配置基本相同,只需要将端口分别修改为26002和26003
启动redis
先启动master,然后启动slaver
redis-server redis_6001.conf
redis-server redis_6002.conf
redis-server redis_6003.conf
最后启动sentinel
redis-server /usr/local/src/redis-2.8.3/sentinel/sentinel_26001.conf --sentinel
redis-server /usr/local/src/redis-2.8.3/sentinel/sentinel_26002.conf --sentinel
redis-server /usr/local/src/redis-2.8.3/sentinel/sentinel_26003.conf --sentinel
注意:这里的路径必须是绝对路径
最后,测试一下master的数据能否备份到两个slaver里,以及master死机之后slaver能否变成master。如果两个功能都能实现,说明redis的哨兵模式已经部署成功。