Skip to content

Instantly share code, notes, and snippets.

@jakeonfire
Last active October 9, 2019 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakeonfire/8d3e6abda4ac8001fbc0bf7f70f04a3e to your computer and use it in GitHub Desktop.
Save jakeonfire/8d3e6abda4ac8001fbc0bf7f70f04a3e to your computer and use it in GitHub Desktop.
Run a redis cluster as part of a docker compose configuration (for testing and development). The below sets up 3 masters with 1 replica each.
services:
redis-cluster:
image: redis
expose:
- '7000-7005'
environment:
- PORT=7000
- NODES=6
- REPLICAS=1
- TIMEOUT=5000
command: >
/usr/bin/env bash -c '
function ctrl_c() {
echo "Exiting!"
exit
}
trap "ctrl_c" SIGINT
# adapted from redis-5.0.5/utils/create-cluster/create-cluster (start & create)
ENDPORT=$$((PORT + NODES))
HOSTS=""
IP=$$(hostname -I)
IP=$$(echo $${IP}) # trim whitespace
while [[ $$((PORT < ENDPORT)) != "0" ]]; do
echo "Starting $$PORT..."
redis-server --port $$PORT --cluster-enabled yes --cluster-config-file nodes-$${PORT}.conf --cluster-node-timeout $$TIMEOUT --appendonly yes --appendfilename appendonly-$${PORT}.aof --dbfilename dump-$${PORT}.rdb --logfile $${PORT}.log --daemonize yes
HOSTS="$$HOSTS $$IP:$$PORT"
PORT=$$((PORT+1))
done
echo "yes" | eval redis-cli --cluster create $$HOSTS --cluster-replicas $$REPLICAS
tail -f *.log
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment