Categories
docker

docker-compose redis-cluster

Clone git repository

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

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

cd into repo.

cd docker-redis-cluster

Build and start all containers

docker-compose up --build -d

View containers

docker ps

Logs

To get a better understanding of how the cluster was created, check out the redis-1 logs.

redis-1 logs

docker-compose logs redis-1

Create Cluster

docker exec -it redis-1 redis-cli -p 7000 --cluster create 10.0.0.2:7000 10.0.0.3:7001 \
10.0.0.4:7002 10.0.0.5:7003 10.0.0.6:7004 10.0.0.7:7005 \
--cluster-replicas 1

Example

Note: Type yes to “Can I set the above configuration?”

docker exec -it redis-1 redis-cli -p 7000 --cluster create 10.0.0.2:7000 10.0.0.3:7001 10.0.0.4:7002 10.0.0.5:7003 10.0.0.6:7004 10.0.0.7:7005 --cluster-replicas 1
               Performing hash slots allocation on 6 nodes…
       Master[0] -> Slots 0 - 5460
       Master[1] -> Slots 5461 - 10922
       Master[2] -> Slots 10923 - 16383
       Adding replica 10.0.0.6:7004 to 10.0.0.2:7000
       Adding replica 10.0.0.7:7005 to 10.0.0.3:7001
       Adding replica 10.0.0.5:7003 to 10.0.0.4:7002
       M: 4b8e584f7518a0efd420eb39e7df0bbff5967a09 10.0.0.2:7000
          slots:[0-5460] (5461 slots) master
       M: ddc890fc4ebb5bd9cb6666ba1c7fdbf557bd4234 10.0.0.3:7001
          slots:[5461-10922] (5462 slots) master
       M: c3ae75db69fbcd71df93d326a12d766408285a6e 10.0.0.4:7002
          slots:[10923-16383] (5461 slots) master
       S: 64cc8a958ca6ee88ad51499f9d1f28dc30525afb 10.0.0.5:7003
          replicates c3ae75db69fbcd71df93d326a12d766408285a6e
       S: 693870037eacb7efdf130efff47f41591dd1f080 10.0.0.6:7004
          replicates 4b8e584f7518a0efd420eb39e7df0bbff5967a09
       S: 1655e3e38210f721b20f95a4b89fec068675deb7 10.0.0.7:7005
          replicates ddc890fc4ebb5bd9cb6666ba1c7fdbf557bd4234
       Can I set the above configuration? (type 'yes' to accept): yes

Connect to one of the masters

Note: -c Enable cluster mode (follow -ASK and -MOVED redirections)

docker exec -it redis-1 redis-cli -c -p 7000

Create keys

127.0.0.1:7000> set mykey1 1
OK
127.0.0.1:7000> set mykey2 2
-> Redirected to slot [14119] located at 10.0.0.4:7002
OK
10.0.0.4:7002> set mykey3 3
-> Redirected to slot [9990] located at 10.0.0.3:7001
OK
10.0.0.3:7001> get mykey1
-> Redirected to slot [1860] located at 10.0.0.2:7000
"1"
10.0.0.2:7000> get mykey2
-> Redirected to slot [14119] located at 10.0.0.4:7002
"2"
10.0.0.4:7002> get mykey3
-> Redirected to slot [9990] located at 10.0.0.3:7001
"3"
10.0.0.3:7001>

Error warning

If you do not use the -c option to connect to the cluster, you will run into the following errors.

docker exec -it redis-1 redis-cli -p 7000
127.0.0.1:7000> set mykey1 1
OK
127.0.0.1:7000> set mykey2 2
(error) MOVED 14119 10.0.0.4:7002
127.0.0.1:7000> set mykey3 3
(error) MOVED 9990 10.0.0.3:7001
127.0.0.1:7000> get mykey1
"1"
127.0.0.1:7000> get mykey2
(error) MOVED 14119 10.0.0.4:7002

View nodes

127.0.0.1:7000> cluster nodes

Example

127.0.0.1:7000> cluster nodes
43b864b848a5d1731ce6e37a4b3bb315c9584903 10.0.0.6:7004@17004 slave 9f0e1bd9ec36a0dd6a72637fecb51c78af4ca8d9 0 1527206692231 5 connected
9f0e1bd9ec36a0dd6a72637fecb51c78af4ca8d9 10.0.0.2:7000@17000 myself,master - 0 1527206690000 1 connected 0-5460
51f892a2dce483b2121c7b5dfb07df5adc7c38ec 10.0.0.4:7002@17002 master - 0 1527206691624 3 connected 10923-16383
65af6640288482d4c12758885f2b1fdd2ab45384 10.0.0.3:7001@17001 master - 0 1527206692000 2 connected 5461-10922
5d8956bd8960ce14fb4958a61b2ada436c80c9e1 10.0.0.7:7005@17005 slave 65af6640288482d4c12758885f2b1fdd2ab45384 0 1527206690198 6 connected
5d274177a8408781e3977329cd839751883a19a9 10.0.0.5:7003@17003 slave 51f892a2dce483b2121c7b5dfb07df5adc7c38ec 0 1527206691217 4 connected

View slots

127.0.0.1:7000> cluster slots

Example

127.0.0.1:7000> cluster slots
1) 1) (integer) 0
   2) (integer) 5460
   3) 1) "10.0.0.2"
      2) (integer) 7000
      3) "9f0e1bd9ec36a0dd6a72637fecb51c78af4ca8d9"
   4) 1) "10.0.0.6"
      2) (integer) 7004
      3) "43b864b848a5d1731ce6e37a4b3bb315c9584903"
2) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "10.0.0.4"
      2) (integer) 7002
      3) "51f892a2dce483b2121c7b5dfb07df5adc7c38ec"
   4) 1) "10.0.0.5"
      2) (integer) 7003
      3) "5d274177a8408781e3977329cd839751883a19a9"
3) 1) (integer) 5461
   2) (integer) 10922
   3) 1) "10.0.0.3"
      2) (integer) 7001
      3) "65af6640288482d4c12758885f2b1fdd2ab45384"
   4) 1) "10.0.0.7"
      2) (integer) 7005
      3) "5d8956bd8960ce14fb4958a61b2ada436c80c9e1"

Clean up

docker-compose down
Comments

By Tommy Elmesewdy

DevOps Engineer