Suyash Srivastava
Sep 5, 2018 · 3 min read

Redis Cluster setup

This post aims to help setting up a minimal redis cluster on *nix environment.

A minimal test cluster can be created on the same** machine. This tutorial uses redis-trib.rb script to create cluster. Make sure that ruby is installed on your machine. You need to install redis gem to be able to run redis-trib

$ gem install redis

redis-trib.rb can be located in redis-stable/src/

Please follow the below steps to create a test cluster with minimal config:

$ mkdir cluster_test
$ cd cluster_test && mkdir 6380 6381 6382 6383 6384 6385

These directories are numbered to identify separate redis instances running on these ports.

Why 6 directories? We will set up 3 node cluster with replication factor 1, which means 1 master - 1 slave.

You can fire up a 3 node cluster with no replication, that means you need 3 directories instead of 6

Go to each numbered directory and create a minimal redis.conf with the following configuration You can experiment with different config later.

$ cd 6380

$ vim config.py

Paste the below configuration

port 6380

cluster-enabled yes

cluster-config-file nodes.conf

cluster-node-timeout 5000

appendonly yes

Go to all other directories and create config as above, replace the port number with correct directory number

Open 6 terminals and start redis server with the above configurations created.

$ redis-server ./config.py

Open another terminal and go to redis-stable directory. Execute the following:

$ ./src/redis-trib.rb create --replicas 1 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385

If the above script is going in a infinite wait while Waiting for the cluster to join.. please check if the above ports are added in the list of open ports in iptables. Also allow Redis Cluster Bus port (1000 + tcp port) in iptables. Even after this if you face the same problem, start redis server as:

redis-server ./redis.conf --protected-mode no

or, add the following in the config file:

protected-mode no

At the end of the ./src/redis-trib.rb execution, you will see the following:

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

To test the cluster, you can start redis-cli with any of the ablove ports check output of the following command:

127.0.01:6380> CLUSTER NODES

The output will contain information about connected nodes. Try doing some get set to verify the cluster.

once you start the cluster, another conf file node.conf will be automatically created. This file contains information about each node. Each line is composed of the following fields:

<id> <ip:port> <flags> <master> <ping-sent> <pong-recv> <config-epoch> <link-state> <slot> <slot> ... <slot>

** If you are setting up cluster with remote machines, please ensure all ports are allowed in list of iptables on all machines, and ensure redis-servers are running in --protected-mode no. By default, redis-trib doesn't recognize hostnames, so please execute redis-trib as follows if you are using hostnames to start the cluster:

./src/redis-trib.rb create --replicas 1 `host hostname1.com | cut -d ' ' -f 4`:6380 `host hostname2.com | cut -d ' ' -f 4`:6381 `host hostname3.com | cut -d ' ' -f 4`:6382 `host hostname4.com | cut -d ' ' -f 4`:6383 `host hostname5.com | cut -d ' ' -f 4`:6384 `host hostname6.com | cut -d ' ' -f 4`:6385