I have set up Redis Sentinel (3 nodes) and Redis (1 master and 2 replicas) on a local Docker Swarm using a Docker Compose YAML file. Currently, the replica-announce-ip
is hardcoded. However, I need to set up Docker Swarm on multiple nodes, and I want to pass the replica-announce-ip
dynamically since the replica nodes may be scheduled on any one of the nodes by Docker Swarm.
Could you please advise on how to achieve this?
Docker Compose. yaml
version: ‘3.8’
networks:
redis_snl-net:
external: true
services:
redis-master:
image: redis:latest
command: redis-server --appendonly yes
ports:
- “16379:6379”
networks:
- redis_snl-net
environment:
- REDIS_REPLICATION_MODE=master
- REDIS_PASSWORD=T0pS3cr3t
redis-slave1:
image: redis:latest
ports:
- “16380:6379”
networks:
- redis_snl-net
environment:
- REDIS_REPLICATION_MODE=replica
- REDIS_MASTER_HOST=redis-master
- REDIS_MASTER_PORT_NUMBER=6379
- REDIS_MASTER_PASSWORD=T0pS3cr3t
- REDIS_PASSWORD=T0pS3cr3t
command: redis-server --slaveof redis-master 6379 --appendonly yes --replica-announce-ip 127.0.0.1 --replica-announce-port 16380
redis-slave2:
image: redis:latest
ports:
- “16381:6379”
networks:
- redis_snl-net
environment:
- REDIS_REPLICATION_MODE=replica
- REDIS_MASTER_HOST=redis-master
- REDIS_MASTER_PORT_NUMBER=6379
- REDIS_MASTER_PASSWORD=T0pS3cr3t
- REDIS_PASSWORD=T0pS3cr3t
command: redis-server --slaveof redis-master 6379 --appendonly yes --replica-announce-ip 127.0.0.1 --replica-announce-port 16381
redis-sentinel1:
image: redis:latest
ports:
- “5000:26379”
networks:
- redis_snl-net
depends_on:
- redis-master
command: redis-server /data/sentinel.conf --sentinel
volumes:
- “./sentinel1:/data”
redis-sentinel2:
image: redis:latest
ports:
- “5001:26379”
networks:
- redis_snl-net
depends_on:
- redis-master
command: redis-server /data/sentinel.conf --sentinel
volumes:
- “./sentinel2:/data”
redis-sentinel3:
image: redis:latest
ports:
- “5002:26379”
networks:
- redis_snl-net
depends_on:
- redis-master
command: redis-server /data/sentinel.conf --sentinel
volumes:
- “./sentinel3:/data”
Sentinel.conf
port 26379
sentinel monitor mymaster 127.0.0.1 16379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster T0pS3cr3t
sentinel announce-ip 127.0.0.1
sentinel announce-port 5000