這篇記錄,如何透過Docker建立一套Redis cluster with Sentinel。
我之前是在windows建立redis cluster with sentinel,
我得老實說,config針對多環境且多instance還滿痛苦的,
我常常都會被搞混又浪費大半天再找錯誤,透過人眼掃描並設定確實耗時費力,
這時透過docker統一處理絕對是首選,而且也可以輕鬆實現IaC,
我將透過Dockerfile建立base image with redis,
並透過docker-compose來建立一套redis cluster with sentinel服務,
同時,我也可以輕鬆又簡單達到scale out目的,我把之前架構修改如下
Dockerfile
FROM redis:4
MAINTAINER RiCo
RUN chown redis:redis /data
VOLUME /data
WORKDIR /data
RUN mkdir -p /usr/local/etc/redis && chmod ugo=+rwx /usr/local/etc/redis
COPY redis-master.conf /usr/local/etc/redis/redis.conf
EXPOSE 6379
CMD ["redis-server","/usr/local/etc/redis/redis.conf"]
Dockerfile-sentinel
FROM redis:4
MAINTAINER RiCo
RUN chown redis:redis /data
VOLUME /data
WORKDIR /data
COPY sentinel.conf /usr/local/etc/redis/sentinel.conf
RUN chmod -R 777 /usr/local/etc/redis
ENV SENTINEL_QUORUM 2
ENV SENTINEL_DOWN_AFTER 5000
ENV SENTINEL_FAILOVER 8000
ENV SENTINEL_PORT 26379
EXPOSE 26379
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod -R 777 /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
docker-compose.yml
# docker-compose up -d --build
version: '3'
services:
master:
image: redis:4
build:
context: .
dockerfile: Dockerfile
volumes:
- ./redis/master:/data
restart: unless-stopped
slave:
image: redis:4
build:
context: .
dockerfile: Dockerfile
command: redis-server --slaveof redis-master 6379
volumes:
- ./redis/slave:/data
restart: unless-stopped
depends_on:
- master
links:
- master:redis-master
sentinel:
image: redis:4-sentinel
build:
context: . #./sentinel/
dockerfile: Dockerfile-sentinel
volumes:
- ./redis/sentinel:/tmp
depends_on:
- master
environment:
- SENTINEL_DOWN_AFTER=5000
- SENTINEL_FAILOVER=5000
links:
- master:redis-master
- slave
redis.conf 和sentinel.conf 可以依照個人需求修改,我針對integration環境設定幾個參數如下
maxmemory 500mb
bind 0.0.0.0
maxmemory-policy allkeys-lru
protected-mode no
appendonly yes
logfile "redis_log"
syslog-enabled yes
@執行docker-compose up –d
@scale out slave
Sudo docker-compose scale slave=3
用不到3秒,我們立馬有3個slave’s instance。
@scale out sentinel
Sudo docker-compose scale sentinel=3
現在我們有3個sentinel’s instance,而且相關設定都參考之前base來取代對應環境變數,一整個輕鬆愜意。
@確認
當我們進入master redis,透過redis-cli > info,可以看到slave數量=3
我先暫停master service,來看看自動failover是否可以正常working
Sudo docker-compose pause master
等達到所設定“DOWN-AFTER” and “FAIL AFTER”條件後,
我們可以查看sentinel log確認是否有自動failover到另一台新的master
Sudo docker-compose logs sentinel
可以看到新的master已經變更為 172.18.0.6,最後我們再恢復原本master。
Sudo docker-compose unpause master
參考
Redis(6)-設定Redis Sentinel & Replication
5 ways to debug an exploding Docker container
Redis Clustering With Redis Sentinel On Docker