Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use redis-cli to create a Redis Cluster on Docker (with Redis 5.0)
#------------ bootstrap the cluster nodes --------------------
start_cmd='redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes'
redis_image='redis:5.0-rc'
network_name='redis_cluster_net'
docker network create $network_name
echo $network_name " created"
#---------- create the cluster ------------------------
for port in `seq 6379 6384`; do \
docker run -d --name "redis-"$port -p $port:6379 --net $network_name $redis_image $start_cmd;
echo "created redis cluster node redis-"$port
done
cluster_hosts=''
for port in `seq 6379 6384`; do \
hostip=`docker inspect -f '{{(index .NetworkSettings.Networks "redis_cluster_net").IPAddress}}' "redis-"$port`;
echo "IP for cluster node redis-"$port "is" $hostip
cluster_hosts="$cluster_hosts$hostip:6379 ";
done
echo "cluster hosts "$cluster_hosts
echo "creating cluster...."
echo 'yes' | docker run -i --rm --net $network_name $redis_image redis-cli --cluster create $cluster_hosts --cluster-replicas 1;
@shinyzhu

This comment has been minimized.

Copy link

@shinyzhu shinyzhu commented Mar 3, 2019

Can NOT connect through cluster mode. Refer to the network limit: https://redis.io/topics/cluster-tutorial, a redis cluster in docker must be in host network.

@immonk

This comment has been minimized.

Copy link

@immonk immonk commented Jul 17, 2019

Agree on @shinyzhu , I've read , but I've seen many blog post for dockerizing redis cluster

@AlexMikhalev

This comment has been minimized.

Copy link

@AlexMikhalev AlexMikhalev commented Jan 6, 2021

Works for me. Thank you @abhirockzz, helpful gist I will try to update it to latest Redis and use for Redis Gears deployment.

@yustinus-arjuna-tokopedia

This comment has been minimized.

Copy link

@yustinus-arjuna-tokopedia yustinus-arjuna-tokopedia commented Feb 7, 2021

this works for me. Thank you for sharing this helpful gist.

@margach

This comment has been minimized.

Copy link

@margach margach commented Feb 23, 2021

From the "Horse's mouth", an extract from the self-documented Redis.conf file version 6.0 and above.


# In certain deployments, Redis Cluster nodes address discovery fails, because
# addresses are NAT-ted or because ports are forwarded (the typical case is
# Docker and other containers).
#
# In order to make Redis Cluster working in such environments, a static
# configuration where each node knows its public address is needed. The
# following two options are used for this scope, and are:
#
# * cluster-announce-ip
# * cluster-announce-port
# * cluster-announce-bus-port
#
# Each instructs the node about its address, client port, and cluster message
# bus port. The information is then published in the header of the bus packets
# so that other nodes will be able to correctly map the address of the node
# publishing the information.
#
# If the above options are not used, the normal Redis Cluster auto-detection
# will be used instead.
#
# Note that when remapped, the bus port may not be at the fixed offset of
# clients port + 10000, so you can specify any port and bus-port depending
# on how they get remapped. If the bus-port is not set, a fixed offset of
# 10000 will be used as usual.
#
# Example:
#
# cluster-announce-ip 10.1.1.5
# cluster-announce-port 6379
# cluster-announce-bus-port 6380
@AlexMikhalev

This comment has been minimized.

Copy link

@AlexMikhalev AlexMikhalev commented Feb 25, 2021

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