环境

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]$ lsadmin  bin  conf  data  init.d  log  logs[redis@p0-tkhihec-redis3-cache01 redis]$ cat conf/redis_6379.confdaemonize yesbind 0.0.0.0pidfile "/home/redis/redis/logs/redis_6379.pid"port 6379tcp-backlog 511timeout 300tcp-keepalive 60loglevel warninglogfile "/home/redis/redis/logs/redis_6379.log"databases 16stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename "dump_6379.rdb"dir "/home/redis/redis-5.0.5/data"replica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100maxclients 10000maxmemory 3gbmaxmemory-policy volatile-lrumaxmemory-samples 3appendonly yesappendfilename "appendonly_6379.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yeslua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 1024latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-entries 512list-max-ziplist-value 64set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yesprotected-mode norequirepass "Redis_xxxx"masterauth "Redis_xxxx"#rename-command FLUSHALL ''#rename-command CONFIG ''#rename-command EVAL ''# Generated by CONFIG REWRITEreplicaof 10.132.128.24 6380[redis@p0-tkhihec-redis3-cache01 redis]$ [redis@p0-tkhihec-redis3-cache01 redis]$ cat conf/sentinel.confport 26379daemonize yesbind 0.0.0.0protected-mode nologfile "/home/redis/redis/log/sentinel.log"dir "/home/redis/redis-5.0.5/conf"sentinel myid 35d16b608e98ac4aa9f298dc122752df0235981bsentinel deny-scripts-reconfig yessentinel monitor mymaster 10.132.128.24 6380 2sentinel failover-timeout mymaster 10000# Generated by CONFIG REWRITEsentinel parallel-syncs mymaster 2sentinel auth-pass mymaster Redis_xxxxsentinel config-epoch mymaster 36240sentinel leader-epoch mymaster 36240sentinel known-replica mymaster 10.132.128.23 6379sentinel known-replica mymaster 10.132.128.24 6379sentinel known-sentinel mymaster 10.132.128.24 26379 ba8e97799c0f58c0c5b028e83adf12aa254832fesentinel known-sentinel mymaster 10.132.128.24 26380 abec83027be224b97f2c1e6001acb695543df584sentinel 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 ~]$ lsredis  redis-5.0.5  redis6380[redis@p0-tkhihec-redis3-cache02 ~]$ cd redis[redis@p0-tkhihec-redis3-cache02 redis]$ cat conf/redis_6379.confdaemonize yesbind 0.0.0.0pidfile "/home/redis/redis/logs/redis_6379.pid"port 6379tcp-backlog 511timeout 300tcp-keepalive 60loglevel warninglogfile "/home/redis/redis/logs/redis_6379.log"databases 16stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename "dump_6379.rdb"dir "/home/redis/redis-5.0.5/data"replica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100maxclients 10000maxmemory 3gbmaxmemory-policy volatile-lrumaxmemory-samples 3appendonly yesappendfilename "appendonly_6379.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yeslua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 1024latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-entries 512list-max-ziplist-value 64set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yesprotected-mode norequirepass "Redis_xxxx"replicaof 10.132.128.24 6380masterauth "Redis_xxxx"#rename-command FLUSHALL ''#rename-command CONFIG ''#rename-command EVAL ''[redis@p0-tkhihec-redis3-cache02 redis]$ cat conf/sentinel.confport 26379daemonize yesbind 0.0.0.0protected-mode nologfile "/home/redis/redis/log/sentinel.log"dir "/home/redis/redis-5.0.5/conf"sentinel myid ba8e97799c0f58c0c5b028e83adf12aa254832fesentinel deny-scripts-reconfig yessentinel monitor mymaster 10.132.128.24 6380 2sentinel failover-timeout mymaster 10000# Generated by CONFIG REWRITEsentinel parallel-syncs mymaster 2sentinel auth-pass mymaster Redis_NOPwd_2020_Tksentinel config-epoch mymaster 36240sentinel leader-epoch mymaster 36240sentinel known-replica mymaster 10.132.128.23 6379sentinel known-replica mymaster 10.132.128.24 6379sentinel known-sentinel mymaster 10.132.128.23 0 5caf62e09e8dbf647d797e9b741f4c02a4454ed0sentinel known-sentinel mymaster 10.132.128.24 0 787829a9585c4c45e0fcf088a4c12b98a3a4b6ecsentinel known-sentinel mymaster 10.132.128.24 26380 abec83027be224b97f2c1e6001acb695543df584sentinel known-sentinel mymaster 10.132.128.23 26379 35d16b608e98ac4aa9f298dc122752df0235981bsentinel current-epoch 36240[redis@p0-tkhihec-redis3-cache02 redis]$ cd ../redis6380/[redis@p0-tkhihec-redis3-cache02 redis6380]$ cat conf/redis_6379.confcat: conf/redis_6379.conf: No such file or directory[redis@p0-tkhihec-redis3-cache02 redis6380]$ cat conf/redis_6380.confdaemonize yesbind 0.0.0.0pidfile "/home/redis/redis/logs/redis_6380.pid"port 6380tcp-backlog 511timeout 300tcp-keepalive 60loglevel warninglogfile "/home/redis/redis/logs/redis_6380.log"databases 16stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename "dump_6380.rdb"dir "/home/redis/redis6380/data"replica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100maxclients 10000maxmemory 3gbmaxmemory-policy volatile-lrumaxmemory-samples 3appendonly yesappendfilename "appendonly_6380.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yeslua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 1024latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-entries 512list-max-ziplist-value 64set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yesprotected-mode norequirepass "Redis_xxxx"masterauth "Redis_xxxx"#rename-command FLUSHALL ''#rename-command CONFIG ''#rename-command EVAL ''[redis@p0-tkhihec-redis3-cache02 redis6380]$ cat conf/sentinel.confport 26380daemonize yesbind 0.0.0.0protected-mode nologfile "/home/redis/redis6380/log/sentinel.log"dir "/home/redis/redis6380/conf"sentinel myid abec83027be224b97f2c1e6001acb695543df584sentinel deny-scripts-reconfig yessentinel monitor mymaster 10.132.128.24 6380 2sentinel failover-timeout mymaster 10000# Generated by CONFIG REWRITEsentinel parallel-syncs mymaster 2sentinel auth-pass mymaster Redis_xxxxsentinel config-epoch mymaster 36240sentinel leader-epoch mymaster 36240sentinel known-replica mymaster 10.132.128.23 6379sentinel known-replica mymaster 10.132.128.24 6379sentinel known-sentinel mymaster 10.132.128.24 26379 ba8e97799c0f58c0c5b028e83adf12aa254832fesentinel known-sentinel mymaster 10.132.128.23 0 5caf62e09e8dbf647d797e9b741f4c02a4454ed0sentinel known-sentinel mymaster 10.132.128.23 26379 35d16b608e98ac4aa9f298dc122752df0235981bsentinel 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_xxxxWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.127.0.0.1:6379> info Replication# Replicationrole:slavemaster_host:10.132.128.24master_port:6380master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:36184101slave_priority:100slave_read_only:1connected_slaves:0master_replid:595f00174f932a7e121148f151579607ba8820c6master_replid2:0000000000000000000000000000000000000000master_repl_offset:36184101second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:35135526repl_backlog_histlen:1048576[redis@p0-tkhihec-redis3-cache01 redis]$ bin/redis-cli -c -p 26379 127.0.0.1:26379> info sentinel# Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=mymaster,status=ok,address=10.132.128.24:6380,slaves=2,sentinels=3

test

  • 测试主从
##master127.0.0.1:6379> set test hellokugou##slave127.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.log8248:X 28 Sep 2021 13:01:06.959 * +reboot master mymaster 10.132.128.23 63798248:X 28 Sep 2021 13:01:07.017 # -sdown master mymaster 10.132.128.23 63798248:X 28 Sep 2021 13:01:07.017 # -odown master mymaster 10.132.128.23 63798248:X 28 Sep 2021 13:01:45.998 # +sdown master mymaster 10.132.128.23 63798248:X 28 Sep 2021 13:01:46.033 # +new-epoch 362408248:X 28 Sep 2021 13:01:46.034 # +vote-for-leader abec83027be224b97f2c1e6001acb695543df584 362408248:X 28 Sep 2021 13:01:46.081 # +odown master mymaster 10.132.128.23 6379 #quorum 3/28248:X 28 Sep 2021 13:01:46.081 # Next failover delay: I will not start a failover before Tue Sep 28 13:02:06 20218248:X 28 Sep 2021 13:01:46.960 # +config-update-from sentinel abec83027be224b97f2c1e6001acb695543df584 10.132.128.24 26380 @ mymaster 10.132.128.23 63798248:X 28 Sep 2021 13:01:46.960 # +switch-master mymaster 10.132.128.23 6379 10.132.128.24 63808248: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 63808248: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 63808248: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 63808248: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_xxxx127.0.0.1:6380> info replication# Replicationrole:mastermaster_host:10.132.128.24master_port:6380master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:36200699slave_priority:100slave_read_only:1connected_slaves:0master_replid:595f00174f932a7e121148f151579607ba8820c6master_replid2:2d68ce9becb5f8b3949d5b01a9bcfe6030844bd0master_repl_offset:36200699second_repl_offset:2274repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:35152124repl_backlog_histlen:1048576