Edited 2 days ago by ExtremeHow Editorial Team
RedisLinuxClusterConfigurationSetupDistributed SystemHigh AvailabilityScalabilityDatabaseServer
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.
First, you must ensure that Redis is installed on all nodes participating in the cluster. Use the following command depending on your Linux distribution:
sudo apt update sudo apt install redis-server
sudo yum install epel-release sudo yum install redis
Start and enable the Redis service:
sudo systemctl start redis sudo systemctl enable redis
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:
bind 0.0.0.0
to allow connections from outside localhost.protected-mode
to no
.cluster-enabled
and set it to yes
.cluster-config-file nodes-6379.conf
to specify the file name for the cluster configuration.cluster-node-timeout 5000
, which is the time period after which the node is considered down.appendonly
comment and set it to yes
for data durability.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
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
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.
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.
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.
Once your cluster is up and running, you may wonder how to perform regular operations. Here are some useful commands for cluster management:
To add a new node to the cluster:
redis-cli --cluster add-node newnodeip:port existingnodeip:port
To remove a node from the cluster:
redis-cli --cluster del-node currentnodeip:port <node_id>
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
Some best practices for ensuring stability and performance in operating a Redis cluster include:
redis.conf
.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