Skip to content

Instantly share code, notes, and snippets.

@tyagiakhilesh
Last active March 27, 2022 11:53
Show Gist options
  • Save tyagiakhilesh/9c11b4fe3295d3e1dd40d78b40047ee9 to your computer and use it in GitHub Desktop.
Save tyagiakhilesh/9c11b4fe3295d3e1dd40d78b40047ee9 to your computer and use it in GitHub Desktop.
Redis Cluster

To Create a redis cluster with redis5:

  • Download source code.
  • Compile it. make
  • cd <working/dir>
  • mkdir 7003 7004 7005
  • cp <redis/root/src/redis-server 7003/
  • cp <redis/root/src/redis-server 7004/
  • cp <redis/root/src/redis-server 7005/
  • cp <redis/root/src/redis-conf 7003/
  • cp <redis/root/src/redis-conf 7004/
  • cp <redis/root/src/redis-conf 7005/

cd 7003

Change content of redis.conf file; change port to match the directory name. For instance

port 30078
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

Then start each instance.

./redis-server ./redis-conf

Then finally join each instance to make a cluster.

<redit-root>/src/redis-cli --cluster create 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 0

In cases where at times the cluster needs to be created again using these nodes and you see error like [ERR] Node 127.0.0.1:30078 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

Then get inside each node and flushall and cluster reset. Do this. And try to create cluster again. See this link.

One more way to do it

Download the source code

curl -O http://download.redis.io/redis-stable.tar.gz

make

make install ### Optional

cd utils/create-cluster

Modify create-cluster scrip to provide proper hostname.

./create-cluster start

/opt/redis-6.0.6/src/redis-cli --cluster call localhost:30001 CONFIG SET protected-mode no
/opt/redis-6.0.6/src/redis-cli --cluster call localhost:30002 CONFIG SET protected-mode no
/opt/redis-6.0.6/src/redis-cli --cluster call localhost:30003 CONFIG SET protected-mode no
/opt/redis-6.0.6/src/redis-cli --cluster call localhost:30004 CONFIG SET protected-mode no
/opt/redis-6.0.6/src/redis-cli --cluster call localhost:30005 CONFIG SET protected-mode no
/opt/redis-6.0.6/src/redis-cli --cluster call localhost:30006 CONFIG SET protected-mode no

And if you want to avoid above 6 commands, then you can acutally set PROTECTED_MODE=no in create-cluster script. :D

./create-cluster create

That is all.

Redis doesn't support hostname. So be aware: redis/redis#2410

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