Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Richard-Mathie/d39ffb5c381344200ead1dee9bb99782 to your computer and use it in GitHub Desktop.
Save Richard-Mathie/d39ffb5c381344200ead1dee9bb99782 to your computer and use it in GitHub Desktop.
Redis cluster setup with docker swarm

Redis Cluster Setup with Docker Swarm

Setup

./redis.sh

Test

test the redis cluster

docker run -it --rm --net mynet redis:3.2.6 redis-cli -c -h redis -p 6379
10.0.0.7:6379> set mykey1 1
OK
10.0.0.7:6379> set mykey2 2
-> Redirected to slot [14119] located at 10.0.0.6:6379
OK
10.0.0.6:6379> set mykey3 3
-> Redirected to slot [9990] located at 10.0.0.4:6379
OK
10.0.0.4:6379> get mykey1
-> Redirected to slot [1860] located at 10.0.0.7:6379
"1"
10.0.0.7:6379> get mykey2
-> Redirected to slot [14119] located at 10.0.0.6:6379
"2"
10.0.0.6:6379> get mykey3
-> Redirected to slot [9990] located at 10.0.0.4:6379
"3"
10.0.0.4:6379> 
#!/bin/bash
REDIS_CONFIG='port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes'
network=mynet
docker service create --name redis \
--network $network \
--replicas=6 \
-e REDIS_CONFIG="$REDIS_CONFIG" \
-e REDIS_CONFIG_FILE="/usr/local/etc/redis/redis.conf" \
redis:3.2.6-alpine sh -c 'mkdir -p $(dirname $REDIS_CONFIG_FILE) && echo "$REDIS_CONFIG" > $REDIS_CONFIG_FILE && cat $REDIS_CONFIG_FILE && redis-server $REDIS_CONFIG_FILE'
sleep 2
docker service ps redis --no-trunc
# run the redis-trib.rb script (the docker inspect runs on the host and the echo output is passed the along to the ruby container)
docker run -it --rm --net $network ruby sh -c "\
gem install redis --version 3.2 \
&& wget http://download.redis.io/redis-stable/src/redis-trib.rb \
&& ruby redis-trib.rb create --replicas 1 \
\$(getent hosts tasks.redis | awk '{print \$1 \":6379\"}') "
@ju5t
Copy link

ju5t commented Dec 25, 2017

Note: the $network needs to be created with --attachable for the redis-trib.rb script to work.

@dev-drprasad
Copy link

what is network type ?? can i use overlay ??

@rong0312
Copy link

it has to be overlay

@gitreapur
Copy link

what is the proper way to shutdown and reboot the docker nodes and actually have the redis cluster come back up.
Running two swarm nodes with 6 resdis instances on each.
Seems like everything I've tried results in a broken cluster.

@spacepirate0001
Copy link

2019-10-16 04:06:47 (348 MB/s) - 'redis-trib.rb' saved [3600/3600]

WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.

All commands and features belonging to redis-trib.rb have been moved
to redis-cli.
In order to use them you should call redis-cli with the --cluster
option followed by the subcommand name, arguments and options.

Use the following syntax:
redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]

Example:
redis-cli --cluster create 10.0.26.20:6379 10.0.26.21:6379 10.0.26.24:6379 10.0.26.23:6379 10.0.26.19:6379 10.0.26.22:6379 --cluster-replicas 1

To get help about all subcommands, type:
redis-cli --cluster help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment