乐趣区

关于redis:redis5-主从同步哨兵高可用

环境

10.132.128.23   master   6379     
10.132.128.24   slave    6379  6380

master redis install

  • 10.132.128.23 master:6379 sentinel:26379
[redis@p0-tkhihec-redis3-cache01 redis]$ pwd
/home/redis/redis
[redis@p0-tkhihec-redis3-cache01 redis]$ ls
admin  bin  conf  data  init.d  log  logs
[redis@p0-tkhihec-redis3-cache01 redis]$ cat conf/redis_6379.conf
daemonize yes
bind 0.0.0.0
pidfile "/home/redis/redis/logs/redis_6379.pid"
port 6379
tcp-backlog 511
timeout 300
tcp-keepalive 60
loglevel warning
logfile "/home/redis/redis/logs/redis_6379.log"
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump_6379.rdb"
dir "/home/redis/redis-5.0.5/data"
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
maxclients 10000
maxmemory 3gb
maxmemory-policy volatile-lru
maxmemory-samples 3
appendonly yes
appendfilename "appendonly_6379.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 1024
latency-monitor-threshold 0
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
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
protected-mode no
requirepass "Redis_xxxx"
masterauth "Redis_xxxx"

#rename-command FLUSHALL ''#rename-command CONFIG''
#rename-command EVAL ''

# Generated by CONFIG REWRITE
replicaof 10.132.128.24 6380
[redis@p0-tkhihec-redis3-cache01 redis]$ 
[redis@p0-tkhihec-redis3-cache01 redis]$ cat conf/sentinel.conf
port 26379
daemonize yes
bind 0.0.0.0
protected-mode no
logfile "/home/redis/redis/log/sentinel.log"
dir "/home/redis/redis-5.0.5/conf"
sentinel myid 35d16b608e98ac4aa9f298dc122752df0235981b
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 10.132.128.24 6380 2
sentinel failover-timeout mymaster 10000

# Generated by CONFIG REWRITE
sentinel parallel-syncs mymaster 2
sentinel auth-pass mymaster Redis_xxxx
sentinel config-epoch mymaster 36240
sentinel leader-epoch mymaster 36240
sentinel known-replica mymaster 10.132.128.23 6379
sentinel known-replica mymaster 10.132.128.24 6379
sentinel known-sentinel mymaster 10.132.128.24 26379 ba8e97799c0f58c0c5b028e83adf12aa254832fe
sentinel known-sentinel mymaster 10.132.128.24 26380 abec83027be224b97f2c1e6001acb695543df584
sentinel current-epoch 36240


master redis install

  • 10.132.128.24
    slave:6379 sentinel:26379
    slave:6380 sentinel:26380
[redis@p0-tkhihec-redis3-cache02 ~]$ netstat -tnlp
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      13133/../bin/redis- 
tcp        0      0 0.0.0.0:26379           0.0.0.0:*               LISTEN      8804/./bin/redis-se 
tcp        0      0 0.0.0.0:6380            0.0.0.0:*               LISTEN      13081/../bin/redis- 
tcp        0      0 0.0.0.0:26380           0.0.0.0:*               LISTEN      9740/./bin/redis-se 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      -                   
tcp6       0      0 :::19100                :::*                    LISTEN      -                   
[redis@p0-tkhihec-redis3-cache02 ~]$ ls
redis  redis-5.0.5  redis6380
[redis@p0-tkhihec-redis3-cache02 ~]$ cd redis
[redis@p0-tkhihec-redis3-cache02 redis]$ cat conf/redis_6379.conf
daemonize yes
bind 0.0.0.0
pidfile "/home/redis/redis/logs/redis_6379.pid"
port 6379
tcp-backlog 511
timeout 300
tcp-keepalive 60
loglevel warning
logfile "/home/redis/redis/logs/redis_6379.log"
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump_6379.rdb"
dir "/home/redis/redis-5.0.5/data"
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
maxclients 10000
maxmemory 3gb
maxmemory-policy volatile-lru
maxmemory-samples 3
appendonly yes
appendfilename "appendonly_6379.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 1024
latency-monitor-threshold 0
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
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
protected-mode no
requirepass "Redis_xxxx"
replicaof 10.132.128.24 6380
masterauth "Redis_xxxx"

#rename-command FLUSHALL ''#rename-command CONFIG''
#rename-command EVAL ''

[redis@p0-tkhihec-redis3-cache02 redis]$ cat conf/sentinel.conf
port 26379
daemonize yes
bind 0.0.0.0
protected-mode no
logfile "/home/redis/redis/log/sentinel.log"
dir "/home/redis/redis-5.0.5/conf"
sentinel myid ba8e97799c0f58c0c5b028e83adf12aa254832fe
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 10.132.128.24 6380 2
sentinel failover-timeout mymaster 10000

# Generated by CONFIG REWRITE
sentinel parallel-syncs mymaster 2
sentinel auth-pass mymaster Redis_NOPwd_2020_Tk
sentinel config-epoch mymaster 36240
sentinel leader-epoch mymaster 36240
sentinel known-replica mymaster 10.132.128.23 6379
sentinel known-replica mymaster 10.132.128.24 6379
sentinel known-sentinel mymaster 10.132.128.23 0 5caf62e09e8dbf647d797e9b741f4c02a4454ed0
sentinel known-sentinel mymaster 10.132.128.24 0 787829a9585c4c45e0fcf088a4c12b98a3a4b6ec
sentinel known-sentinel mymaster 10.132.128.24 26380 abec83027be224b97f2c1e6001acb695543df584
sentinel known-sentinel mymaster 10.132.128.23 26379 35d16b608e98ac4aa9f298dc122752df0235981b
sentinel current-epoch 36240
[redis@p0-tkhihec-redis3-cache02 redis]$ cd ../redis6380/
[redis@p0-tkhihec-redis3-cache02 redis6380]$ cat conf/redis_6379.conf
cat: conf/redis_6379.conf: No such file or directory
[redis@p0-tkhihec-redis3-cache02 redis6380]$ cat conf/redis_6380.conf
daemonize yes
bind 0.0.0.0
pidfile "/home/redis/redis/logs/redis_6380.pid"
port 6380
tcp-backlog 511
timeout 300
tcp-keepalive 60
loglevel warning
logfile "/home/redis/redis/logs/redis_6380.log"
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump_6380.rdb"
dir "/home/redis/redis6380/data"
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
maxclients 10000
maxmemory 3gb
maxmemory-policy volatile-lru
maxmemory-samples 3
appendonly yes
appendfilename "appendonly_6380.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 1024
latency-monitor-threshold 0
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
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
protected-mode no
requirepass "Redis_xxxx"

masterauth "Redis_xxxx"

#rename-command FLUSHALL ''#rename-command CONFIG''
#rename-command EVAL ''

[redis@p0-tkhihec-redis3-cache02 redis6380]$ cat conf/sentinel.conf
port 26380
daemonize yes
bind 0.0.0.0
protected-mode no
logfile "/home/redis/redis6380/log/sentinel.log"
dir "/home/redis/redis6380/conf"
sentinel myid abec83027be224b97f2c1e6001acb695543df584
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 10.132.128.24 6380 2
sentinel failover-timeout mymaster 10000
# Generated by CONFIG REWRITE
sentinel parallel-syncs mymaster 2
sentinel auth-pass mymaster Redis_xxxx
sentinel config-epoch mymaster 36240
sentinel leader-epoch mymaster 36240
sentinel known-replica mymaster 10.132.128.23 6379
sentinel known-replica mymaster 10.132.128.24 6379
sentinel known-sentinel mymaster 10.132.128.24 26379 ba8e97799c0f58c0c5b028e83adf12aa254832fe
sentinel known-sentinel mymaster 10.132.128.23 0 5caf62e09e8dbf647d797e9b741f4c02a4454ed0
sentinel known-sentinel mymaster 10.132.128.23 26379 35d16b608e98ac4aa9f298dc122752df0235981b
sentinel current-epoch 36240

启动


[redis@p0-tkhihec-redis3-cache01 redis]$ bin/redis-server conf/redis_6379.conf
[redis@p0-tkhihec-redis3-cache01 redis]$ bin/redis-sentinel conf/sentinel.conf


[redis@p0-tkhihec-redis3-cache01 redis]$ bin/redis-cli -c -p 6379 -a Redis_xxxx
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> info Replication
# Replication
role:slave
master_host:10.132.128.24
master_port:6380
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:36184101
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:595f00174f932a7e121148f151579607ba8820c6
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:36184101
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:35135526
repl_backlog_histlen:1048576


[redis@p0-tkhihec-redis3-cache01 redis]$ bin/redis-cli -c -p 26379 
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.132.128.24:6380,slaves=2,sentinels=3


test

  • 测试主从
##master
127.0.0.1:6379> set test hellokugou
##slave
127.0.0.1:6379> get test
"hellokugou"
  • 测试哨兵故障转移
1.kill 掉 master redis 过程

2. 察看 master sentinel 日志

[redis@p0-tkhihec-redis3-cache01 redis]$ tail -20f /home/redis/redis/log/sentinel.log
8248:X 28 Sep 2021 13:01:06.959 * +reboot master mymaster 10.132.128.23 6379
8248:X 28 Sep 2021 13:01:07.017 # -sdown master mymaster 10.132.128.23 6379
8248:X 28 Sep 2021 13:01:07.017 # -odown master mymaster 10.132.128.23 6379
8248:X 28 Sep 2021 13:01:45.998 # +sdown master mymaster 10.132.128.23 6379
8248:X 28 Sep 2021 13:01:46.033 # +new-epoch 36240
8248:X 28 Sep 2021 13:01:46.034 # +vote-for-leader abec83027be224b97f2c1e6001acb695543df584 36240
8248:X 28 Sep 2021 13:01:46.081 # +odown master mymaster 10.132.128.23 6379 #quorum 3/2
8248:X 28 Sep 2021 13:01:46.081 # Next failover delay: I will not start a failover before Tue Sep 28 13:02:06 2021
8248:X 28 Sep 2021 13:01:46.960 # +config-update-from sentinel abec83027be224b97f2c1e6001acb695543df584 10.132.128.24 26380 @ mymaster 10.132.128.23 6379
8248:X 28 Sep 2021 13:01:46.960 # +switch-master mymaster 10.132.128.23 6379 10.132.128.24 6380
8248:X 28 Sep 2021 13:01:46.960 * +slave slave 10.132.128.24:6379 10.132.128.24 6379 @ mymaster 10.132.128.24 6380
8248:X 28 Sep 2021 13:01:46.961 * +slave slave 10.132.128.23:6379 10.132.128.23 6379 @ mymaster 10.132.128.24 6380
8248:X 28 Sep 2021 13:02:16.982 # +sdown slave 10.132.128.23:6379 10.132.128.23 6379 @ mymaster 10.132.128.24 6380
8248:X 28 Sep 2021 13:02:38.076 # -sdown slave 10.132.128.23:6379 10.132.128.23 6379 @ mymaster 10.132.128.24 6380

## 察看日志得悉 master 转移到了 24 的 6380 上了

[redis@p0-tkhihec-redis3-cache02 redis6380]$ bin/redis-cli -c -p 6380 -a Redis_xxxx
127.0.0.1:6380> info replication
# Replication
role:master
master_host:10.132.128.24
master_port:6380
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:36200699
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:595f00174f932a7e121148f151579607ba8820c6
master_replid2:2d68ce9becb5f8b3949d5b01a9bcfe6030844bd0
master_repl_offset:36200699
second_repl_offset:2274
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:35152124
repl_backlog_histlen:1048576

退出移动版