就让我
她不在这里,她无处追寻,可她在我心里 -- 挥之不去
redis 高可用架构之 sentinel

 遇到故障自动切换的 redis master 的集群。至少需要四个实例来搭建演示,如下

127.0.0.1:6400 主
127.0.0.1:6401 从
127.0.0.1:7400 哨兵1
127.0.0.1:7401 哨兵2

1. 配置文件建立
127.0.0.1:6400 主redis配置文件如下:

daemonize yes
pidfile "/var/run/redis_6400.pid"
port 6400
tcp-backlog 65535
bind 0.0.0.0
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/var/log/redis/redis_6400.log"
maxmemory 8gb
maxmemory-policy allkeys-lru
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 "/data/redis/6400"
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
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

127.0.0.1:6401 从redis配置文件如下:

daemonize yes
pidfile "/var/run/redis_6401.pid"
port 6401
tcp-backlog 65535
bind 0.0.0.0
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/var/log/redis/redis_6401.log"
maxmemory 8gb
maxmemory-policy allkeys-lru
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 "/data/redis/6401"
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
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

127.0.0.1:7400 哨兵1 的配置文件相同如下:

daemonize yes
port 7400
dir "/data/redis/sentinels7400"
pidfile "/var/run/redis_sentinel7400.pid"
logfile "/var/log/redis/sentinel7400.log"
sentinel myid 068b3763283fb922ff1923bcc2a35972f094f987
sentinel monitor mymaster 127.0.0.1 6401 2
sentinel down-after-milliseconds mymaster 6000

127.0.0.1:7401 哨兵2 的配置文件相同如下:

daemonize yes
port 7401
dir "/data/redis/sentinels7401"
pidfile "/var/run/redis_sentinel7401.pid"
logfile "/var/log/redis/sentinel7401.log"
sentinel myid d26f0b208ce0e63f9dfc409f81096318ad5507e4
sentinel monitor mymaster 127.0.0.1 6401 2
sentinel down-after-milliseconds mymaster 6000


2. 启动 redis 所有服务

建立所有需要的文件夹

mkdir -p /data/redis /var/log/redis /data/redis/6400 /data/redis/6401 /data/redis/sentinels7400 /data/redis/sentinels7401

redis 主从服务

./src/redis-server conf/6400.conf
./src/redis-server conf/6401.conf

设置主从

./src/redis-cli -p 6401 slaveof 127.0.0.1 6400

启动哨兵服务

./src/redis-sentinel conf/sentinel7400.conf
./src/redis-sentinel conf/sentinel7401.conf

3. 查看各服务状态

查看 redis 主从服务状态

./src/redis-cli -h 127.0.0.1  -p 6400 info  Replication 
./src/redis-cli -h 127.0.0.1  -p 6401 info  Replication 

查看 redis 哨兵服务状态

./src/redis-cli -h 127.0.0.1  -p 7400 info  Sentinel
./src/redis-cli -h 127.0.0.1  -p 7401 info  Sentinel 

4. 模拟异常,自动切换 master

master 自行挂掉

./src/redis-cli -h 127.0.0.1 -p 6400 shutdown

查看之前的 从 redis

./src/redis-cli -h 127.0.0.1  -p 6401 info Replication

127.0.0.1:6401 升级为 master,完成自动切换。

5. 注意下各配置文件

哨兵服务器配置文件,被追加了一下数据:

# Generated by CONFIG REWRITE
sentinel failover-timeout mymaster 18000
sentinel config-epoch mymaster 1
sentinel leader-epoch mymaster 1
sentinel known-slave mymaster 127.0.0.1 6400
sentinel known-sentinel mymaster 127.0.0.1 7400 068b3763283fb922ff1923bcc2a35972f094f987
sentinel current-epoch 1

可以得知 sentinel 会自动更新配置文件,重启将使用新的master 地址。

包括之前 6400 配置文件,追加了以下数据:

# Generated by CONFIG REWRITE
slaveof 127.0.0.1 6401
<< 上一篇 PHP浮点数的一个常见问题的解答 vagrant 在本地编辑 js 和 css 后,vagrant nginx或 apache 读取的数据为乱码 下一篇 >>
文章标签
随意 | Created At 2014 By William Clinton | 蜀ICP备14002619号-4 |