Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect Redis with sentinels in docker?

I setup a Redis master/slaves/sentinels from docker, and this is my docker-compose.yml

  redis-master:
    image: redis:3
    ports:
      - 6380:6379
  redis-slave:
    image: redis:3
    ports:
      - 6381:6379
    command: redis-server --slaveof redis-master 6379
    deploy:
      replicas: 2
  redis-sentinel:
    image: mengli/redis-sentinel
    ports:
      - 26379:26379
    deploy:
      replicas: 3
    environment:
      - MASTER_HOST=redis-mater
      - SENTINEL_PORT=26379
      - SENTINEL_QUORUM=2

I want to connect the Redis out of docker, I use spring-data-redis, and this is my configuration:

  redis:
    sentinel:
      master: mymaster
      nodes: 127.0.0.1:26379

but when connect to the Redis, ip address as 10.0.0.* was found, which is the ip address in docker, so the a connection exception was thrown.

Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

Please tell me how to connect Redis with sentinels out of docker. Thanks

like image 572
dream83619 Avatar asked Oct 03 '17 11:10

dream83619


People also ask

How do I install Redis on a docker container?

Install "redis-tools" in your docker host. Centos install redis via epel release. sudo docker run -d --name redis-test -p 6379:6379 -v /redis/redis.conf:/redis.conf redis redis-server /redis.conf --appendonly yes --requirepass "redis"

How do I Find my Redis Container ID?

If this doesn't work, you can do more digging by running docker ps to get a list of running container IDs, and then docker exec -it <redis container ID> redis-cli to connect directly to your redis container and run redis-cli.

How can I access Redis in another machine?

you can access the Redis in the same machine using Redis-CLI and if you are using other machines use host machine IP. if you are accessing Redis container in the same host another docker container uses the private IP of the machine. In case you want to run Redis Cluster in docker containers.

How to connect to localhost from a docker container?

This forwards redis from the container to your host, so you should just be able to connect to localhost as if redis was running directly your machine. If this doesn't work, you can do more digging by running docker ps to get a list of running container IDs, and then docker exec -it <redis container ID> redis-cli


1 Answers

docker-compose

services:
  master:
    image: redis
    ports:
      - 6379:6379
  slave:
    image: redis
    command: >
      bash -c "echo 'port 6380' > slave.conf &&
      echo 'replicaof master 6379' >> slave.conf &&
      cat slave.conf &&
      redis-server slave.conf"
    links:
      - master
    ports:
      - 6380:6380
  sentinel:
    image: redis
    command: >
      bash -c "echo 'port 26379' > sentinel.conf &&
      echo 'dir /tmp' >> sentinel.conf &&
      echo 'sentinel monitor mymaster master 6379 2' >> sentinel.conf &&
      echo 'sentinel down-after-milliseconds mymaster 5000' >> sentinel.conf &&
      echo 'sentinel parallel-syncs mymaster 1' >> sentinel.conf &&
      echo 'sentinel failover-timeout mymaster 5000' >> sentinel.conf &&
      cat sentinel.conf &&
      redis-server sentinel.conf --sentinel"
    links:
      - master
      - slave
    ports:
      - 26379:26379
  sentinel1:
    image: redis
    command: >
      bash -c "echo 'port 26380' > sentinel.conf &&
      echo 'dir /tmp' >> sentinel.conf &&
      echo 'sentinel monitor mymaster master 6379 2' >> sentinel.conf &&
      echo 'sentinel down-after-milliseconds mymaster 5000' >> sentinel.conf &&
      echo 'sentinel parallel-syncs mymaster 1' >> sentinel.conf &&
      echo 'sentinel failover-timeout mymaster 5000' >> sentinel.conf &&
      cat sentinel.conf &&
      redis-server sentinel.conf --sentinel"
    links:
      - master
      - slave
    ports:
      - 26380:26380
  sentinel2:
    image: redis
    command: >
      bash -c "echo 'port 26381' > sentinel.conf &&
      echo 'dir /tmp' >> sentinel.conf &&
      echo 'sentinel monitor mymaster master 6379 2' >> sentinel.conf &&
      echo 'sentinel down-after-milliseconds mymaster 5000' >> sentinel.conf &&
      echo 'sentinel parallel-syncs mymaster 1' >> sentinel.conf &&
      echo 'sentinel failover-timeout mymaster 5000' >> sentinel.conf &&
      cat sentinel.conf &&
      redis-server sentinel.conf --sentinel"
    links:
      - master
      - slave
    ports:
      - 26381:26381
  app:
    build:
      context: ./
      dockerfile: Dockerfile
    links:
      - master
      - slave
      - sentinel
      - sentinel1
      - sentinel2
    working_dir: /app
    command: [sh, -c, 'mkdir -p ~/logs/; cd /src ; mvn clean spring-boot:run -Dspring.profiles.active=local -DLOG_DIR=/root/logs/ -DLOG_FILE=hubstamper.log']
    ports:
      - 8080:8080
    volumes:
      - "${HOME}/.m2:/root/.m2"

applicationproperties file

spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=sentinel:26379,sentinel1:26380,sentinel2:26381

dockerfile

FROM maven:3.5-jdk-8-alpine
COPY . /app
like image 179
Anshul Sharma Avatar answered Nov 15 '22 05:11

Anshul Sharma