How to Setup & Configure a Redis Cluster Easily

We all know the power of Redis already. During the development phase of a project, as a developer, sometimes we need to do some POC work that includes Redis, but we might not have Redis up and running in our system or servers. So, for that, we either need to wait for the Infrastructure team in organizations or if we are doing it at a small or individual level, we need to set it up on our own.
To save us time thinking about how to install or who can help us in installing this, let’s follow this quick guide to set up Redis, and instead use the time saved in implementation.
There can be various methods for setting up a Redis cluster. One of the easiest and the quickest ways to set up a Redis cluster is discussed below.
Step 1 - Download Docker image
We will first download the docker image. This image can be found at https://hub.docker.com/_/redis/.
Use the following command to pull docker image:
docker pull redisFor cluster, we need six instances of Redis running, out of which three will be Master nodes and the other three will be Slave nodes. (We don’t need to specify them explicitly).
Step 2 - Prepare Redis.conf file
So, we will be creating six folders, each having a ‘redis.conf’ file, that will be used to create docker instances. Use this command to do so:
mkdir 7000 7001 7002 7003 7004 7005Note: These folders are names to identify which port are we going to provide to the Redis node.
Now, in each folder created above, we will add the redis.conf file, which will look like this:
port 7000 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 append-only yes
bind <server-IP>>> A few points to Note:
- Remember to keep this port the same as the folder name. For e.g. In folder 7001, the port will be 7001, in folder 7002, the port will be 7002, and so on.
- This <server-IP> must be replaced by the IP of the server we are using. This is to make our Redis instance accessible from the outside, where that server is also accessible.
We are now ready with our minimal setup for creating our cluster. Let’s start the docker containers.
Step 3 - Start Docker containers
We will be starting the docker containers using the docker image downloaded in step 1. Use the following commands:
docker run -v <folder-path>/7000/redis.conf:/usr/local/etc/redis/redis.conf -d --net=host --name myredis-0 redis redis-server /usr/local/etc/redis/redis.conf
docker run -v <folder-path>/7001/redis.conf:/usr/local/etc/redis/redis.conf -d --net=host --name myredis-1 redis redis-server /usr/local/etc/redis/redis.conf
docker run -v <folder-path>/7002/redis.conf:/usr/local/etc/redis/redis.conf -d --net=host --name myredis-2 redis redis-server /usr/local/etc/redis/redis.conf
docker run -v <folder-path>/7003/redis.conf:/usr/local/etc/redis/redis.conf -d --net=host --name myredis-3 redis redis-server /usr/local/etc/redis/redis.conf
docker run -v <folder-path>/7004/redis.conf:/usr/local/etc/redis/redis.conf -d --net=host --name myredis-4 redis redis-server /usr/local/etc/redis/redis.conf
docker run -v <folder-path>/7005/redis.conf:/usr/local/etc/redis/redis.conf -d --net=host --name myredis-5 redis redis-server /usr/local/etc/redis/redis.confPlease note:
- <folder-path> must be an absolute path.
- There must be proper permissions to read these ‘redis.conf’ files. Permissions can be set using command chmod 755 <file name>. We will need sudo access to set permissions here.
- We have six docker containers running till here. The list of which can be obtained using the command: docker ps | grep redis
Step 4 - Create a cluster using Redis nodes
Now, we will create a Redis cluster using the Redis nodes we have just started. For this, we need redis-cli. Well, this redis-cli is itself present inside the redis nodes we have created.
So, we will open shell from any of the docker containers just created above, using this command:
docker exec -it <container-id> shWe will create the cluster using the command:
redis-cli --cluster create <server-IP>:7000 <server-IP>:7001 <server-IP>:7002 <server-IP>:7003 <server-IP>:7004 <server-IP>:7005 --cluster-replicas 1Please note:
- <server-IP> will be the same as that mentioned in the redis.conf file above.
- — cluster-replicas 1 will create three nodes as master nodes and the other three as their slave nodes.
>> Within a few seconds, if everything is fine, a message will be displayed saying:
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.Our Redis cluster is now up and running on ports 7000, 7002, 7002, 7003, 7004, and 7005.
I hope this quick guide will help you in setting up Redis-cluster in a very short time span. If you face any difficulty in doing this, you can also refer to the official documentation of Redis, or you can comment below for your queries.
Docker is a trademark or registered trademark of Docker, Inc. in the United States and/or other countries.
Author — Prakhar Goel, DLT Labs™
About the Author: Prakhar is a young professional, currently working as a Nodejs developer in our DL Asset Track product team.
Disclaimer: This article was originally published on the DLT Labs Blog page: https://www.dltlabs.com/blog/how-to-setup-configure-a-redis-cluster-easily-573120

