How to Install and Configure a Redis Cluster on Ubuntu 16.04-Devlabs & Wiki

Redis clusters have grown to be a popular tool for caches, queues, and more because of its potential for scalability and speed. This guide aims to create a cluster using three Linodes to demonstrate sharding. Then, you will promote a slave to a master – insurance, in the event of a failure.

Redis as an in-memory store allows for extremely fast operations such as counting, caching, queuing, and more. A cluster setup greatly increases the reliability of Redis by reducing the point of failures.

Prior to starting, we recommend you familiarize yourself with the following:

Install Redis on Each Linode

Depending on your version of Linux, it may be possible to install Redis through a package manager. Only Redis 3.0 and above supports clustering. The steps below are for installation of the latest stable branch of Redis.

  1. Install the dependencies:{{< note >}} Alternatively, you could use build-essential to load the dependencies for Redis. {{< /note >}}
  2. From the documentation, download the current stable branch, then extract:
  3. Ensure the installation is successful by running:If installation is successful, the console will output:\o/ All tests passed without errors!
  4. Repeat installation for each and every server that will be part of the cluster.

Configure Master and Slave Nodes

This guide manually connects each master and slave across three Linodes. Consider using tmux for management of multiple terminal windows.

Although the official documentation recommends creating six nodes, this guide will use the minimum of three nodes with the following topology:

How to Install and Configure a Redis Cluster on Ubuntu 16.04-Devlabs & Wiki

This setup uses three Linodes running two instances of Redis server per Linode. You must ensure each host is independent, and then consider using additional nodes if there is a need to maintain uptime requirements.

  1. SSH into server 1. Navigate to redis-stable/ then copy redis.conf. Configuration files in this guide are named consistent with the figure above:
  2. In a_master.conf, comment the bind directive and enable cluster mode. The ports in this example will range from 6379 to 6381.{{< file “/redis-stable/a_master.conf” >}}


protected-mode no port 6379 pidfile /var/run/ cluster-enabled yes cluster-config-file nodes-6379.conf cluster-node-timeout 15000

{{< /file >}}

A node in the Redis cluster requires a defined port and a port higher than 10000. In this instance, TCP ports 6379 and 16379 are both required to be open. Ensure iptables or ufw is configured properly. {{< /caution >}}

  1. In c_slave.conf, the configuration will be similar except for an update of the port number. redis-trib.rb will be used later to configure this into a slave for the appropriate master, rather than the slaveof directive.{{< file “/redis-stable/c_slave.conf” >}}

bind 127.00.1

protected-mode no port 6381 pidfile /var/run/ cluster-enabled yes cluster-config-file nodes-6381.conf cluster-node-timeout 15000

{{< /file >}}

  1. Repeat this process across the remaining two Linodes, taking care to specify the port numbers for all master slave pairs.ServerMasterSlave163796381263806379363816380

Connect Master and Slave

Master/slave replication can be achieved across three nodes by running two instances of a Redis server on each node.

  1. SSH into server 1 and start the two Redis instances.
  2. Substitute a_master.conf and c_slave.conf with the appropriate configuration file for the remaining two servers. All the master nodes should be starting in cluster mode.{{< file “Server 1” >}} . .-__ ''-._ _.- .  . ”-._ Redis 4.0.1 (00000000/0) 64 bit .- .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in cluster mode |`-._`-...-` __...-.-.|’_.-'| Port: 6379 |  -. ._ / _.-' | PID: 10352 -._  -._  -./  .-‘  .-‘ |-._-. -.__.-' _.-'_.-'| |  -.-._ _.-'_.-' | -._  -._-..-‘.-‘  .-‘ |-._-. -.__.-' _.-'_.-'| |  -.-._ _.-'_.-' | -._  -._-..-‘_.-‘ _.-‘ -._  -..-‘ _.-‘ -._ _.-' -..-‘

{{< /file >}}

Create Cluster Using Built-In Ruby Script

At this point, each Linode hosts two independent master nodes. The Redis installation comes with a Ruby script located in ~/redis-stable/src/ that can help create and manage a cluster.

  1. If Ruby is not already installed, it can be found in your package manager:
  2. Install the Redis gem:
  3. Navigate to the source directory to run the script.
  4. Accept the configuration with three masters. Successful set up of the cluster will return the following message:
  5. See all the current nodes connected to the cluster by using the redis-cli tool. The -c flag specifies connection to the cluster.This will return a list of nodes currently in the cluster identified by their i.d. and slaves – if any exist. To leave the interface, click on exit.{{< note >}} Redis keywords are not case sensitive. However, they are written as all capitals in this guide for clarity. {{< /note >}}

Add Slaves

The redis-trib tool can also be used to add new nodes to the cluster. Using the remaining three nodes, you can manually add them to the selected master.

  1. Connect the slave to a given master using add-note and a specified master_id.The resulting output should be:
  2. Repeat for the remaining two nodes.

Add Key-Value Pairs and Sharding

The command line interface offers a way to SET and GET keys, in addition to returning information about the cluster. On your local computer, you can connect to any of the master nodes and explore some properties of a Redis cluster.

  1. Repeat the installation of Redis on your local computer, if needed. Check that firewall settings allow communicating with the master nodes.
  2. Use the CLUSTER INFO command to see information about the state of the cluster such as size, hash slots, and failures, if any.
  3. To check master/slave replication, INFO replication returns information about the slave.
  4. To demonstrate sharding, you can set a few example key-value pairs. Setting a key will redirect the value to a hash slot among the three master nodes.

Promote Slave to Master

Based on the current topology, the cluster will remain online if one of the Linodes fails. At that point, you can expect a slave to promote into a master with the data replicated.

How to Install and Configure a Redis Cluster on Ubuntu 16.04-Devlabs & Wiki
  1. Add a key value pair.The key foo is added to a master on server 3 and replicated to a slave on server 1.
  2. In the event server 3 is down, the slave on server 1 will become a master and the cluster will remain online.
  3. A key that was previously located in a hash slot on server 3, the key value pair is now stored on server 1.

Remember to ensure firewall settings are adequate for all Redis instances. There is supplemental functionality, such as adding additional nodes, creating multiple slaves, or resharding, which are beyond the scope of this document. For more guidance, consult the official Redis documentation for how to implement these features.