Set Up Redis Cluster on 1&1 Cloud Servers

Table of Contents

Introduction

Redis is a popular open source in-memory data structure store which is used as an alternative to SQL and relational databases. Using Redis Cluster to spread the processing over multiple servers greatly increases the throughput and stability of a Redis installation. Redis Cluster automatically shards data across multiple servers, to reduce the possible points of failure.

Requirements

  • At least two 1&1 Cloud Servers with Redis 3.0+ installed.

Redis Cluster is only supported by Redis versions 3.0 and above. For step-by-step instructions to install Redis, see our article Installing Redis on a 1&1 Linux Cloud Server.

Note: The official documentation recommends beginning with six servers in a Redis cluster: three manager and three worker nodes. For the purpose of this tutorial, we will use three servers: one manager and two worker nodes.

Firewall Access

You will need to allow access for TCP connections on two ports:

  • The normal Redis TCP data port: 6379
  • The cluster port, which is the TCP data port plus 10,000: 16379

Feel free to customize the Redis TCP data port number. However, the cluster port offset is fixed, and is always 10,000.

For more information about creating and modifying the 1&1 Cloud Server Firewall Policy, see using a firewall on a 1&1 Cloud Server, see our article Configure the Firewall Policy.

Configure the Servers

The /etc/redis/redis.conf file will need to be configured for each server in the cluster. This file contains all of the configurations for the cluster, and you will want to customize many of them to improve security and suit your needs.

See the official Redis Cluster documentation for an explanation of the most commonly-used configuration parameters. For this tutorial, we will only be updating the minimum needed to get the cluster up and running.

Configure the Manager Server(s)

Connect to the manager server with SSH, then open the /etc/redis/redis.conf file for editing:

sudo nano /etc/redis/redis.conf

To make the server accessible to the public internet, find the section:

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1

Add a # to disable this directive so that it reads:

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#bind 127.0.0.1

Next, set a password. Find the section:

# requirepass foobared

Remove the # to enable this directive, and change foobared to the secure Redis password. For example, to set Wg1Or0H32z as the password, change this directive to read:

requirepass Wg1Or0H32z

Save and exit the file. Restart Redis for the changes to take effect:

sudo systemctl restart redis.service

Repeat these steps for each manager server in the cluster.

Configure the Worker Server(s)

Connect to the worker server with SSH, then open the /etc/redis/redis.conf file for editing:

sudo nano /etc/redis/redis.conf

To make the server accessible to the public internet, find the section:

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1

Add a # to disable this directive so that it reads:

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#bind 127.0.0.1

Add the manager server's connection information. Find the section:

# slaveof <masterip> <masterport>

Remove the # to enable this directive, and add the manager server's IP address and TCP data port number:

slaveof 192.168.0.1 6379

Add the manager server's password. Find the section:

# masterauth <master-password>

Remove the # to enable this directive, and add the manager server's Redis password. For example, if the manager server's Redis password is Wg1Or0H32z, change this directive to read:

masterauth Wg1Or0H32z

Next, set a password. Find the section:

# requirepass foobared

Remove the # to enable this directive, and change foobared to the worker server's secure Redis password. For example, to set Pb6wkj128M as the worker server's password, change this line to read:

requirepass Pb6wkj128M

Save and exit the file. Restart Redis for the changes to take effect:

sudo systemctl restart redis.service

Repeat these steps for each worker server in the cluster.

Verify the Cluster

After all of the servers have been configured, connect to the manager server with SSH to verify that the cluster is running. Log in to Redis:

redis-cli -h 127.0.0.1 -p 6379

This will log you in to the Redis command-line client.

Authenticate with the manager server's Redis password:

AUTH [password]

For example, if the manager server's Redis password is Wg1Or0H32z, the command is:

AUTH Wg1Or0H32z

The server will reply:

OK

Query Redis for the system's status:

INFO

This will output information about the Redis installation. Look for the Replication section:

# Replication
role:master
connected_slaves:2
slave0:ip=192.168.0.1,port=6379,state=online,offset=532,lag=0
slave1:ip=192.168.0.2,port=6379,state=online,offset=532,lag=0
master_replid:f525b02b0b2548b6946cf11df78df19cdb0300cd
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:532
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:532

In the above example, the Redis cluster is set up and running correctly, with two worker ("slave") nodes, and their IP addresses.

Comments

Tags: Redis