redis / redis-py

Redis Python client
MIT License
12.54k stars 2.51k forks source link

redis.sentinel.MasterNotFoundError: No master found for mymaster (But able to connect to master with Redis class) #2782

Open cdevacc1 opened 1 year ago

cdevacc1 commented 1 year ago

Facing the same issue: https://github.com/redis/redis-py/issues/1388

@filipcima comment not work, Did I miss anything?

I think your problem here is that you're specifying password only for sentinel. If your redis instance is password protected too, you need to pass it in connection_kwargs like in following code snippet:

sentinel = Sentinel(["localhost:26379"], password="REDIS_PASS", sentinel_kwargs={"password": "SENTINEL_PASS"})

Hope it helps.

>>> sentinel = redis.sentinel.Sentinel([('sentinel.redis', 5000)], password='password', sentinel_kwargs={'password': 'password'}).discover_master('mymaster')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/site-packages/redis/sentinel.py", line 252, in discover_master
    raise MasterNotFoundError(f"No master found for {service_name!r}{error_info}")
redis.sentinel.MasterNotFoundError: No master found for 'mymaster' : Redis<ConnectionPool<Connection<host=sentinel.redis,port=5000,db=0>>> - AuthenticationError('AUTH <password> called without any password configured for the default user. Are you sure your configuration is correct?')

>>> sentinel = redis.sentinel.Sentinel([(':password@sentinel.redis', 5000)], sentinel_kwargs={'password': 'password'}).discover_master('mymaster')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/site-packages/redis/sentinel.py", line 252, in discover_master
    raise MasterNotFoundError(f"No master found for {service_name!r}{error_info}")
redis.sentinel.MasterNotFoundError: No master found for 'mymaster' : Redis<ConnectionPool<Connection<host=:password@sentinel.redis,port=5000,db=0>>> - ConnectionError('Error -2 connecting to :password@sentinel.redis:5000. Name or service not known.')
cdevacc1 commented 1 year ago

This command without password work. Can someone please explain why?

>>> import redis
>>> redis.sentinel.Sentinel([('sentinel.redis', 5000)]).discover_master('mymaster')
('redis-0.redis.redis.svc.cluster.local', 6379)

My /etc/redis/redis.conf

masterauth "password"
requirepass "password"

My /etc/redis/sentinel.conf

port 5000
sentinel resolve-hostnames yes
sentinel announce-hostnames yes
sentinel monitor mymaster redis-0.redis.redis.svc.cluster.local 6379 2
sentinel auth-pass mymaster password
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
nathan-bowman commented 7 months ago

@cdevacc1 Did you ever have any success with this?

This works for me...

root@ubuntu:~# redis-cli -h redis-sentinel-sentinel.redis.svc.cluster.local -p 26379 --no-auth-warning -c SENTINEL MASTERS
1)  1) "name"
    2) "myMaster"
    3) "ip"
    4) "192.168.94.104"
    5) "port"
    6) "6379"
    7) "runid"
...

This works for me...

root@ubuntu:~# cat sentinel.go
package main

import (
        "github.com/redis/go-redis/v9"
        "context"
        "fmt"
)

func main() {
  sentinel := redis.NewSentinelClient(&redis.Options{
          Addr: "redis-sentinel-sentinel-headless.redis.svc.cluster.local:26379",
          Username: "default",
          Password: "asdf"},
  )

  ctx := context.TODO()

  fmt.Print( sentinel.GetMasterAddrByName(ctx, "myMaster").Result() )

}
root@ubuntu:~# go run sentinel.go
[192.168.94.104 6379]

This does NOT...

root@ubuntu:~# python3
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import redis
>>> sentinel = redis.sentinel.Sentinel([('redis-sentinel-sentinel-headless.redis.svc.cluster.local', 26379)], socket_timeout=0.5, password='asdf', sentinel_kwargs={'password': 'asdf'})
>>> sentinel.sentinel_get_master_addr_by_name('myMaster')
True
>>> sentinel.discover_master('myMaster')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/root/test/lib/python3.10/site-packages/redis/sentinel.py", line 301, in discover_master
    raise MasterNotFoundError(f"No master found for {service_name!r}{error_info}")
redis.sentinel.MasterNotFoundError: No master found for 'myMaster'