简体   繁体   中英

How to failover to new Master node when using Redis with Sentinel and redis-py?

To subscribe to Sentinel failover, what is the name of the Channel, and how do I detect that I need to refresh the master in the subscribed function?

I have a multi node Redis setup using Redis Sentinel for High Availability and failover.

I need to setup a Pub/Sub to Redis to detect when the Redis Master has failed and the system has elected a new Master.

_sentinel = redis.sentinel.Sentinel([(app.config["REDIS_HOSTNAME"],app.config["REDIS_SENTINEL_PORT"])])
_master = _sentinel.master_for(app.config["REDIS_SERVICE_NAME"])

def _sentinel_message_handler(message):
    #TODO how do I detect that there is a new Redis Master?

_pubsub = _master.pubsub()
_pubsub.subscribe(**{app.config["TODO"]:_sentinel_message_handler})

To achieve this, you will want to subscribe to the sentinel, not the master node. The channel you are looking for is "+switch-master" . The Sentinel documentation indicates the channel as "switch-master" "without the +", but as of 3.2.4 the + is included.

Redis Sentinel Pub/Sub Message Docs

You should be able to reference the redis-py docs for the rest. Redis-py Docs


UPDATE

You could also consider using the client-reconfig-script setting.

When the master changed because of a failover a script can be called in order to perform application-specific tasks to notify the clients that the configuration has changed and the master is at a different address.

http://download.redis.io/redis-stable/sentinel.conf

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM