skip to Main Content

i am running redis with sentinel in docker and I am not able to connect to my redis using the sentinel. This is my docker-compose file:

version: '3'

networks:
  app-tier:
    driver: bridge

services:
  redis:
    image: 'bitnami/redis:latest'
    environment:
        - REDIS_REPLICATION_MODE=master
        - REDIS_PASSWORD=str0ng_passw0rd
    networks:
      - app-tier
    ports:
      - '6379'
  redis-slave:
    image: 'bitnami/redis:latest'
    environment:
        - REDIS_REPLICATION_MODE=slave
        - REDIS_MASTER_HOST=redis
        - REDIS_MASTER_PASSWORD=str0ng_passw0rd
        - REDIS_PASSWORD=str0ng_passw0rd
    ports:
        - '6379'
    depends_on:
        - redis
    networks:
        - app-tier
  redis-sentinel:
    image: 'bitnami/redis-sentinel:latest'
    environment:
        - REDIS_MASTER_HOST=redis
        - REDIS_MASTER_PASSWORD=str0ng_passw0rd
    depends_on:
        - redis
        - redis-slave
    ports:
        - '26379:26379'
    networks:
        - app-tier

I can see that my sentinetl is up and running and have access to master and slave:

docker exec redis-image_redis-sentinel_1 redis-cli -p 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.23.0.2:6379,slaves=1,sentinels=1

But when I am accessing to redis from my local PC outside the docker network from java I am getting this output:

 redis.clients.jedis.JedisSentinelPool initSentinels
INFO: Trying to find master from available Sentinels...
 redis.clients.jedis.JedisSentinelPool initSentinels
INFO: Redis master running at 172.23.0.2:6379, starting Sentinel listeners...
 redis.clients.jedis.JedisSentinelPool initPool
INFO: Created JedisPool to master at 172.23.0.2:6379
from master
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
    at redis.clients.util.Pool.getResource(Pool.java:53)
    at redis.clients.jedis.JedisSentinelPool.getResource(JedisSentinelPool.java:209)
    at Main.main(Main.java:22)
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
    at redis.clients.jedis.Connection.connect(Connection.java:164)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:80)
    at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1677)
    at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:87)
    at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:868)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:435)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)
    at redis.clients.util.Pool.getResource(Pool.java:49)
    ... 2 more
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.base/java.net.Socket.connect(Socket.java:608)
    at redis.clients.jedis.Connection.connect(Connection.java:158)
    ... 9 more

Looks like I am getting internal docker network IP (172.23.0.2:6379) and I am not able to connect to the instance.

I am not able to find solution how to connect to redis with sentinals outside docker.

Thanks

2

Answers


  1. try to use this :

    docker pull bitnami/redis
    docker pull bitnami/redis-sentinel
    docker network create redis-network --driver bridge
    docker run --name redis-server -e ALLOW_EMPTY_PASSWORD=yes -p 6379:6379 --network redis-network bitnami/redis
    docker run --name redis-sentinel -it --rm -e REDIS_MASTER_HOST=127.0.0.1 -p 26379:26379 --network redis-network bitnami/redis-sentinel
    

    you may connect to sentinel at 127.0.0.1:26379

    Login or Signup to reply.
  2. I don’t know if you are still having this problem, but it looks like it is due to the port bindings on your "redis" and "redis-slave" containers.

    If you use a single port number (e.g. - 6379), Docker will bind that port on your container to a random port on your host. If you change it to something like - 6379:6379, it will create the desired port bindings.

    You can test this by running docker container ls and checking the values in the "ports" column.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search