I have been working with Redis lately and in my local development environment I was just testing against a single node setup of Redis. However, this did not give me the functionality I wanted to test against since our production environment uses a Redis cluster of six nodes.
Connecting to a Redis cluster requires a different configuration from a single Redis instance and is also functionally different. For example, a standard six node cluster of Redis is actually three pairs of Master-Replica where each pair stores a third of the data. So for those reasons I really wanted to test my application setup against a Redis cluster in my local development environment, which was Ubuntu.
Note: At the the time of this post I was using Ubuntu 16.04 and Redis 4.0.9.
So here is how to spin up a six node test cluster of Redis on a single Ubuntu instance.
Download and Build Redis
wget http://download.redis.io/releases/redis-4.0.9.tar.gz
tar -zxvf redis-4.0.9.tar.gz
cd redis-4.0.9
make
Create Six Redis Nodes
First, edit the create-cluster startup file:
cd utils/create-cluster
vi create-cluster
In my setup, I changed the following:
- Port – set to 7000 (This will be the starting port)
- Timeout – set to 10000 (I found this handy in my dev enivornment)
- Nodes – set to 6 (We use a 6 node cluster in production, so I wanted the same)
- Replicas – set to 1 (This means each master with have one replica, 6 nodes = 3 Masters and 3 Replicas)
Now we can start all six nodes:
./create-cluster start
You will see the nodes have been started:
If you do an “ls” in the folder you will notice that some files have been created:
- Each node has a log file (For example: 7001.log)
- Each node has a cluster configuration file (For example: nodes-7001.conf)
You can also confirm the nodes are running by connecting to a node using the “redis-cli” command line tool by specifying the port of a node:
cd redis-4.0.9/src
./redis-cli -p 7001
Join the Nodes as a Cluster
Even though at this stage we have six Redis nodes, they know nothing about each other. So the next step is to join these nodes as a cluster.
Prerequisites: Ruby and Redis Gem
The script that connects the nodes as a cluster is written in Ruby and requires the Redis Gem, which requires Ruby 2.2.2 or higher.
Update Ruby
sudo apt-get install ruby-full
Install Redis Gem
sudo gem install redis
Create the Cluster
To create a cluster where you only need to connect to it from the local machine you can use:
cd redis-4.0.9/src
./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
NOTE: If you plan to connect to your Redis cluster remotely, you will need to use the public IP of the machine instead of 127.0.0.1.
When you run the join cluster script it will show you the generated configuration and then ask you to confirm the setup:
After you answer “yes” and press enter, the nodes will be joined in a cluster. If you see the following the cluster creation was successful:
You now have a functional Redis Cluster that you can connect to from your application!
For more information on Redis Clusters I recommend reading the Redis Cluster Tutorial.
I hope that helps!