WindowsMacSoftwareSettingsSecurityProductivityLinuxAndroidPerformanceConfigurationApple All

How to Set Up Redis Cluster on Linux

Edited 2 days ago by ExtremeHow Editorial Team

RedisLinuxClusterConfigurationSetupDistributed SystemHigh AvailabilityScalabilityDatabaseServer

How to Set Up Redis Cluster on Linux

This content is available in 7 different language

Redis is a popular in-memory data store that can be used as a database, cache, and message broker. It supports data structures such as strings, hashes, lists, sets, and more. Redis Cluster provides a way to run a Redis installation where data is automatically partitioned across multiple Redis nodes. This tutorial will guide you through the steps to set up a Redis cluster on a Linux machine.

Prerequisites

Step 1: Install Redis on all nodes

First, you must ensure that Redis is installed on all nodes participating in the cluster. Use the following command depending on your Linux distribution:

For Ubuntu:

sudo apt update sudo apt install redis-server

For CentOS:

sudo yum install epel-release sudo yum install redis

Start and enable the Redis service:

sudo systemctl start redis sudo systemctl enable redis

Step 2: Configure Redis for cluster mode

On each Redis node, you must configure the Redis instance for cluster mode. To achieve this, you must edit /etc/redis/redis.conf file.

Locate and modify the following configurations:

An example section of a redis.conf file might look like this:

bind 0.0.0.0 protected-mode no port 6379 cluster-enabled yes cluster-config-file nodes-6379.conf cluster-node-timeout 5000 appendonly yes

After saving the changes, restart the Redis service to apply the new configuration:

sudo systemctl restart redis

Step 3: Create configuration directories

Create directories for the cluster's nodes' configuration and data. For simplicity, let's assume the path is /var/lib/redis/ . Repeat this for each node:

sudo mkdir -p /var/lib/redis/6379 sudo chown redis:redis /var/lib/redis/6379

Step 4: Open the required ports

Redis cluster needs to keep some ports open for communication. For each node, the following ports must be open in the firewall:

Use iptables or the cloud provider's firewall settings to allow these ports.

Step 5: Set up the cluster

Next, we will connect all the configured Redis instances to form a cluster. Use redis-cli tool for this purpose. On any node, execute the following command:

redis-cli --cluster create 192.168.1.1:6379 192.168.1.2:6379 192.168.1.3:6379 192.168.1.4:6379 192.168.1.5:6379 --cluster-replicas 1

Replace the IP addresses with the IP addresses of your nodes. --cluster-replicas 1 option ensures that each master node has one replica. This command will prompt you for confirmation before allocating masters and slaves as part of the cluster configuration.

Step 6: Test the cluster

Once the cluster is set up, you should verify its configuration by connecting to it using any node with redis-cli tool:

redis-cli -c -p 6379

To verify the cluster status, you can run the command:

cluster nodes

This output will display all nodes in your cluster with their roles (master or slave), IDs, and IP addresses.

Step 7: Managing the cluster

Once your cluster is up and running, you may wonder how to perform regular operations. Here are some useful commands for cluster management:

Adding new nodes

To add a new node to the cluster:

redis-cli --cluster add-node newnodeip:port existingnodeip:port

Deleting nodes

To remove a node from the cluster:

redis-cli --cluster del-node currentnodeip:port <node_id>

Migrating slots

Redis Cluster uses hash slots to determine where data will be stored. Sometimes, you may want to reallocate slots from one node to another:

redis-cli --cluster reshard currentnodeip:port

Best practices

Some best practices for ensuring stability and performance in operating a Redis cluster include:

Conclusion

Setting up a Redis cluster on Linux involves installing Redis on multiple nodes, configuring each instance for cluster mode, and using the CLI tool to connect them together. Although this guide provides a comprehensive walkthrough, regular management and monitoring activities will be required to achieve optimal performance and stability. By following this guide, you have now taken an important step towards using Redis Cluster and its benefits for distributed data management.

If you find anything wrong with the article content, you can


Comments