5

I have url as

BROKER_URL = 'sentinel://192.168.10.1:26379/0;sentinel://192.168.10.2:26379/0;sentinel://192.168.10.3:26379/0'

In this, redis is running on 192.168.10.1, 192.168.10.2 and 192.168.10.3. One node is master and others are slaves. If master went down, other node take place for master.

I check the redis client but it has no method, where we can provide url like I gave.

We have to provide hostname and port. In my case, master will be anyone form these 3.

DennisLi
  • 3,915
  • 6
  • 30
  • 66
Nilesh
  • 20,521
  • 16
  • 92
  • 148

2 Answers2

12

Check the redis-py codebase readme.md at https://github.com/andymccurdy/redis-py/blob/master/README.rst#sentinel-support

Like this:

from redis.sentinel import Sentinel
sentinel = Sentinel([('192.168.10.1', 26379), ('192.168.10.2',26379), ('192.168.10.3',26379)], socket_timeout=0.1)

master = sentinel.master_for('master-name', socket_timeout=0.1)

The master and slave objects are normal StrictRedis instances with their connection pool bound to the Sentinel instance. When a Sentinel backed client attempts to establish a connection, it first queries the Sentinel servers to determine an appropriate host to connect to. If no server is found, a MasterNotFoundError or SlaveNotFoundError is raised.

The Actual thing is that if you build Sentinel for redis cluster, you do not need to connect the redis server directly. Do as above, first connect to Sentinel, and use master_for to query the an appropriate host to connect to. Only in this way, if master is down, your client could be directed to the new master.

And The master-name in the code above, you should specify in the sentinel.conf in

sentinel monitor <master-group-name> <ip> <port> <quorum>

like this:

sentinel monitor mymaster 127.0.0.1 6379 2

GuangshengZuo
  • 4,447
  • 21
  • 27
  • How about the case if the master name in sentinel conf is changing according to the master node? Some redis service in cloud contains ip of master node in sentinel conf(like sentinel monitor master-127-0-0-1 127.0.0.1 6379 2), is there a proper way to process this? – buxizhizhoum Jan 13 '20 at 11:37
  • Hi following the steps, when I called `master.keys()` it raised: `unknown command 'SENTINEL'` – DennisLi Mar 14 '20 at 03:56
  • 1
    Should I call `master_for()` (and `slave_for()`) each time I want to read or write to Redis or is it ok to call these functions at the start of the program and reuse the returned objects? – Oleg Yablokov Sep 26 '21 at 09:11
10

To connect with python redis client, you can proceed as follows if you have an authentication required setup with password do as follows:

from redis.sentinel import Sentinel
sentinel = Sentinel([('192.168.10.1', 26379),
                     ('192.168.10.2',26379),
                     ('192.168.10.3',26379)],
                   sentinel_kwargs={'password': YOUR_REDIS_PASSWORD})
# you will need to handle yourself the connection to pass again the password
# and avoid AuthenticationError at redis queries
host, port = sentinel.discover_master(YOUR_REDIS_DB_MASTER)
redis_client = redis.StrictRedis(
            host=host,
            port=port,
            password= YOUR_REDIS_PASSWORD
        )

You can test it directly with a simple query like

redis_client.exists("mykey")

Of course if you didnt set up a password, you can remove the sentinel_kwargs={'password': YOUR_REDIS_PASSWORD} and the password attribute in your redis_client instance.

If the setup failed you might encounter a MasterNotFoundError (if sentinal discovery failed) or a AuthenticationError if you didn't pass the correct password or the password argument at the right place

SARA E
  • 189
  • 1
  • 6
  • If you deploy redis cluster on Kubernetes the master name is found/set through sentinel.masterSet variable in your deployment yaml – SARA E Jan 21 '20 at 15:27
  • I was getting `redis.exceptions.ResponseError: AUTH called without any password configured for the default user. Are you sure your configuration is correct?` Removing `sentinel_kwargs` from `Sentinel()` fixed this issue (idk. why). – hafiz031 Sep 08 '21 at 05:54
  • hi @hafiz031 yes this configuration is only if you did set a password. If you dont have a password call sentinel without the kwargs indeed! – SARA E Oct 04 '21 at 14:07
  • Hi, very recently I faced an issue with this implementation. It couldn't change the master automatically. I got: `redis.exceptions.ReadOnlyError: You can't write against a read only replica.` But when I hard-coded the changed master in the first position inside `Sentinel()` in the program, it worked again. – hafiz031 Nov 18 '21 at 05:14
  • we need to access the redis cluster without in the cluster, so for the ip address, is it a fix ip for the pods? or we can use the ingress(k8s) or route(openshift ) instead? – CYC Aug 13 '22 at 13:54