Redis Sentinel哨兵模式添加从节点
向部署好的Sentinel哨兵添加一个新的从节点是一个简单的过程,因为Sentinel实现了自动发现机制。您所需要做的就是启动配置。
查看当前的哨兵集群信息
两个从节点、三个sentinel节点,如下所示:
root@ubuntu-x64_01:/opt# /usr/local/redis/bin/redis-cli -h 192.168.88.11 -p 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=192.168.88.11:6379,slaves=2,sentinels=3
当前主从配置信息
192.168.88.11:6379 是主库、6380、6381是从库
root@ubuntu-x64_01:/opt# /usr/local/redis/bin/redis-cli -h 192.168.88.11 -p 6379 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.88.11,port=6380,state=online,offset=1820584,lag=1
slave1:ip=192.168.88.11,port=6381,state=online,offset=1820584,lag=1
master_replid:c66066ef1186fcce90974dc9dd40279e9b0987c0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1820721
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:772146
repl_backlog_histlen:1048576
添加6382从节点
replicaof 192.168.88.11 6379 表示从6379复制数据
root@ubuntu-x64_01:/opt# cat /etc/redis6382.conf
### NETWORK ###
bind 192.168.88.11
protected-mode yes
port 6382
tcp-backlog 511
unixsocket /tmp/redis6382.sock
unixsocketperm 700
timeout 0
tcp-keepalive 300
### GENERAL ###
daemonize no
supervised no
pidfile /var/run/redis6382.pid
loglevel notice
logfile "/data/redis6382/redis6382.log"
databases 16
always-show-logo yes
### SNAPSHOTTING ###
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump6382.rdb
dir /data/redis6382/data/
### REPLICATION ###
replicaof 192.168.88.11 6379
masterauth "123456"
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
### SECURITY ###
requirepass "123456"
### CLIENTS ###
maxclients 10000
### MEMORY MANAGEMENT ###
maxmemory 1073741824
maxmemory-policy volatile-ttl
maxmemory-samples 5
replica-ignore-maxmemory yes
### LAZY FREEING ###
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
### APPEND ONLY MODE ###
appendonly yes
appendfilename "appendonly6382.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 1024mb
aof-load-truncated yes
aof-use-rdb-preamble yes
### LUA SCRIPTING ###
lua-time-limit 5000
### SLOW LOG ###
slowlog-log-slower-than 2000
slowlog-max-len 128
### LATENCY MONITOR ###
latency-monitor-threshold 0
### EVENT NOTIFICATION ###
notify-keyspace-events ""
### ADVANCED CONFIG ###
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
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
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
启动redis-server
root@ubuntu-x64_01:/opt# su - redis -c "/usr/local/redis/bin/redis-server /etc/redis6382.conf 2>&1 >/dev/null & "
root@ubuntu-x64_01:/opt#
# 检查进程 : 6382 端口已启动
root@ubuntu-x64_01:/opt# ps -ef | grep redis-server
redis 2400 1 0 05:22 ? 00:00:21 /usr/local/redis/bin/redis-server 192.168.88.11:6379
redis 2415 1 0 05:22 ? 00:00:20 /usr/local/redis/bin/redis-server 192.168.88.11:6380
redis 2432 1 0 05:22 ? 00:00:20 /usr/local/redis/bin/redis-server 192.168.88.11:6381
redis 3078 1 0 07:58 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.88.11:6382
root 3087 1848 0 07:58 pts/0 00:00:00 grep redis-server
检查复制状态
6382实例已经成为从节点,如下所示:
root@ubuntu-x64_01:/opt# /usr/local/redis/bin/redis-cli -h 192.168.88.11 -p 6379 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:3
slave0:ip=192.168.88.11,port=6380,state=online,offset=1940609,lag=1
slave1:ip=192.168.88.11,port=6381,state=online,offset=1940609,lag=1
slave2:ip=192.168.88.11,port=6382,state=online,offset=1940609,lag=1
master_replid:c66066ef1186fcce90974dc9dd40279e9b0987c0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1940746
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:892171
repl_backlog_histlen:1048576
sentinel信息显示
slaves=3 表示当前有三个从节点(原来是2个从节点,刚新增了6382实例)
root@ubuntu-x64_01:/opt# /usr/local/redis/bin/redis-cli -h 192.168.88.11 -p 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=192.168.88.11:6379,slaves=3,sentinels=3
Redis Sentinel哨兵模式删除从节点
哨兵永远不会忘记给定主人的副本,即使他们长时间无法访问。 这很有用,因为哨兵应该能够在网络分区或故障事件后正确地重新配置返回的副本。
此外,在故障转移之后,故障转移的主服务器实际上被添加为新主服务器的副本,这样它将被重新配置为在它再次可用时立即与新主服务器一起复制。
然而,有时你想从 Sentinels 监控的副本列表中永远删除一个副本(可能是旧的 master)。
为此,您需要向所有 Sentinels 发送 SENTINEL RESET mastername 命令:它们将在接下来的 10 秒内刷新副本列表,仅添加从当前主 INFO 输出中列为正确复制的副本。
关闭从节点进程
root@ubuntu-x64_01:/opt# su - redis -c "/usr/local/redis/bin/redis-cli -h 192.168.88.11 -p 6382 -a \"123456\" shutdown"
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# 检查进程状态
root@ubuntu-x64_01:/opt# ps -ef | grep redis-server
redis 2400 1 0 05:22 ? 00:00:21 /usr/local/redis/bin/redis-server 192.168.88.11:6379
redis 2415 1 0 05:22 ? 00:00:21 /usr/local/redis/bin/redis-server 192.168.88.11:6380
redis 2432 1 0 05:22 ? 00:00:21 /usr/local/redis/bin/redis-server 192.168.88.11:6381
root 3102 1848 0 08:03 pts/0 00:00:00 grep redis-server
刷新副本列表
向所有 Sentinels 发送 SENTINEL RESET mastername 命令:它们将在接下来的 10 秒内刷新副本列表
# 当前slaves=3还是三个从节点!!!
root@ubuntu-x64_01:/opt# /usr/local/redis/bin/redis-cli -h 192.168.88.11 -p 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=192.168.88.11:6379,slaves=3,sentinels=3
# 刷新副本列表
root@ubuntu-x64_01:/opt# /usr/local/redis/bin/redis-cli -h 192.168.88.11 -p 26379 sentinel reset mymaster
(integer) 1
root@ubuntu-x64_01:/opt# /usr/local/redis/bin/redis-cli -h 192.168.88.11 -p 26380 sentinel reset mymaster
(integer) 1
root@ubuntu-x64_01:/opt# /usr/local/redis/bin/redis-cli -h 192.168.88.11 -p 26381 sentinel reset mymaster
(integer) 1
检查状态已更新
slaves=2 已更新为2个从节点!!!
root@ubuntu-x64_01:/opt#
root@ubuntu-x64_01:/opt# /usr/local/redis/bin/redis-cli -h 192.168.88.11 -p 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=192.168.88.11:6379,slaves=2,sentinels=3
root@ubuntu-x64_01:/opt#
root@ubuntu-x64_01:/opt# /usr/local/redis/bin/redis-cli -h 192.168.88.11 -p 26380 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=192.168.88.11:6379,slaves=2,sentinels=3
root@ubuntu-x64_01:/opt#
root@ubuntu-x64_01:/opt# /usr/local/redis/bin/redis-cli -h 192.168.88.11 -p 26381 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=192.168.88.11:6379,slaves=2,sentinels=3