DEV Community

loading...

Creating local Redis cluster in minutes on WSL2

adityakanekar profile image Aditya Kanekar ・4 min read

This article focuses on setting up Redis cluster quickly on local environment using WSL2 (If you have not set it up on your Windows 10 machine, you can refer my post). However this is completely optional and the same should work on other Ubuntu Distros.

Install Redis on Ubuntu

To install Redis on Ubuntu we will launch Ubuntu 18.04 WSL instance on the Windows 10 machine. This step is completely optional and you can also use the same steps on any Ubuntu 18.04 instance.

Before installing Redis we need to install few dependencies. Run following command in the bash to install the dependencies required for running Redis.

sudo apt-get update
sudo apt install make gcc libc6-dev tcl build-essential
Enter fullscreen mode Exit fullscreen mode

Now lets download Redis from the official Redis channel,

cd ~
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
Enter fullscreen mode Exit fullscreen mode

Install Redis build dependencies

cd redis-stable/deps
make hiredis lua jemalloc linenoise
Enter fullscreen mode Exit fullscreen mode

This will take some time, once the above step finishes run make command in the redis-stable directory as below,

cd ~/redis-stable
make
Enter fullscreen mode Exit fullscreen mode

Make command will also take some time to complete, once its complete you can run 'make test' command to see if everything is setup properly. This is a optional step, if the make is completed successfully you can skip it.

Create cluster

There are lot of articles available for setting up the cluster manually. However Redis has a utility to create a cluster quickly which will get you started in minutes. To create cluster run following command,

cd ~/redis-stable/utils/create-cluster
./create-cluster start

Enter fullscreen mode Exit fullscreen mode

The start command will create the cluster with port ranging from 30001 to 30006. The output should look like below,

Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
Enter fullscreen mode Exit fullscreen mode

This will start 6 instances of Redis, now to create a cluster run,

./create-cluster create
Enter fullscreen mode Exit fullscreen mode

This command will create the cluster with 3 master and 3 slaves configuration. It will also display you hash slots for all 3 masters and the slave instance mapping with master instance. Type yes in the prompt to continue.

>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:30005 to 127.0.0.1:30001
Adding replica 127.0.0.1:30006 to 127.0.0.1:30002
Adding replica 127.0.0.1:30004 to 127.0.0.1:30003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 8106f2118a007250a1b003c01e3d84dff87e7117 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
M: 7f0c05048e8864334de8a24de1318327c11a1b8d 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
M: f5d904e68e316d0c310cb7acb4201e5ad9539202 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
S: e5ccc0ac06c1921f70530fa01c62ba2b6de441a3 127.0.0.1:30004
   replicates f5d904e68e316d0c310cb7acb4201e5ad9539202
S: a140fe7519762b1f2c35e06f8e5c46487bad1f6c 127.0.0.1:30005
   replicates 8106f2118a007250a1b003c01e3d84dff87e7117
S: 63d7a31d2820e346eec839470fc3ab5cafd25cc7 127.0.0.1:30006
   replicates 7f0c05048e8864334de8a24de1318327c11a1b8d
Can I set the above configuration? (type 'yes' to accept):
Enter fullscreen mode Exit fullscreen mode

This will create the cluster and you should be able to see output as below,

>>> 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 127.0.0.1:30001)
M: 8106f2118a007250a1b003c01e3d84dff87e7117 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 7f0c05048e8864334de8a24de1318327c11a1b8d 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: f5d904e68e316d0c310cb7acb4201e5ad9539202 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 63d7a31d2820e346eec839470fc3ab5cafd25cc7 127.0.0.1:30006
   slots: (0 slots) slave
   replicates 7f0c05048e8864334de8a24de1318327c11a1b8d
S: e5ccc0ac06c1921f70530fa01c62ba2b6de441a3 127.0.0.1:30004
   slots: (0 slots) slave
   replicates f5d904e68e316d0c310cb7acb4201e5ad9539202
S: a140fe7519762b1f2c35e06f8e5c46487bad1f6c 127.0.0.1:30005
   slots: (0 slots) slave
   replicates 8106f2118a007250a1b003c01e3d84dff87e7117
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Enter fullscreen mode Exit fullscreen mode

Connecting to Redis Cluster

To connect to Redis cluster using redis-cli, open another bash window and cd into the redis-stable directory.

cd ~/redis-stable/src
./redis-cli -c -p 30001
Enter fullscreen mode Exit fullscreen mode

You should now be connected to the Redis cluster. The output will be similar to,

127.0.0.1:30001>
Enter fullscreen mode Exit fullscreen mode

Testing the cluster

To set key run,

set foo bar
Enter fullscreen mode Exit fullscreen mode

To retrieve the value of the key run,

get foo
Enter fullscreen mode Exit fullscreen mode

The get command will show output as "bar".

So your Redis cluster is now setup. If you want to stop your cluster run,

./create-cluster stop
Enter fullscreen mode Exit fullscreen mode

You will notice all the nodes listening from 30001-30006 have stopped.

Stopping 30001
Stopping 30002
Stopping 30003
Stopping 30004
Stopping 30005
Stopping 30006
Enter fullscreen mode Exit fullscreen mode

To delete the cluster run,

./create-cluster stop
./create-cluster clean
Enter fullscreen mode Exit fullscreen mode

Now if you run ls you will notice that the folder contains only two files,

  • README
  • create-cluster

Now to run a new cluster you can run the start and then create command as demonstrated earlier in this article.

Happy coding!

Discussion (0)

pic
Editor guide