Creating Redis Cluster Using Docker, Ubuntu on VirtualBox


For creating a Redis cluster using this guide make sure you have Ubuntu running on VirtualBox and docker is installed and up. You can check this using the following command. This should display information about installed docker otherwise you need to install it.

:~$ docker version

A Redis cache cluster can be created in several ways and here is the easiest one to create a minimal working cluster.

Steps to create a Redis cluster using Docker

Step 1 – Pull Redis image from docker hub using following cmd

:~$ docker pull redis

For creating a Redis cluster minimum of 3 master nodes are required and we will create 3 slave nodes or replicas for each master.

Step 2 – Now you need to create 6 directories inside a directory to store the config of each node. Note for simplicity we will keep the name as a port of docker instance.

:~$ mkdir cluster-test
:~$ cd cluster-test
:~$ mkdir 7000 7001 7002 7003 7004 7005

Now create redis.conf file in each folder as given below

port <PORT>
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
bind <Server-IP>

Make sure to change PORT according to the folder name and Server-IP with server IP running on VirtualBox. After this, we can create our minimal Redis cluster.

Step 3 – To Run docker images create one shell script as given below and make sure redis.conf path is an absolute path.

docker run -v /home/c4c/cluster-test/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 /home/c4c/cluster-test/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 /home/c4c/cluster-test/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 /home/c4c/cluster-test/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 /home/c4c/cluster-test/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 /home/c4c/cluster-test/7005/redis.conf:/usr/local/etc/redis/redis.conf -d --net=host --name myredis-5 redis redis-server /usr/local/etc/redis/redis.conf

Save the above sh file named start-nodes.sh and make it executable using chmod and execute the file

$ sudo chmode +x start-nodes.sh
$ ./start-nodes.sh

It will download and start 6 docker instances, you verify using the docker container list command

$ docker container ls

CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
c832d47d8793   redis     "docker-entrypoint.s…"   7 minutes ago   Up 7 minutes             myredis-5
a799c34458ea   redis     "docker-entrypoint.s…"   7 minutes ago   Up 7 minutes             myredis-4
583ec13d9b72   redis     "docker-entrypoint.s…"   7 minutes ago   Up 7 minutes             myredis-3
d9ce0c11676a   redis     "docker-entrypoint.s…"   7 minutes ago   Up 7 minutes             myredis-2
e4efc98e3e4e   redis     "docker-entrypoint.s…"   7 minutes ago   Up 7 minutes             myredis-1
a4e6c27bb22c   redis     "docker-entrypoint.s…"   7 minutes ago   Up 7 minutes             myredis-0

Step 4 – Now we need to create a cluster, we can do this by executing the command on any of the nodes. To get shell access of a docker run the following command. Change container id in below command with yours.

$ docker exec -it <container-id> sh

Above will open the shell of that container.

Now we can create the Redis cluster using redis-cli, replace server-IP with your server IP.

$ redis-cli --cluster create <server-IP>:7000 <server-IP>:7001 <server-IP>:7002 <server-IP>:7003 <server-IP>:7004 <server-IP>:7005 --cluster-replicas 1

The above command will create a cluster and you can see the following output.

>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.1.29:7004 to 192.168.1.29:7000
Adding replica 192.168.1.29:7005 to 192.168.1.29:7001
Adding replica 192.168.1.29:7003 to 192.168.1.29:7002
...

Now our Redis cluster is running on ports 7000, 7002, 7002, 7003, 7004, and 7005.

Step 5 – To test cluster, install redis-cli only on Ubuntu host using the following command.

$ sudo apt-get install redis-tools

Now you can connect to Redis using the following command

$ redis-cli -h 192.168.1.29 -p 7000

and check redis working with redis-cli using the following simple incr counter

192.168.1.29:7000> incr mycounter
(integer) 1
192.168.1.29:7000> incr mycounter
(integer) 2


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.