Categories
docker

docker-compose redis-cluster

Clone git repository

Updated: June 25, 2020

Clone the repo.

https://github.com/itsmetommy/docker-redis-cluster

cd into repo.

cd docker-redis-cluster

Build and start all containers

docker-compose up --build -d

Example

docker-compose up --build -d
Creating network "docker-redis-cluster_redisnet" with driver "bridge"
Building redis-1
Step 1/7 : FROM redis:latest
---> 63130206b0fa
Step 2/7 : MAINTAINER Tommy Elmesewdy r1tommy@gmail.com
---> Using cache
---> 05eb5ef50d38
Step 3/7 : RUN apt-get update
---> Using cache
---> a927db34871b
Step 4/7 : RUN apt-get install -y gettext-base
---> Using cache
---> 4899e7f5f919
Step 5/7 : COPY redis.conf /etc/redis/redis.conf.template
---> Using cache
---> a8024d70ee9a
Step 6/7 : COPY start.sh /start.sh
---> Using cache
---> 280d4ce7e793
Step 7/7 : ENTRYPOINT [ "/bin/bash", "/start.sh" ]
---> Using cache
---> f76ed653beaa
Successfully built f76ed653beaa
Successfully tagged redis-cluster-node:latest
Creating docker-redis-cluster_redis-3_1 … done
Creating docker-redis-cluster_redis-2_1 … done
Creating docker-redis-cluster_redis-5_1 … done
Creating docker-redis-cluster_redis-1_1 … done
Creating docker-redis-cluster_redis-6_1 … done
Creating docker-redis-cluster_redis-4_1 … done
Creating docker-redis-cluster_redis-cluster_1 … done

View containers

docker ps

Example

docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5cabf429bd43 redis-cluster-node "/bin/bash /start.sh" 6 seconds ago Up 4 seconds 0.0.0.0:7004->6379/tcp docker-redis-cluster_redis-4_1
426dc6b1acc7 redis-cluster-node "/bin/bash /start.sh" 6 seconds ago Up 4 seconds 0.0.0.0:7001->6379/tcp docker-redis-cluster_redis-1_1
48aa90cbefe9 redis-cluster-node "/bin/bash /start.sh" 6 seconds ago Up 4 seconds 0.0.0.0:7002->6379/tcp docker-redis-cluster_redis-2_1
82dce22ae7a8 redis-cluster-node "/bin/bash /start.sh" 6 seconds ago Up 4 seconds 0.0.0.0:7005->6379/tcp docker-redis-cluster_redis-5_1
dbf648f81a28 redis-cluster-node "/bin/bash /start.sh" 6 seconds ago Up 4 seconds 0.0.0.0:7006->6379/tcp docker-redis-cluster_redis-6_1
dbed310da93e redis-cluster-node "/bin/bash /start.sh" 6 seconds ago Up 4 seconds 0.0.0.0:7003->6379/tcp docker-redis-cluster_redis-3_1

Logs

To get a better understanding of how the cluster was created, check out one of the container logs.

docker logs docker-redis-cluster_redis-1_1
docker logs docker-redis-cluster_redis-2_1
docker logs docker-redis-cluster_redis-3_1
docker logs docker-redis-cluster_redis-4_1
docker logs docker-redis-cluster_redis-5_1
docker logs docker-redis-cluster_redis-6_1

Connect to one of the masters

docker exec -it docker-redis-cluster_redis-1_1 redis-cli -c -p 7001

Example

docker exec -it docker-redis-cluster_redis-1_1 redis-cli -c -p 7001
127.0.0.1:7001>

Create keys

127.0.0.1:7001> set mykey1 1
127.0.0.1:7001> set mykey2 2
10.0.0.13:7003> set mykey3 3
10.0.0.12:7002> get mykey1
10.0.0.11:7001> get mykey2
10.0.0.13:7003> get mykey3

Example

127.0.0.1:7001> set mykey1 1
OK
127.0.0.1:7001> set mykey2 2
-> Redirected to slot [14119] located at 10.0.0.13:7003
OK
10.0.0.13:7003> set mykey3 3
-> Redirected to slot [9990] located at 10.0.0.12:7002
OK
10.0.0.12:7002> get mykey1
-> Redirected to slot [1860] located at 10.0.0.11:7001
"1"
10.0.0.11:7001> get mykey2
-> Redirected to slot [14119] located at 10.0.0.13:7003
"2"
10.0.0.13:7003> get mykey3
-> Redirected to slot [9990] located at 10.0.0.12:7002
"3"

View nodes

10.0.0.12:7002> cluster nodes

Example

10.0.0.12:7002> cluster nodes
3383e5eaa3d3989643c9c672b222e946f2bbd9e4 10.0.0.13:7003@17003 master - 0 1586251801471 3 connected 10923-16383
8b7fe89284371a82485f803af8387bb768b2635c 10.0.0.14:7004@17004 slave 3383e5eaa3d3989643c9c672b222e946f2bbd9e4 0 1586251799428 4 connected
b23c10d00b735f70ef8a3b891464952627833888 10.0.0.16:7006@17006 slave d4175fe9d9d96b3963df95fb7ace75e295b31ae3 0 1586251800000 6 connected
c54513f21e761a73314ed32910cba92520545438 10.0.0.15:7005@17005 slave 205763b89ce74a1e250b01e176c81d588495cd7d 0 1586251800445 5 connected
d4175fe9d9d96b3963df95fb7ace75e295b31ae3 10.0.0.12:7002@17002 myself,master - 0 1586251799000 2 connected 5461-10922
205763b89ce74a1e250b01e176c81d588495cd7d 10.0.0.11:7001@17001 master - 0 1586251800000 1 connected 0-5460

View slots

10.0.0.12:7002> cluster slots

Example

10.0.0.12:7002> cluster slots
1) 1) (integer) 10923
2) (integer) 16383
3) 1) "10.0.0.13"
2) (integer) 7003
3) "3383e5eaa3d3989643c9c672b222e946f2bbd9e4"
4) 1) "10.0.0.14"
2) (integer) 7004
3) "8b7fe89284371a82485f803af8387bb768b2635c"
2) 1) (integer) 5461
2) (integer) 10922
3) 1) "10.0.0.12"
2) (integer) 7002
3) "d4175fe9d9d96b3963df95fb7ace75e295b31ae3"
4) 1) "10.0.0.16"
2) (integer) 7006
3) "b23c10d00b735f70ef8a3b891464952627833888"
3) 1) (integer) 0
2) (integer) 5460
3) 1) "10.0.0.11"
2) (integer) 7001
3) "205763b89ce74a1e250b01e176c81d588495cd7d"
4) 1) "10.0.0.15"
2) (integer) 7005
3) "c54513f21e761a73314ed32910cba92520545438"

Clean up

docker-compose down

Example

docker-compose down
Stopping docker-redis-cluster_redis-4_1 … done
Stopping docker-redis-cluster_redis-1_1 … done
Stopping docker-redis-cluster_redis-2_1 … done
Stopping docker-redis-cluster_redis-5_1 … done
Stopping docker-redis-cluster_redis-6_1 … done
Stopping docker-redis-cluster_redis-3_1 … done
Removing docker-redis-cluster_redis-cluster_1 … done
Removing docker-redis-cluster_redis-4_1 … done
Removing docker-redis-cluster_redis-1_1 … done
Removing docker-redis-cluster_redis-2_1 … done
Removing docker-redis-cluster_redis-5_1 … done
Removing docker-redis-cluster_redis-6_1 … done
Removing docker-redis-cluster_redis-3_1 … done
Removing network docker-redis-cluster_redisnet

By Tommy Elmesewdy

Data Janitor