How to Quickly Create and Easily Configure a Local Redis Cluster
This tutorial talks about how to quickly create and easily configure a local Redis cluster for testing or troubleshooting with minimal modifications and more control.
Join the DZone community and get the full member experience.
Join For FreeContext
Do you crave hands-on experience with Redis clusters? Perhaps you're eager to learn its intricacies or conduct targeted testing and troubleshooting. A local Redis cluster empowers you with that very control. By setting it up on your own machine, you gain the freedom to experiment, validate concepts, and delve deeper into its functionality. This guide will equip you with the knowledge to quickly create and manage a Redis cluster on your local machine, paving the way for a productive and insightful learning journey.
Install Redis
The first step would be to install a Redis server locally. Later cluster creation commands will use Redis instances as building blocks and combine them into a cluster.
Mac
The easiest way would be to install using Homebrew. Use the following command to install Redis on your Macbook.
brew install redis
Linux
Use the following command to install.
sudo apt update
sudo apt install redis-server
From the Source
If you need a specific version then you can use this method of installation. For this, you can use the following steps:
- Download the latest Redis source code from the official website.
- Unpack the downloaded archive.
- Navigate to the extracted directory in your terminal.
- Run the following commands:
make
sudo make install
Create Cluster
One Time Steps
- Clone the git repository
- Go to the directory where you cloned the repository
- Then go to the following directory
cd <path to local redis repository>/redis/utils/create-cluster
- Modify create-cluster with the path to your Redis-server
vi create-cluster
Replace BIN_PATH="$SCRIPT_DIR/../../src/"
with BIN_PATH="/usr/local/bin/"
Steps to Create/Start/Stop/Clean Cluster
These steps are used whenever you need to use a Redis Cluster.
Start the Redis Instances
./create-cluster start
Create the Cluster
echo "yes" | ./create-cluster create
Tip
You can create an alias and add it to the shell configuration files (~/.bashrc or ~./zshrc
)
Example:
open ~/.zshrc
Add the following to this file.
alias cluster_start="./create-cluster start && echo "yes" | ./create-cluster create"
Open a new terminal and run the following.
source ~/.zshrc
Now you use “cluster_start
” in the command line and it will start and create the cluster for you.
Stop the Cluster
./create-cluster stop
Clean Up
Clears previous cluster data for a fresh start.
./create-cluster clean
Tip
Similarly, you can create an alias as below to stop the cluster and clean the cluster data files.
alias cluster_stop="./create-cluster stop && ./create-cluster clean”
How To Create the Cluster With a Custom Number of Nodes by Default
By default cluster-create script creates 6 nodes with 3 primaries and 3 replicas. For some special testing or troubleshooting if you need to change the number of nodes you can modify the script instead of manually adding nodes.
vi create-cluster
Edit the following to the desired number of nodes for the cluster.
NODES=6
Also, by default, it creates 1 replica for a primary. You can change that as well by changing the value in the same script (create-cluster) to the desired value.
REPLICAS=1
Create Cluster With Custom Configuration
Redis provides various options to customize the configuration to configure Redis servers the way you want. All those are present in the redis.conf file. In order to customize those with the desired options follow these steps:
Edit the redis.conf
With Desired Configurations
cd <path to local redis repository>/redis/redis.conf
Edit the create-cluster
Script
vi create-cluster
Modify the command in the start and restart options of the script to add the following
../../redis.conf
Before Modification
$BIN_PATH/redis-server --port $PORT --protected-mode $PROTECTED_MODE --cluster-enabled yes --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --appenddirname appendonlydir-${PORT} --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --daemonize yes ${ADDITIONAL_OPTIONS}
After Modification
$BIN_PATH/redis-server ../../redis.conf --port $PORT --protected-mode $PROTECTED_MODE --cluster-enabled yes --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --appenddirname appendonlydir-${PORT} --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --daemonize yes ${ADDITIONAL_OPTIONS}
For reference please see the snippet below after modification in the start option:
References
Opinions expressed by DZone contributors are their own.
Comments