Skip to content

Instantly share code, notes, and snippets.

@shinyzhu
Forked from abhirockzz/create-redis-cluster.sh
Last active March 3, 2019 03:13
Show Gist options
  • Save shinyzhu/ae867cda5ed75141434c63daa847bffc to your computer and use it in GitHub Desktop.
Save shinyzhu/ae867cda5ed75141434c63daa847bffc to your computer and use it in GitHub Desktop.
Use redis-cli to create a Redis Cluster on Docker (with Redis 5.0)
#------------ bootstrap the cluster nodes --------------------
redis_image='redis:5'
network_name='host' # must be in host mode
#---------- create the cluster ------------------------
for port in `seq 6379 6384`; do \
start_cmd="redis-server --port $port --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes"
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='192.168.1.100' # Should be your host's public ip
echo "IP for cluster node redis-"$port "is" $hostip
cluster_hosts="$cluster_hosts$hostip:$port ";
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
Copy link
Author

shinyzhu commented Mar 3, 2019

the output is:

[root@apphost01 redis]# ./docker_redis_cluster.sh 
3e6df216394d118149ff00d62b5c9efa8e681ece9ee51bab31dfcd449601e3af
created redis cluster node redis-6379
763ffa0e85cbd4588345915870b01374ff2d2c61a27a507d0743135bdc1bb4ae
created redis cluster node redis-6380
76c96f82be7297f4f0e6eb97e9e3d7ef54998e4c835f2aab43190c0666ac4c39
created redis cluster node redis-6381
3762833b52c842dcd818c1b771225e577eada7b333d0aa54339bfea705bb6353
created redis cluster node redis-6382
2d6d0c5bc1884c7a1a870b1e29597565ed5f5355eb65eac843e376ca89ed3439
created redis cluster node redis-6383
e7e162f4e96696b8ac7788c33a829d5c2807b38cef269c25cc71a96c78d10274
created redis cluster node redis-6384
IP for cluster node redis-6379 is 192.168.0.100
IP for cluster node redis-6380 is 192.168.0.100
IP for cluster node redis-6381 is 192.168.0.100
IP for cluster node redis-6382 is 192.168.0.100
IP for cluster node redis-6383 is 192.168.0.100
IP for cluster node redis-6384 is 192.168.0.100
cluster hosts 192.168.0.100:6379 192.168.0.100:6380 192.168.0.100:6381 192.168.0.100:6382 192.168.0.100:6383 192.168.0.100:6384
creating cluster....
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.0.100:6382 to 192.168.0.100:6379
Adding replica 192.168.0.100:6383 to 192.168.0.100:6380
Adding replica 192.168.0.100:6384 to 192.168.0.100:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 97e8066288126de4437716613427eac7c7cac4ea 192.168.0.100:6379
   slots:[0-5460] (5461 slots) master
M: 6e9190a6630b41feb45b1a8e77cc3378b896528c 192.168.0.100:6380
   slots:[5461-10922] (5462 slots) master
M: 804d67fb66be7b78a4f6ca85fb33a66ea7f6ce8f 192.168.0.100:6381
   slots:[10923-16383] (5461 slots) master
S: 2810e659da6067c1464b766fe168f2995639e0ac 192.168.0.100:6382
   replicates 804d67fb66be7b78a4f6ca85fb33a66ea7f6ce8f
S: 1d2215f525bb418c36f483d5d86311600066dc25 192.168.0.100:6383
   replicates 97e8066288126de4437716613427eac7c7cac4ea
S: fba06f28a4e73c14a4ae3c3656faf65139883780 192.168.0.100:6384
   replicates 6e9190a6630b41feb45b1a8e77cc3378b896528c
Can I set the above configuration? (type 'yes' to accept): >>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 192.168.0.100:6379)
M: 97e8066288126de4437716613427eac7c7cac4ea 192.168.0.100:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2810e659da6067c1464b766fe168f2995639e0ac 192.168.0.100:6382
   slots: (0 slots) slave
   replicates 804d67fb66be7b78a4f6ca85fb33a66ea7f6ce8f
S: 1d2215f525bb418c36f483d5d86311600066dc25 192.168.0.100:6383
   slots: (0 slots) slave
   replicates 97e8066288126de4437716613427eac7c7cac4ea
S: fba06f28a4e73c14a4ae3c3656faf65139883780 192.168.0.100:6384
   slots: (0 slots) slave
   replicates 6e9190a6630b41feb45b1a8e77cc3378b896528c
M: 6e9190a6630b41feb45b1a8e77cc3378b896528c 192.168.0.100:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 804d67fb66be7b78a4f6ca85fb33a66ea7f6ce8f 192.168.0.100:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

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