/redis-cluster-docker-swarm

Redis cluster cache configuration for docker swarm

Primary LanguageShell

Redis Cluster Cache for Docker Swarm

CircleCI

Quick and dirty Redis cluster taking advantage of Redis Sentinel for automatic failover. Persistence is turned off by default.

Usage

  1. Setup docker swarm
  2. Create a overlay network:
docker network create --attachable --driver overlay redis
  1. Modify scripts/docker-compose.yml to how you want to deploy the stack.
  2. Run scripts/bootstrap.sh.
bash scripts/bootstrap.sh latest
  1. Profit!

Details

A docker service called redis-zero is created to serve as the initial master for the redis sentinels to setup. The redis-look instances watches the redis sentinels for a master, and connects to redis-zero once a master has been decided. Once the dust has settled, remove the redis-zero instance and wait for failover to take over so a new redis-master will take over. Use redis-utils to reset sentinels so that its metadata is accurate with the correct state.

The use of redis-zero as a bootstrapping step allows for the docker-compose.yml to provide only the long running services:

version: '3.1'

services:

  redis-sentinel:
    image: redis-sentinel:v0.1.0-redis-4.0.2
    environment:
      - REDIS_IP=redis-zero
      - REDIS_MASTER_NAME=redismaster
    deploy:
      replicas: 3
    networks:
      - redis

  redis:
    image: redis-look:v0.1.0-redis-4.0.2
    environment:
      - REDIS_SENTINEL_IP=redis-sentinel
      - REDIS_MASTER_NAME=redismaster
      - REDIS_SENTINEL_PORT=26379
    deploy:
      replicas: 3
    networks:
      - redis

networks:
  redis:
    external: true

Scaling

From now on just scale redis to expand the number of slaves or scale redis-sentinel to increase the number of sentinels.