docker-compose redis

Clone git repository

Pick your redis version and download the git repository.

redis version 4

My example uses 4.0.2-alpine.

https://github.com/itsmetommy/docker-redis4

redis version 2-3

My example uses 2.6.17.

https://github.com/itsmetommy/docker-redis-2-3

Build and start all containers

This will build all the sentinel images, then start all containers in detached mode.

docker-compose up --build -d

Verify that the master is the master and that the slave is the slave

View running docker containers.

docker ps

View the master role.

docker exec -it redis-master redis-cli info | grep role

Example

docker exec -it redis-master redis-cli info | grep role
role:master

View the slave role.

docker exec -it redis-slave redis-cli info | grep role

Example

docker exec -it redis-slave redis-cli info | grep role
role:slave

View sentinel logs

docker-compose logs redis-sentinel-1

OR tail the logs in real time.

Note: If you decide to tail the logs, you will need to open a new terminal window to run the following docker commands.

docker-compose logs -f redis-sentinel-1

Pause the redis-master container to initiate failover

You should see the failover if you are tailing the sentinel logs.

docker pause redis-master

Check the redis-slave role

Connect to the slave and check its role (notice it is now the new master).

Note: You may have to wait a few seconds.

docker exec -it redis-slave redis-cli info | grep role

Example

docker exec -it redis-slave redis-cli info | grep role
role:master

Unpause the redis master container which will bring it back as a slave

docker unpause redis-master

Check the redis-master role

Connect to the master and check its role (notice it is now a slave)

docker exec -it redis-master redis-cli info | grep role

Example

docker exec -it redis-master redis-cli info | grep role
role:slave

Now pause the slave to initiate the failback to the original master

docker pause redis-slave

Verify that the master is back to being the master

docker exec -it redis-master redis-cli info | grep role

Example

docker exec -it redis-master redis-cli info | grep role
role:master

Unpause the slave once the master is back to master

docker unpause redis-slave

Verify the slave is back to being the slave

docker exec -it redis-slave redis-cli info | grep role

Example

docker exec -it redis-slave redis-cli info | grep role
role:slave

View failover logs

If you were tailing the sentinel logs, cancel out of it to run the following command.

docker-compose logs redis-sentinel-1 | egrep 'failover|switch-master'

Example

docker-compose logs redis-sentinel-1 | egrep 'failover|switch-master'
redis-sentinel-1 | [10] 13 Apr 04:12:11.214 # +failover-detected master redismaster 172.23.0.2 6379
redis-sentinel-1 | [10] 13 Apr 04:12:11.316 # +failover-end master redismaster 172.23.0.2 6379
redis-sentinel-1 | [10] 13 Apr 04:12:11.316 # +switch-master redismaster 172.23.0.2 6379 172.23.0.3 6379
redis-sentinel-1 | [10] 13 Apr 04:13:26.005 # +failover-detected master redismaster 172.23.0.3 6379
redis-sentinel-1 | [10] 13 Apr 04:13:26.106 # +failover-end master redismaster 172.23.0.3 6379
redis-sentinel-1 | [10] 13 Apr 04:13:26.106 # +switch-master redismaster 172.23.0.3 6379 172.23.0.2 6379

Stop and remove the docker containers

docker-compose down