Merge pull request #142 from motusllc/master

add support to replica mode for redis-sentinel to find the master
This commit is contained in:
Javier J. Salmerón-García 2019-04-03 18:08:51 +02:00 committed by GitHub
commit 51b475756f
7 changed files with 44 additions and 0 deletions

View File

@ -23,6 +23,9 @@ ENV ALLOW_EMPTY_PASSWORD="no" \
REDIS_MASTER_PASSWORD="" \
REDIS_MASTER_PASSWORD_FILE="" \
REDIS_MASTER_PORT_NUMBER="6379" \
REDIS_SENTINEL_HOST="" \
REDIS_SENTINEL_MASTER_NAME="mymaster" \
REDIS_SENTINEL_PORT_NUMBER="26379" \
REDIS_PASSWORD="" \
REDIS_PASSWORD_FILE="" \
REDIS_REPLICATION_MODE=""

View File

@ -273,6 +273,11 @@ redis_configure_replication() {
redis_conf_set masterauth "$REDIS_PASSWORD"
fi
elif [[ "$REDIS_REPLICATION_MODE" =~ ^(slave|replica)$ ]]; then
if [[ -n "$REDIS_SENTINEL_HOST" ]]; then
REDIS_SENTINEL_INFO=($(redis-cli -h $REDIS_SENTINEL_HOST -p $REDIS_SENTINEL_PORT_NUMBER sentinel get-master-addr-by-name $REDIS_SENTINEL_MASTER_NAME))
REDIS_MASTER_HOST=${REDIS_SENTINEL_INFO[0]}
REDIS_MASTER_PORT_NUMBER=${REDIS_SENTINEL_INFO[1]}
fi
wait-for-port --host "$REDIS_MASTER_HOST" "$REDIS_MASTER_PORT_NUMBER"
[[ -n "$REDIS_MASTER_PASSWORD" ]] && redis_conf_set masterauth "$REDIS_MASTER_PASSWORD"
# Starting with Redis 5, use 'replicaof' instead of 'slaveof'. Maintaining both for backward compatibility

View File

@ -23,6 +23,9 @@ ENV ALLOW_EMPTY_PASSWORD="no" \
REDIS_MASTER_PASSWORD="" \
REDIS_MASTER_PASSWORD_FILE="" \
REDIS_MASTER_PORT_NUMBER="6379" \
REDIS_SENTINEL_HOST="" \
REDIS_SENTINEL_MASTER_NAME="mymaster" \
REDIS_SENTINEL_PORT_NUMBER="26379" \
REDIS_PASSWORD="" \
REDIS_PASSWORD_FILE="" \
REDIS_REPLICATION_MODE=""

View File

@ -273,6 +273,11 @@ redis_configure_replication() {
redis_conf_set masterauth "$REDIS_PASSWORD"
fi
elif [[ "$REDIS_REPLICATION_MODE" =~ ^(slave|replica)$ ]]; then
if [[ -n "$REDIS_SENTINEL_HOST" ]]; then
REDIS_SENTINEL_INFO=($(redis-cli -h $REDIS_SENTINEL_HOST -p $REDIS_SENTINEL_PORT_NUMBER sentinel get-master-addr-by-name $REDIS_SENTINEL_MASTER_NAME))
REDIS_MASTER_HOST=${REDIS_SENTINEL_INFO[0]}
REDIS_MASTER_PORT_NUMBER=${REDIS_SENTINEL_INFO[1]}
fi
wait-for-port --host "$REDIS_MASTER_HOST" "$REDIS_MASTER_PORT_NUMBER"
[[ -n "$REDIS_MASTER_PASSWORD" ]] && redis_conf_set masterauth "$REDIS_MASTER_PASSWORD"
# Starting with Redis 5, use 'replicaof' instead of 'slaveof'. Maintaining both for backward compatibility

View File

@ -23,6 +23,9 @@ ENV ALLOW_EMPTY_PASSWORD="no" \
REDIS_MASTER_PASSWORD="" \
REDIS_MASTER_PASSWORD_FILE="" \
REDIS_MASTER_PORT_NUMBER="6379" \
REDIS_SENTINEL_HOST="" \
REDIS_SENTINEL_MASTER_NAME="mymaster" \
REDIS_SENTINEL_PORT_NUMBER="26379" \
REDIS_PASSWORD="" \
REDIS_PASSWORD_FILE="" \
REDIS_REPLICATION_MODE=""

View File

@ -273,6 +273,11 @@ redis_configure_replication() {
redis_conf_set masterauth "$REDIS_PASSWORD"
fi
elif [[ "$REDIS_REPLICATION_MODE" =~ ^(slave|replica)$ ]]; then
if [[ -n "$REDIS_SENTINEL_HOST" ]]; then
REDIS_SENTINEL_INFO=($(redis-cli -h $REDIS_SENTINEL_HOST -p $REDIS_SENTINEL_PORT_NUMBER sentinel get-master-addr-by-name $REDIS_SENTINEL_MASTER_NAME))
REDIS_MASTER_HOST=${REDIS_SENTINEL_INFO[0]}
REDIS_MASTER_PORT_NUMBER=${REDIS_SENTINEL_INFO[1]}
fi
wait-for-port --host "$REDIS_MASTER_HOST" "$REDIS_MASTER_PORT_NUMBER"
[[ -n "$REDIS_MASTER_PASSWORD" ]] && redis_conf_set masterauth "$REDIS_MASTER_PASSWORD"
# Starting with Redis 5, use 'replicaof' instead of 'slaveof'. Maintaining both for backward compatibility

View File

@ -377,6 +377,26 @@ The above command scales up the number of replicas to `3`. You can scale down in
> **Note**: You should not scale up/down the number of master nodes. Always have only one master node running.
### Using redis-sentinel
If you have redis-sentinel running, you can use it to find the current master by configuring the following environment variables:
- REDIS_REPLICATION_MODE=slave (we only want to configure this when in slave mode)
- REDIS_SENTINEL_HOST=<sentinel-hosts> (this would ideally be a round robin DNS hostname)
- REDIS_SENTINEL_PORT=<port sentinel is listening on>
What happens behind the scenes is at startup the sentinel gets queried for the current master IP and port, and sets the ```REDIS_MASTER_HOST``` and ```REDIS_MASTER_PORTNUMER``` with those values.
```bash
$ docker run --name redis-replica \
--link redis-master:master \
-e REDIS_REPLICATION_MODE=slave \
-e REDIS_SENTINEL_HOST=redis-sentinel \
-e REDIS_SENTINEL_PORT_NUMBER=26379 \
-e REDIS_SENTINEL_MASTER_NAME=mymaster \
-e REDIS_PASSWORD=password123 \
bitnami/redis:latest
```
## Configuration file
The image looks for configurations in `/opt/bitnami/redis/etc/redis.conf`. You can overwrite the `redis.conf` file using your own custom configuration file.