Sign inSign up

mlawry/redis-sentinel

By mlawry

•Updated almost 8 years ago

Redis cache sentinel mode on Ubuntu 16.04

Image
0

1.7K

mlawry/redis-sentinel repository overview

⁠The Image

Redis 5.0.0 is compiled and installed separately on a Ubuntu 16.04 computer.

$ sudo apt-get install build-essential tcl
$ wget http://download.redis.io/releases/redis-5.0.0.tar.gz
$ tar -xzvf redis-5.0.0.tar.gz
$ cd redis-5.0.0
$ make
$ make test
$ make PREFIX=/opt/redis install

. Then in a new empty directory, create the following Dockerfile:

FROM mlawry/ubuntu-xenial-nocmd:201810

# Install the /usr/bin/host command, which is required by the startsentinel.sh script.
RUN apt update && apt upgrade
RUN apt install -y bind9-host
RUN apt clean

# Copy precompiled redis binary to image.
COPY redis/ /opt/redis/
COPY startsentinel.sh /opt/redis/startsentinel.sh

# Create /data directory to store data.
RUN mkdir /data
EXPOSE 26379
ENTRYPOINT ["/bin/bash", "/opt/redis/startsentinel.sh"]

. Also create a bash script named startsentinel.sh to start redis in sentinel mode.

#!/bin/bash
echo "protected-mode no"        >> /opt/redis/sentinel.conf
echo "dir /data"                >> /opt/redis/sentinel.conf
echo "logfile \"\""             >> /opt/redis/sentinel.conf

# Announce this node's IP address using the hostname if given. No announce if not defined.
if [ -n "$SENTINEL_HOSTNAME" ]; then
  SENTINEL_IP=`host -t A $SENTINEL_HOSTNAME | cut --delimiter=" " --fields=4 -`
  echo "sentinel announce-ip $SENTINEL_IP"    >> /opt/redis/sentinel.conf
fi

# Override default port for this node if given.
if [ -z "$SENTINEL_PORT" ]; then
  echo "port 26379"             >> /opt/redis/sentinel.conf
else
  echo "port $SENTINEL_PORT"    >> /opt/redis/sentinel.conf
fi

# Get master IP address using master's hostname if given.
REDIS_MASTER_IP=127.0.0.1
if [ -n "$REDIS_MASTER_HOSTNAME" ]; then
  REDIS_MASTER_IP=`host -t A $REDIS_MASTER_HOSTNAME | cut --delimiter=" " --fields=4 -`
fi

# Use default port for master node if no override defined.
if [ -z "$REDIS_MASTER_PORT" ]; then
  REDIS_MASTER_PORT=6379
fi

# Use default sentinel quorum if no override defined.
if [ -z "$SENTINEL_QUORUM" ]; then
  SENTINEL_QUORUM=2
fi

echo "sentinel monitor mymaster $REDIS_MASTER_IP $REDIS_MASTER_PORT $SENTINEL_QUORUM" >> /opt/redis/sentinel.conf

echo "Starting redis-server using configuration file"
echo "-----------------------------------"
cat /opt/redis/sentinel.conf
echo "-----------------------------------"

/opt/redis/bin/redis-server /opt/redis/sentinel.conf --sentinel

. Copy the compiled binary into the current directory (required by Dockerfile).

$ cp -r /opt/redis ./redis

. The image is now built with:

sudo docker build -t mlawry/redis-sentinel .
sudo docker push mlawry/redis-sentinel:latest

.

⁠Environment Variables

Set the following environment variables to configure the image behaviour on start up.

SENTINEL_HOSTNAME (optional)

Hostname of the container instance, used by the startsentinel.sh script to get the container IP address, which is then used to output the sentinel announce-ip $SENTINEL_IP configuration line. If not defined or empty, the configuration line is not added.

SENTINEL_PORT (optional)

The port that the sentinel service is listening on. Used by startsentinel.sh to output the port $SENTINEL_PORT configuration line. If not defined or empty, the default port 26379 is used.

REDIS_MASTER_HOSTNAME (optional)

Hostname of the redis master node, used by the startsentinel.sh script to get the IP address to connect to redis master. Used to output the sentinel monitor mymaster $REDIS_MASTER_IP $REDIS_MASTER_PORT $SENTINEL_QUORUM configuration line. If not defined or empty, sentinel will connect to 127.0.0.1 as the master node.

REDIS_MASTER_PORT (optional)

Port of the redis master service, used by the startsentinel.sh script to output the sentinel monitor mymaster $REDIS_MASTER_IP $REDIS_MASTER_PORT $SENTINEL_QUORUM configuration line. If not defined or empty, the default port 6379 is used.

SENTINEL_QUORUM (optional)

Used by the startsentinel.sh script to output the sentinel monitor mymaster $REDIS_MASTER_IP $REDIS_MASTER_PORT $SENTINEL_QUORUM configuration line. You should set this to an appropriate value based on how many sentinel instances are monitoring the redis master. If not defined or empty, the default value of 2 is used.

⁠Actual Configuration

When you run this image, the configuration created by the startsentinel.sh script is printed to standard output. The sentinel process also logs to standard output, using the logfile "" configuration line.

Tag summary

Content type

Image

Digest

Size

263.9 MB

Last updated

almost 8 years ago

docker pull mlawry/redis-sentinel