VOOZH about

URL: https://dzone.com/articles/quickly-create-and-configure-a-local-redis-cluster

⇱ Quickly Create and Configure a Local Redis Cluster


Related

  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. How to Quickly Create and Easily Configure a Local Redis Cluster

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.

By May. 24, 24 · Tutorial
Likes
Comment
Save
6.1K Views

Join the DZone community and get the full member experience.

Join For Free

Context

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.

Shell
brew install redis


Linux

Use the following command to install.

Shell
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:
Shell
make
sudo make install


Create Cluster

One Time Steps

Shell
cd <path to local redis repository>/redis/utils/create-cluster


  • Modify create-cluster with the path to your Redis-server
Shell
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

Shell
./create-cluster start


Create the Cluster 

Shell
echo "yes" | ./create-cluster create


Tip

You can create an alias and add it to the shell configuration files (~/.bashrc or ~./zshrc)

Example:

Shell
open ~/.zshrc


Add the following to this file.

Shell
alias cluster_start="./create-cluster start && echo "yes" | ./create-cluster create"


Open a new terminal and run the following.

Shell
source ~/.zshrc


Now you use “cluster_start” in the command line and it will start and create the cluster for you.

Stop the Cluster

Shell
./create-cluster stop


Clean Up 

Clears previous cluster data for a fresh start.

Shell
./create-cluster clean


Tip

Similarly, you can create an alias as below to stop the cluster and clean the cluster data files.

Shell
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.

Shell
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

Shell
cd <path to local redis repository>/redis/redis.conf


Edit the create-cluster Script

Shell
vi create-cluster


Modify the command in the start and restart options of the script to add the following 

../../redis.conf

Before Modification

Shell
$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

Shell
$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

GitHub Redis

Redis.git

cluster Redis (company) Linux (operating system)

Opinions expressed by DZone contributors are their own.

Related

  • Techniques for Chaos Testing Your Redis Cluster
  • How To Manage Redis Cluster Topology With Command Line
  • OpenShift Container Platform 4.11 Cluster Setup
  • Stateless JWT Auth Microservice Architecture With Spring Boot 3 and Redis Sentinel

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

Let's be friends: