首先搭建1主两从的redis主从服务
mkdir -pv /etc/redis/
mkdir -pv /redis/db{23}
cd /etc/redis/
redis 主配置不变
vim /usr/local/redis-6.2.6/bin/redis.conf
requirepass "lzjasdqq"
appendonly yes
daemonize yes
pidfile "/var/run/redis_6380.pid"
logfile "/var/log/redis/redis6380.log
protected-mode no
port 6380
dir "/var/lib/redis"

masterauth  lzjasdqq

cp -r /usr/local/redis-6.2.6/bin/redis.conf /etc/redis/redis.conf2
vim /etc/redis/redis.conf2
redis2 配置

requirepass "lzjasdqq"
appendonly yes
daemonize yes
pidfile "/var/run/redis_6380.pid"
logfile "/var/log/redis/redis6380.log
protected-mode no
port 6380
dir "/redis/db2"

replicaof 192.168.40.130 6379

masterauth  lzjasdqq

cp -r /usr/local/redis-6.2.6/bin/redis.conf /etc/redis/redis.conf3
vim /etc/redis/redis.conf3
redis3 配置
requirepass "lzjasdqq"
appendonly yes
daemonize yes
pidfile "/var/run/redis_6381.pid"
logfile "/var/log/redis/redis6381.log
protected-mode no
port 6381
dir "/redis/db3"

replicaof 192.168.40.130 6379

masterauth  lzjasdqq
systemctl start redis 
redis-server /etc/redis/redis.conf2
redis-server /etc/redis/redis.conf3
redis-cli 
info replication
到源码里面可以看到 sentinel.conf
cp sentinel.conf  /etc/
redis-sentinel配置

bind 0.0.0.0
sentinel monitor mymaster 192.168.40.130 6379 1
logfile "/var/log/redis/sentinel.log"
daemonize yes
sentinel auth-pass mymaster lzjasdqq
protected-mode no
#user default on nopass sanitize-payload ~* &* +@all
requirepass "lzjasdqq"


vim  /usr/lib/systemd/system/sentinel.service
[Unit]
Description=redis-server
After=network.target

[Service]
Type=forking
# ExecStart需要按照实际情况修改成自己的地址
ExecStart=/usr/local/redis-6.2.6/bin/redis-server /etc/sentinel.conf --sentinel
PrivateTmp=true

[Install]
WantedBy=multi-user.target





# sentinel auth-pass <master-name> <password>
#
# Set the password to use to authenticate with the master and replicas.
# Useful if there is a password set in the Redis instances to monitor.
#
# Note that the master password is also used for replicas, so it is not
# possible to set a different password in masters and replicas instances
# if you want to be able to monitor these instances with Sentinel.
#
# However you can have Redis instances without the authentication enabled
# mixed with Redis instances requiring the authentication (as long as the
# password set is the same for all the instances requiring the password) as
# the AUTH command will have no effect in Redis instances with authentication
# switched off.
#
# Example:
#
# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd

# sentinel auth-user <master-name> <username>

#sentinel auth pass<master name><password>
#
#设置用于与主服务器和副本进行身份验证的密码。
#如果要监视的Redis实例中设置了密码,则会很有用。
#
#请注意,主密码也用于副本,因此不是
#可以在主实例和副本实例中设置不同的密码
#如果您希望能够使用Sentinel监视这些实例。
#
#但是,您可以在不启用身份验证的情况下使用Redis实例
#与需要身份验证的Redis实例混合(只要
#对于所有需要密码的实例,密码集都是相同的)
#AUTH命

requirepass lzjasdqq
# You can configure Sentinel itself to require a password, however when doing
# so Sentinel will try to authenticate with the same password to all the
# group with the same "requirepass" password. Check the following documentation
# IMPORTANT NOTE: starting with Redis 6.2 "requirepass" is a compatibility
# the password for the default user. Clients will still authenticate using
# AUTH <password> as usually, or more explicitly with AUTH default <password>
# sentinel-user and sentinel-pass)
# The requirepass is not compatable with aclfile option and the ACL LOAD
# command, these will cause requirepass to be ignored.
# sentinel sentinel-pass <password>
# The password for Sentinel to authenticate with other Sentinels. If sentinel-user
# is not configured, Sentinel will use 'default' user with sentinel-pass to authenticate.
# The following arguments are passed to the script:

#您可以将Sentinel本身配置为需要密码,但在执行操作时
#因此Sentinel将尝试使用相同的密码对所有
#具有相同“requirepass”密码的组。检查以下文档
#重要提示:从Redis 6.2“requirepass”开始是一种兼容性
#默认用户的密码。客户端仍将使用进行身份验证
#AUTH<password>通常,或者更明确地使用AUTH默认<password>
#sentinel用户和sentinel通行证)
#requirepass与aclfile选项和ACL LOAD不兼容
#命令,这些将导致requirepass被忽略。
#sentinel sentinel pass<password>
#Sentinel与其他Sentinel进行身份验证的密码。如果哨兵用户
#未配置,Sentinel将使用具有Sentinel通行证的“默认”用户进行身份验证。
#将以下参数传递给脚本:



root@host1 ~]# redis-cli -p 26379
127.0.0.1:26379> auth lzjasdqq
OK
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=192.168.40.130:6380,slaves=2,sentinels=1



测试:停止6380的redis
[root@host1 redis]# ss -ntlp|grep 6380
LISTEN     0      511          *:6380                     *:*                   users:(("redis-server",pid=8572,fd=6))
LISTEN     0      511       [::]:6380                  [::]:*                   users:(("redis-server",pid=8572,fd=7))
[root@host1 redis]# kill -9 8572


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=192.168.40.130:6381,slaves=2,sentinels=1


查看到master已经切换到6381

SENTINEL masters     //列出所有监控的主服务器
SENTINEL slaves <master name>         //获取指定redis集群的从节点

SENTINEL get-master-addr-by-name <master name>        //根据指定master的名称获取其IP
SENTINEL reset         //用于重置,包括名称,清除服务器所有运行状态,故障转移、等等
SENTINEL failover <master name>        //手动向某一组redis集群发起执行故障转移


SENTINEL get-master-addr-by-name mymaster 
SENTINEL failover mymaster

127.0.0.1:26379> SENTINEL get-master-addr-by-name mymaster 
1) "192.168.40.130"
2) "6379"



python操作redis-sentinel 
from redis import Sentinel

sentinel = Sentinel([('192.168.40.130', 26379)], socket_timeout=10,sentinel_kwargs={'password': 'lzjasdqq'})
print(sentinel)
redis_server = sentinel.discover_master('mymaster')

print(redis_server)


master = sentinel.master_for('mymaster', socket_timeout=0.1,password='lzjasdqq')
print(master) # Redis<SentinelConnectionPool<service=mymaster(master)>

master.set('foo','bnar')

slave = sentinel.slave_for('mymaster', socket_timeout=0.1,password='lzjasdqq')
print(slave.get('foo'))