Redis Cluster (Master-Slave)

์žญ์žญ์ดยท2021๋…„ 6์›” 29์ผ
2

redis

๋ชฉ๋ก ๋ณด๊ธฐ
4/6
post-thumbnail

Redis Cluster (Master-Slave)

๐ŸŽ ๋ชฉ์ฐจ

๊ฐœ์š”

Redis Cluster x Docker)๊ณผ ์ด์–ด์ง€๋Š” ๋‚ด์šฉ์ด๋‹ค.
Redis cluster์— slave node๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด์ž.
Slave node๋Š” master node์˜ ๋ณต์ œ๋ณธ์ด๊ณ , master node์—์„œ ์žฅ์•  ๋ฐœ์ƒ ์‹œ master๋กœ ์Šน๊ฒฉํ•œ๋‹ค.
์ด์ƒ์ ์ธ HA๋ฅผ ์œ„ํ•ด ๋™์ผ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ ์žˆ๋Š” master์™€ slave๋Š” ๋‹ค๋ฅธ ์„œ๋ฒ„์— ๋ถ„๋ฆฌ์‹œ์ผœ ๋†“๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

1. Configuration information

1.1. server table

์ด๋ฆ„์ฃผ์†Œํด๋Ÿฌ์Šคํ„ฐ
node1192.168.56.100redis-master-1, redis-slave-3
node2192.168.56.101redis-master-2, redis-slave-1
node3192.168.56.102redis-master-3, redis-slave-2
  • ํ•˜๋‚˜์˜ ๋งˆ์Šคํ„ฐ์— ํ•˜๋‚˜์˜ ๋ณต์ œ๋ณธ์„ ์ƒ์„ฑํ•œ๋‹ค.
    • masterslave
      1redis-master-1redis-slave-1
      2redis-master-2redis-slave-2
      3redis-master-3redis-slave-3
    • ์œ„์—์„œ ์„ค๋ช…ํ•œ ๋ฐ”์™€ ๊ฐ™์ด master, slave์Œ์„ ๋ถ„๋ฆฌ์‹œ์ผœ ๋†“๋Š”๋‹ค.
    • ๋ถ„๋ฆฌ์‹œ์ผœ๋†“์ง€ ์•Š์œผ๋ฉด ํ•˜๋‚˜์˜ ์„œ๋ฒ„๋งŒ ์ฃฝ์–ด๋„ cluster๊ฐ€ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค.

2. Start cluster

2.1. create cluster

--cluster-replicas๋ฅผ 1๋กœ ์„ค์ •ํ•˜์—ฌ ํ•˜๋‚˜์˜ master๋‹น ํ•˜๋‚˜์˜ slave๋กœ ๊ตฌ์„ฑํ•œ๋‹ค.
master๋Š” ์•ž์—์„œ ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ํ• ๋‹น๋˜๋ฉฐ ํ• ๋‹น์ด ๋๋‚œ ํ›„ slave๋„ ์ฐจ๋ก€๋Œ€๋กœ master์— ๋งคํ•‘๋œ๋‹ค.

$ docker exec -it redis-master-1 bash
$ redis-cli --cluster create 192.168.56.100:7001 192.168.56.101:7001 192.168.56.102:7001 192.168.56.100:7002 192.168.56.101:7002 192.168.56.102:7002 --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 192.168.56.101:7002 to 192.168.56.100:7001
Adding replica 192.168.56.102:7002 to 192.168.56.101:7001
Adding replica 192.168.56.100:7002 to 192.168.56.102:7001
M: 5b56d458a0d8e64d5f40ece0a99713dcb9c70723 192.168.56.100:7001
   slots:[0-5460] (5461 slots) master
M: c952f5ef4783b5c19129bc630b88e8e3bf602622 192.168.56.101:7001
   slots:[5461-10922] (5462 slots) master
M: 22110f4ea10f11a8cb6ea283dedfc27c6ffabc07 192.168.56.102:7001
   slots:[10923-16383] (5461 slots) master
S: 30a99d668af3ddda16e2a9d3ee97fb53a5ebfa6d 192.168.56.100:7002
   replicates 22110f4ea10f11a8cb6ea283dedfc27c6ffabc07
S: e0d9ee09b593889cd093d217a16a0b535e6abef2 192.168.56.101:7002
   replicates 5b56d458a0d8e64d5f40ece0a99713dcb9c70723
S: 094af2ab1db0d147d7f475f3954429ae7d18dee0 192.168.56.102:7002
   replicates c952f5ef4783b5c19129bc630b88e8e3bf602622
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 192.168.56.100:7001)
M: 5b56d458a0d8e64d5f40ece0a99713dcb9c70723 192.168.56.100:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: e0d9ee09b593889cd093d217a16a0b535e6abef2 192.168.56.101:7002
   slots: (0 slots) slave
   replicates 5b56d458a0d8e64d5f40ece0a99713dcb9c70723
S: 30a99d668af3ddda16e2a9d3ee97fb53a5ebfa6d 192.168.56.100:7002
   slots: (0 slots) slave
   replicates 22110f4ea10f11a8cb6ea283dedfc27c6ffabc07
M: 22110f4ea10f11a8cb6ea283dedfc27c6ffabc07 192.168.56.102:7001
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: c952f5ef4783b5c19129bc630b88e8e3bf602622 192.168.56.101:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 094af2ab1db0d147d7f475f3954429ae7d18dee0 192.168.56.102:7002
   slots: (0 slots) slave
   replicates c952f5ef4783b5c19129bc630b88e8e3bf602622
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
  • redis-cli --cluster create $MASTER_1 ... $MASTER_N $SLAVE_1 ... $SLAVE_N --cluster-replicas 1
    • --cluster-replicas๋งŒํผ ๋ณต์ œ๋ณธ์„ ๊ฐ€์ง„ cluster๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

2.2. check

$ redis-cli -c -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:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:800
cluster_stats_messages_pong_sent:810
cluster_stats_messages_sent:1610
cluster_stats_messages_ping_received:805
cluster_stats_messages_pong_received:800
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1610

127.0.0.1:7001> cluster nodes
e0d9ee09b593889cd093d217a16a0b535e6abef2 192.168.56.101:7002@17002 slave 5b56d458a0d8e64d5f40ece0a99713dcb9c70723 0 1624430604104 1 connected
30a99d668af3ddda16e2a9d3ee97fb53a5ebfa6d 192.168.56.100:7002@17002 slave 22110f4ea10f11a8cb6ea283dedfc27c6ffabc07 0 1624430604000 3 connected
22110f4ea10f11a8cb6ea283dedfc27c6ffabc07 192.168.56.102:7001@17001 master - 0 1624430604000 3 connected 10923-16383
c952f5ef4783b5c19129bc630b88e8e3bf602622 192.168.56.101:7001@17001 master - 0 1624430604306 2 connected 5461-10922
094af2ab1db0d147d7f475f3954429ae7d18dee0 192.168.56.102:7002@17002 slave c952f5ef4783b5c19129bc630b88e8e3bf602622 0 1624430603000 2 connected
5b56d458a0d8e64d5f40ece0a99713dcb9c70723 192.168.56.100:7001@17001 myself,master - 0 1624430603000 1 connected 0-5460
  • cluster_size:3
    • cluster๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฐœ์ˆ˜
    • slave๋ฅผ ์ œ์™ธํ•œ master์˜ ์ด ๊ฐœ์ˆ˜
  • cluster_known_nodes
    • cluster์—์„œ master, slave๋ฅผ ํฌํ•จํ•œ ์ด ๋…ธ๋“œ ์ˆ˜

3. Trouble shooting

redis๊ฐ€ ํ†ต์‹ ์ด ๋˜์ง€ ์•Š์„ ๋•Œ ๋‹ค์Œ์„ ํ™•์ธํ•ด๋ณธ๋‹ค.
1. docker container๊ฐ€ host network๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.
2. redis.conf์—์„œ cluster-config-file์„ค์ •์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ํŒŒ์ผ์€ node ๋ณ„ ํ•˜๋‚˜์”ฉ ์กด์žฌํ•ด์•ผ ํ•œ๋‹ค.
3. ๋ฐฉํ™”๋ฒฝ์„ ํ™•์ธํ•œ๋‹ค.

3.1. docker host network

docker-compose๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, docker-compose.yaml์— ๋‹ค์Œ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

network_mode: "host"   

3.2. redis.conf

node์˜ ํ™˜๊ฒฝ ์„ค์ •์„ ์…‹ํŒ…ํ•˜๋Š” ํŒŒ์ผ์ด๋‹ค.
์—ฌ๊ธฐ์— cluster-config-file์„ ํ†ตํ•ด ์„ค์ •๋˜์–ด์ง„ config์ •๋ณด๋ฅผ ์ €์žฅํ•  ํŒŒ์ผ๋ช…์„ ๊ธฐ์ž…ํ•œ๋‹ค.
๋งŒ์—ํ•˜๋‚˜ docker volume mount๋ฅผ ํ†ตํ•ด ์ •๋ณด๋ฅผ ์ €์žฅํ•  ํŒŒ์ผ ๋ช…์ด ๊ฒน์น˜๊ฒŒ ๋˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

3.3 Firewall

Linux์˜ ๊ฒฝ์šฐ ํ”ํžˆ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.
๋ฐฉํ™”๋ฒฝ์„ ๋Œ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๋ณด์•ˆ์„ ์œ„ํ•ด ๋‹ค์Œ์„ ๊ถŒ์žฅํ•œ๋‹ค.

  1. ๋ฐฉํ™”๋ฒฝ ํ™•์ธ
$ sudo firewall-cmd --list-all --permanent --zone=public
  1. ํฌํŠธ ํ—ˆ์šฉ
$ sudo firewall-cmd --permanent --add-port=7051/tcp --zone=public
$ sudo firewall-cmd --permanent --add-port=17051/tcp --zone=public
$ sudo firewall-cmd --permanent --add-port=7052/tcp --zone=public
$ sudo firewall-cmd --permanent --add-port=17052/tcp --zone=public
  1. ๋ณ€๊ฒฝ์‚ฌํ•ญ ์ ์šฉ
$ sudo firewall-cmd --reload
  1. ์ ์šฉ์™„๋ฃŒ ํ™•์ธ
$ sudo firewall-cmd --list-all --permanent --zone=public
  1. ๊ธฐํƒ€
# ํ—ˆ์šฉ๋œ ํฌํŠธ ์ œ๊ฑฐ
$ sudo firewall-cmd --permanent --remove-port=7001/tcp --zone=public
# ํ—ˆ์šฉ๋œ ์„œ๋น„์Šค ์ œ๊ฑฐ
$ sudo firewall-cmd --permanent --remove-service=http --zone=public

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2024๋…„ 6์›” 10์ผ

์•ˆ๋…•ํ•˜์„ธ์š”.

ํฌ์ŠคํŒ… ๋ณด๋ฉด์„œ ๊ณต๋ถ€ํ•˜๊ณ  ์žˆ๋Š”๋ฐ 1. Configuration information ์ด๋ถ€๋ถ„์—์„œ ๋ง‰ํ˜€์„œ์š” ํ•ด๋‹น ๋„์ปค์—์„œ ํ•ด๋‹น ์•„์ดํ”ผ 3๊ฐœ์— master, slave๋กœ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„...์ž˜ ๋ชจ๋ฅด๊ฒ ์–ด์„œ์š” ใ… ใ…  ๊ฐ๊ฐ์˜ ์ปจํ…Œ์ด๋„ˆ๋Š” ๊ณ ์œ ์˜ ip๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ master๋ž‘ slave๊ฐ€ ๊ฐ™์€ ip๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  port๋งŒ ๋‹ค๋ฅด๊ฒŒ ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑํ•ด์•ผํ•˜๋Š”์ง€.. ์•Œ๋ ค์ฃผ์‹ค ์ˆ˜ ์žˆ์„๊นŒ์š”? ใ…Žใ…Ž..

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ