Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cstpraveen/b327d6c6b1b983f5fe243dc29c8342b8 to your computer and use it in GitHub Desktop.
Save cstpraveen/b327d6c6b1b983f5fe243dc29c8342b8 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\"}') "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment