์์์ Redis cluster๋ฅผ ๊ตฌ์ฑํด๋ณด์๋ค.
Redis cluster๊ตฌ์ฑ์ ์ ๋ณด๊ฐ ํ์ํ๋ค๋ฉด ๋ค์ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ๋ฉด ๋๋ค.
๋ณธ ์ฅ์์๋ ์ค์ ๋ฐ์ดํฐ์ ์ ์ฅ๊ณผ ๋
ธ๋ ์ถ๊ฐ, ์ฅ์ ์๋ฎฌ๋ ์ด์
๋ฑ์ ์์๋ณผ ๊ฒ์ด๋ค.
ํ ์คํธ๋ฅผ ์ํด 3๊ฐ์ master๋ก ์ด๋ฃจ์ด์ง cluster๋ฅผ ๊ตฌ์ฑํ์๋ค.
node | container | port |
---|---|---|
1 | redis-master-1 | 7001 |
2 | redis-master-2 | 7002 |
3 | redis-master-3 | 7003 |
$ docker exec -it redis-master-1 bash
$ redis-cli -p 7001
127.0.0.1:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:3
cluster_size:3
cluster_current_epoch:3
cluster_my_epoch:1
cluster_stats_messages_ping_sent:87
cluster_stats_messages_pong_sent:92
cluster_stats_messages_sent:179
cluster_stats_messages_ping_received:90
cluster_stats_messages_pong_received:87
cluster_stats_messages_meet_received:2
cluster_stats_messages_received:179
127.0.0.1:7001> cluster nodes
027a002ecc012b61a5997f151ad01bccbb65d1c0 127.0.0.1:7001@17001 myself,master - 0 1624260271000 1 connected 0-5460
3c349984f0bb61490c170ab68f2617a35d9581d6 127.0.0.1:7003@17003 master - 0 1624260271040 3 connected 10923-16383
79816979a6dd4b226e476121dd385ed6c25e5151 127.0.0.1:7002@17002 master - 0 1624260272044 2 connected 5461-10922
redis๋ set $KEY $VALUE
๋ก ๋ฐ์ดํฐ๋ฅผ ์ฝ์
ํ๊ณ , get $KEY
๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ๋ค.
redis-cli -p $PORT
๋จผ์ redis-cli
๋ก ์ ๊ทผ์ ํ๋ค.
7001๋ฒ ํฌํธ๋ก ์ ์์ ํ๋ฉด 7001๋ฒ์ด ์๋ ๋ค๋ฅธ ํฌํธ์ access๋ฅผ ํ ๋์ error
๊ฐ ๋ฐ์ํ๋ค.
root@ubuntu:/data$ redis-cli -p 7001
127.0.0.1:7001> set a b
(error) MOVED 15495 127.0.0.1:7003
127.0.0.1:7001> get a
(error) MOVED 15495 127.0.0.1:7003
127.0.0.1:7001> get a
(error) MOVED 15495 127.0.0.1:7003
127.0.0.1:7001> get a
(error) MOVED 15495 127.0.0.1:7003
127.0.0.1:7001> set b c
OK
127.0.0.1:7001> get b
"c"
127.0.0.1:7001> exit
redis-cli -c -p $PORT
-c
์ต์
์ redis๋ฅผ cluster๋ชจ๋๋ก ์ ์ํ๊ฒ ํด์ค๋ค.
์์๋ ๋ค๋ฅด๊ฒ ํด๋นํ๋ hash slot์ ๊ฐ์ง ์ฃผ์๋ก redirect๋ฅผ ํด์ค๋ค.
root@ubuntu:/data$ redis-cli -c -p 7001
127.0.0.1:7001> set a b
-> Redirected to slot [15495] located at 127.0.0.1:7003
OK
127.0.0.1:7003> get a
"b"
127.0.0.1:7003> set b c
-> Redirected to slot [3300] located at 127.0.0.1:7001
OK
127.0.0.1:7001> get b
"c"
127.0.0.1:7001> set c d
-> Redirected to slot [7365] located at 127.0.0.1:7002
OK
127.0.0.1:7002> get c
"d"
127.0.0.1:7002> get d
-> Redirected to slot [11298] located at 127.0.0.1:7003
(nil)
127.0.0.1:7003>
redis-cli -c -p 7001
127.0.0.1:7001> set a b
-> Redirected to slot [15495] located at 127.0.0.1:7003
CRC16(key) mod 16384
๋ฅผ ํตํด hashingํ๊ณ , ํด๋นํ๋ hash slot์ redirect ํ๋ค.CRC16(a) mod 16384
๋ 15495๋ก 7003๋
ธ๋๊ฐ ๊ฐ์ง๊ณ ์๋ hash slot์ด๋ค.127.0.0.1:7003> get a
์ด๋ฏธ ๊ตฌ์ฑ๋์ด ์๋ cluster์ ๋ ธ๋๋ฅผ ์ถ๊ฐํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์.
redis-7004.yaml
์ด๋ผ๋ ํ์ผ์ ์์ฑํ๊ณ ์์ํ๋ค.
ํฌํธ๋ฅผ ์ ์ธํ ๋ชจ๋ ์ค์ ์ ๊ธฐ์กด ๋
ธ๋๋ค๊ณผ ๊ฐ๋ค.
$ docker-compose -f redis-7004.yaml up -d
Creating redis-master-4 ... done
$ docker ps -a | grep redis
91f1d77161a7 redis:6.2.3 "docker-entrypoint.sโฆ" 34 seconds ago Up 32 seconds redis-master-4
0706f949061d redis:6.2.3 "docker-entrypoint.sโฆ" 18 minutes ago Up 18 minutes redis-master-3
de5e52fb0428 redis:6.2.3 "docker-entrypoint.sโฆ" 18 minutes ago Up 9 minutes redis-master-2
8a4cde915a47 redis:6.2.3 "docker-entrypoint.sโฆ" 18 minutes ago Up 18 minutes redis-master-1
cluster์ ์๋ก ๋์์ง ๋ ธ๋๋ฅผ ์ถ๊ฐํ๋ค.
$ docker exec -it redis-master-1 bash
$ redis-cli -p 7001 --cluster add-node 127.0.0.1:7004 127.0.0.1:7001
>>> Adding node 127.0.0.1:7004 to cluster 127.0.0.1:7001
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 027a002ecc012b61a5997f151ad01bccbb65d1c0 127.0.0.1:7001
slots:[0-5460] (5461 slots) master
M: 3c349984f0bb61490c170ab68f2617a35d9581d6 127.0.0.1:7003
slots:[10923-16383] (5461 slots) master
M: 79816979a6dd4b226e476121dd385ed6c25e5151 127.0.0.1:7002
slots:[5461-10922] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 127.0.0.1:7004 to make it join the cluster.
[OK] New node added correctly.
redis-cli -p port --cluster add-node ์ถ๊ฐํ ๋
ธ๋ ์์
ํ ๋
ธ๋
127.0.0.1:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:4
cluster_size:3
cluster_current_epoch:3
cluster_size:3
127.0.0.1:7001> cluster nodes
2bc7574b8d23ec967260fb822cccad471e3cecfa 127.0.0.1:7004@17004 master - 0 1624261424857 0 connected
027a002ecc012b61a5997f151ad01bccbb65d1c0 127.0.0.1:7001@17001 myself,master - 0 1624261425000 1 connected 0-5460
3c349984f0bb61490c170ab68f2617a35d9581d6 127.0.0.1:7003@17003 master - 0 1624261425863 3 connected 10923-16383
79816979a6dd4b226e476121dd385ed6c25e5151 127.0.0.1:7002@17002 master - 0 1624261424000 2 connected 5461-10922
์ ๊ท ๋
ธ๋๋ ๊ธฐ์กด ๋
ธ๋์ hash slot์ ๋๋์ด ๊ฐ์ง ์ ์๋ค.
reshard
๋ฅผ ํตํด ๋๋์ด๊ฐ์ง hash slot์ ์ ํ ์ ์๊ณ ,
rebalance
๋ฅผ ํตํด hash slot์ ๊ท ๋ฑํ๊ฒ ๋ฐฐ๋ถํ ์๋ ์๋ค.
redis-cli์ ์ต์ ์ ๋ค์๊ณผ ๊ฐ๋ค.
create
reshard
moveslots
add-node
del-node
info
rebalance
check
call
set-timeout
cluster-node-timeout
์ ์ค์ ํ๋ค.help