Redis 哨兵模式(Sentinel mode)介绍
安装部署
安装
# CentOS
yum install -y redis
# Ubuntu
apt install -y redis-server
配置 redis.conf
redis master /etc/redis.conf 配置:
bind 0.0.0.0
daemonize yes
masterauth <master-password>
requirepass <password>
systemctl start redis.service
redis slave /etc/redis.conf 配置:
与 master 相同,新增:
slaveof <redis-master-ip> <redis-port>
systemctl start redis.service
配置 redis-sentinel.conf
redis /etc/redis-sentinel.conf 配置:
bind 0.0.0.0
protected-mode no
port 26379
sentinel monitor mymaster <redis-master-ip> <redis-port> 2
sentinel auth-pass mymaster <password>
###启动服务
systemctl start redis.service
systemctl start redis-sentinel.service
systemctl enable redis.service
systemctl enable redis-sentinel.service
运维命令
参考:https://redis.io/topics/sentinel
信息查看命令
info Server
info Clients
info CPU
info Stats
info Sentinel
集群信息
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=547,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=547,lag=1
master_repl_offset:547
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:546
查看 Sentinel 信息
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=172.22.134.190:6379,slaves=2,sentinels=4
查看所有信息
[root@xiexianbin_cn ~]# redis-cli -h 127.0.0.1 -p 26379
127.0.0.1:26379> info
# Server
redis_version:4.0.6
redis_git_sha1:2334ceab
redis_git_dirty:0
redis_build_id:5b333971ded73847
redis_mode:sentinel
os:Linux 3.10.0-514.26.2.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:6.4.0
process_id:12
run_id:57b456d0d88c51b45e73b5f4787f93797215567b
tcp_port:26379
uptime_in_seconds:12512
uptime_in_days:0
hz:17
lru_clock:6772127
executable:/redis-sentinel
config_file:/sentinel.conf
# Clients
connected_clients:4
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# CPU
used_cpu_sys:75.26
used_cpu_user:26.09
used_cpu_sys_children:0.00
used_cpu_user_children:0.00
# Stats
total_connections_received:6
total_commands_processed:54321
instantaneous_ops_per_sec:3
total_net_input_bytes:3096315
total_net_output_bytes:347993
instantaneous_input_kbps:0.20
instantaneous_output_kbps:0.02
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
# 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=172.22.134.190:6379,slaves=2,sentinels=4
列出主Redis服务实例
127.0.0.1:26379> sentinel masters
1) 1) "name"
2) "mymaster"
3) "ip"
4) "172.22.134.190"
5) "port"
6) "6379"
...
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"
列出所有从实例
127.0.0.1:26379> sentinel slaves mymaster
1) 1) "name"
2) "127.0.0.1:6379"
3) "ip"
4) "127.0.0.1"
5) "port"
...
39) "slave-repl-offset"
40) "3431244"
2) 1) "name"
2) "172.22.227.237:6379"
3) "ip"
4) "172.22.227.237"
5) "port"
6) "6379"
...
37) "slave-priority"
38) "100"
39) "slave-repl-offset"
40) "3431244"
列出主实例的 IP 地址和端口号
127.0.0.1:26379> sentinel get-master-addr-by-name mymaster
1) "172.22.134.190"
2) "6379"
重置所有名字和给定模式 pattern 相匹配的主服务器
127.0.0.1:26379> sentinel reset mymaster
(integer) 1
主动的failover
127.0.0.1:26379> sentinel failover mymaster
OK
F&Q
NOAUTH Authentication required
MASTER aborted replication with an error: NOAUTH Authentication required.
方式一:在 /etc/redis.conf
中添加认证配置,永久有效
masterauth xxx
方式二:通过命令行设置密码,重启无效
# 设置密码
config set masterauth "xxx"
# 获取当前加载的密码
config get masterauth
# 立即生效
config rewrite
# config set requirepass "xxx"
扩展