├── README.md ├── docker-compose.yml └── sentinel ├── Dockerfile ├── entrypoint.sh └── sentinel.conf /README.md: -------------------------------------------------------------------------------- 1 | # redis-cluster-with-sentinel 2 | **Redis cluster with Docker Compose** 3 | 4 | There are following services in the cluster, 5 | 6 | * master: Master Redis Server 7 | * slave: Slave Redis Server 8 | * sentinel: Sentinel Server 9 | 10 | 11 | The sentinels are configured with a "mymaster" instance with the following properties - 12 | 13 | ``` 14 | sentinel monitor mymaster redis-master 6379 2 15 | sentinel down-after-milliseconds mymaster 5000 16 | sentinel parallel-syncs mymaster 1 17 | sentinel failover-timeout mymaster 5000 18 | ``` 19 | 20 | The details could be found in sentinel/sentinel.conf 21 | 22 | The default values of the environment variables for Sentinel are as following 23 | 24 | * SENTINEL_QUORUM: 2 25 | * SENTINEL_DOWN_AFTER: 5000 26 | * SENTINEL_FAILOVER: 5000 27 | 28 | 29 | 30 | ## Try it 31 | 32 | Build and start services 33 | ``` 34 | docker-compose up --build 35 | ``` 36 | Check the status of redis cluster 37 | ``` 38 | docker-compose ps 39 | ``` 40 | The result is 41 | ``` 42 | Name Command State Ports 43 | --------------------------------------------------------------------------------------- 44 | redisclusterwithsentinel_master_1 docker-entrypoint.sh redis ... Up 6379/tcp 45 | redisclusterwithsentinel_sentinel_1 entrypoint.sh Up 6379/tcp 46 | redisclusterwithsentinel_slave_1 docker-entrypoint.sh redis ... Up 6379/tcp 47 | ``` 48 | 49 | Scale out the instance number of sentinel 50 | 51 | ``` 52 | docker-compose scale sentinel=3 53 | ``` 54 | 55 | Scale out the instance number of slaves 56 | 57 | ``` 58 | docker-compose scale slave=4 59 | ``` 60 | 61 | Check the status of redis cluster 62 | 63 | ``` 64 | docker-compose ps 65 | ``` 66 | 67 | The result is 68 | 69 | ``` 70 | Name Command State Ports 71 | --------------------------------------------------------------------------------------- 72 | redisclusterwithsentinel_master_1 docker-entrypoint.sh redis ... Up 6379/tcp 73 | redisclusterwithsentinel_sentinel_1 entrypoint.sh Up 6379/tcp 74 | redisclusterwithsentinel_sentinel_2 entrypoint.sh Up 6379/tcp 75 | redisclusterwithsentinel_sentinel_3 entrypoint.sh Up 6379/tcp 76 | redisclusterwithsentinel_slave_1 docker-entrypoint.sh redis ... Up 6379/tcp 77 | redisclusterwithsentinel_slave_2 docker-entrypoint.sh redis ... Up 6379/tcp 78 | redisclusterwithsentinel_slave_3 docker-entrypoint.sh redis ... Up 6379/tcp 79 | redisclusterwithsentinel_slave_4 docker-entrypoint.sh redis ... Up 6379/tcp 80 | ``` 81 | 82 | For stop master redis server. 83 | ``` 84 | docker-compose unpause master 85 | ``` 86 | And get the sentinel infomation with following commands 87 | 88 | ``` 89 | docker-compose exec sentinel redis-cli -p 26379 SENTINEL get-master-addr-by-name mymaster 90 | ``` 91 | 92 | ## References 93 | 94 | [https://github.com/AliyunContainerService/redis-cluster][1] 95 | 96 | [https://registry.hub.docker.com/u/joshula/redis-sentinel/] [2] 97 | 98 | [https://docs.docker.com/compose/] [3] 99 | 100 | [1]: https://github.com/AliyunContainerService/redis-cluster 101 | [2]: https://registry.hub.docker.com/u/joshula/redis-sentinel/ 102 | [3]: https://docs.docker.com/compose/ 103 | 104 | ## Contributors 105 | 106 | * Mustafa Ileri () 107 | 108 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | master: 2 | image: redis:3 3 | slave: 4 | image: redis:3 5 | command: redis-server --slaveof redis-master 6379 6 | links: 7 | - master:redis-master 8 | sentinel: 9 | build: sentinel 10 | environment: 11 | - SENTINEL_DOWN_AFTER=5000 12 | - SENTINEL_FAILOVER=5000 13 | links: 14 | - master:redis-master 15 | - slave -------------------------------------------------------------------------------- /sentinel/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM redis:3 2 | 3 | 4 | ADD sentinel.conf /etc/redis/sentinel.conf 5 | RUN chown redis:redis /etc/redis/sentinel.conf 6 | ENV SENTINEL_QUORUM 2 7 | ENV SENTINEL_DOWN_AFTER 5000 8 | ENV SENTINEL_FAILOVER 10000 9 | ENV SENTINEL_PORT 26000 10 | ADD entrypoint.sh /usr/local/bin/ 11 | RUN chmod +x /usr/local/bin/entrypoint.sh 12 | ENTRYPOINT ["entrypoint.sh"] -------------------------------------------------------------------------------- /sentinel/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | sed -i "s/\$SENTINEL_PORT/$SENTINEL_PORT/g" /etc/redis/sentinel.conf 4 | sed -i "s/\$SENTINEL_QUORUM/$SENTINEL_QUORUM/g" /etc/redis/sentinel.conf 5 | sed -i "s/\$SENTINEL_DOWN_AFTER/$SENTINEL_DOWN_AFTER/g" /etc/redis/sentinel.conf 6 | sed -i "s/\$SENTINEL_FAILOVER/$SENTINEL_FAILOVER/g" /etc/redis/sentinel.conf 7 | 8 | exec docker-entrypoint.sh redis-server /etc/redis/sentinel.conf --sentinel 9 | -------------------------------------------------------------------------------- /sentinel/sentinel.conf: -------------------------------------------------------------------------------- 1 | port 26379 2 | dir /tmp 3 | sentinel monitor mymaster redis-master 6379 $SENTINEL_QUORUM 4 | sentinel down-after-milliseconds mymaster $SENTINEL_DOWN_AFTER 5 | sentinel parallel-syncs mymaster 1 6 | sentinel failover-timeout mymaster $SENTINEL_FAILOVER --------------------------------------------------------------------------------