Redis Cluster์์ ์ฅ์ ๊ฐ ๋๋ฉด ์ด๋ป๊ฒ ๋ ๊น?
๋ณธ ๋ฌธ์์์๋ ๋๊ฐ์ง ์๋๋ฆฌ์ค๋ฅผ ๋ค๋ฃฌ๋ค.
slave๊ฐ ์๋ master์ ์ฅ์ ๊ฐ ๋ฐ์ํ๋ฉด ์ด๋ป๊ฒ ๋๋์ง ์์๋ณด์.
7001, 7002, 7003ํฌํธ์๋ ๋ชจ๋ redis master๊ฐ ์๋น์ค๋๊ณ , 7002๋
ธ๋๋ฅผ ์ ์งํด๋ณธ๋ค.
$ docker exec -it redis-master-2 bash
$ redis-cli -c -p 7002
127.0.0.1:7002> debug segfault
Error: Connection reset by peer
(0.86s)
debug segfault
docker log๋ฅผ ์ดํด๋ณธ๋ค.
cluster state๊ฐ fail๋ก ๋ฐ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
docker logs -f redis-master-2
1:M 21 Jun 2021 07:28:21.434 * FAIL message received from 3c349984f0bb61490c170ab68f2617a35d9581d6 about 79816979a6dd4b226e476121dd385ed6c25e5151
1:M 21 Jun 2021 07:28:21.435 # Cluster state changed: fail
์ด์ 7001๋ฒ ๋ ธ๋์ ์ ์ํ์ฌ cluster ์ ๋ณด๋ฅผ ์ดํด๋ณด์
$ docker exec -it redis-master-1 bash
$ redis-cli -c -p 7001
127.0.0.1:7001> set a b
(error) CLUSTERDOWN The cluster is down
127.0.0.1:7001> cluster info
cluster_state:fail
cluster_slots_assigned:16384
cluster_slots_ok:10922
cluster_slots_pfail:0
cluster_slots_fail:5462
cluster_known_nodes:3
cluster_size:3
cluster_current_epoch:3
cluster_my_epoch:1
cluster_stats_messages_ping_sent:435
cluster_stats_messages_pong_sent:461
cluster_stats_messages_sent:896
cluster_stats_messages_ping_received:459
cluster_stats_messages_pong_received:434
cluster_stats_messages_meet_received:2
cluster_stats_messages_fail_received:1
cluster_stats_messages_received:896
127.0.0.1:7001> cluster nodes
027a002ecc012b61a5997f151ad01bccbb65d1c0 127.0.0.1:7001@17001 myself,master - 0 1624260599000 1 connected 0-5460
3c349984f0bb61490c170ab68f2617a35d9581d6 127.0.0.1:7003@17003 master - 0 1624260599580 3 connected 10923-16383
79816979a6dd4b226e476121dd385ed6c25e5151 127.0.0.1:7002@17002 master,fail - 1624260495388 1624260493352 2 disconnected 5461-10922
127.0.0.1:7001> set a b
cluster_state:fail
127.0.0.1:7001> cluster nodes
7002๋ฒ master๋ฅผ ์ฌ์์ํ๋ค.
$ docker restart redis-master-2
redis-master-2
$ docker ps | grep redis-master-2
de5e52fb0428 redis:6.2.3 "docker-entrypoint.sโฆ" 9 minutes ago Up 8 seconds redis-master-2
docker log๋ฅผ ํ์ธํด๋ณด์.
$ docker logs redis-master-2
1:M 21 Jun 2021 07:30:56.966 * Node configuration loaded, I'm 79816979a6dd4b226e476121dd385ed6c25e5151
Node configuration loaded
๋ ธ๋๋ค์ ์ํ์ ๋ณด๋ฅผ ํ์ธํด๋ณด๋ฉด 3๊ฐ์ master ๋ชจ๋ ์ ์์ ์ผ๋ก ์๋๋๊ณ ์๋๊ฒ์ ํ์ธํ ์ ์๋ค.
$ docker exec -it redis-master-1 bash
$ redis-cli -c -p 7001
127.0.0.1:7001> cluster nodes
027a002ecc012b61a5997f151ad01bccbb65d1c0 127.0.0.1:7001@17001 myself,master - 0 1624260765000 1 connected 0-5460
3c349984f0bb61490c170ab68f2617a35d9581d6 127.0.0.1:7003@17003 master - 0 1624260767224 3 connected 10923-16383
79816979a6dd4b226e476121dd385ed6c25e5151 127.0.0.1:7002@17002 master - 0 1624260766218 2 connected 5461-10922
์ด์ cluster๊ฐ ์ ์ํ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
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> set c d
-> Redirected to slot [7365] located at 127.0.0.1:7002
OK
127.0.0.1:7002> get c
"d"
redis cluster์์๋ Master-Slave๊ตฌ์กฐ๋ฅผ ํตํด cluster์ ์์์ฑ์ ๋ณด์ฅํด์ค๋ค.
slave๋ master์ ๋ฐ์ดํฐ๋ฅผ ๋ณต์ ํ๊ณ master ์ฅ์ ๋ฐ์ ์ slave๊ฐ master๋ก ์น๊ฒฉํ์ฌ cluster๋ฅผ ์ ์งํ๋ค.
master-slave๊ตฌ์กฐ์์์ ์ฅ์ ๋ฐ์ ์๋๋ฆฌ์ค๋ฅผ ์ดํด๋ณด์.
์๋๋ฆฌ์ค๋ ๋ค์๊ณผ ๊ฐ๋ค.
์ด๋ฆ | ์ฃผ์ | ํด๋ฌ์คํฐ |
---|---|---|
node1 | 192.168.56.100 | redis-master-1, redis-slave-3 |
node2 | 192.168.56.101 | redis-master-2, redis-slave-1 |
node3 | 192.168.56.102 | redis-master-3, redis-slave-2 |
redis-master-2
๊ฐ fail์ด ๋๋ ์ํฉ์ด๋ค.192.168.56.101์ ์ ์ํ์ฌ container๋ฅผ ์ค์งํ๋ค.
$ docker stop redis-master-2
192.168.56.102์ ์ ์ํ์ฌ slave์ ๋ก๊ทธ๋ฅผ ํ์ธํ๋ค.(101๋ฒ master์ slave๋ 102๋ฒ ์๋ฒ์ ์๋ค.)
$ docker logs -f redis-slave-2
1:S 23 Jun 2021 07:49:33.581 # Connection with master lost.
1:S 23 Jun 2021 07:49:33.582 * Caching the disconnected master state.
1:S 23 Jun 2021 07:49:33.582 * Reconnecting to MASTER 192.168.56.101:7001
1:S 23 Jun 2021 07:49:33.582 * MASTER <-> REPLICA sync started
1:S 23 Jun 2021 07:49:33.583 # Error condition on socket for SYNC: Connection refused
...
1:S 23 Jun 2021 07:49:38.756 # Failover election won: I'm the new master.
1:M 23 Jun 2021 07:49:38.757 # Cluster state changed: ok
Failover election won: I'm the new master.
redis-master-2
๊ฐ fail์ด ๋๊ณ , redis-slave-2
๊ฐ master๋ก ์น๊ฒฉํ ๊ฒ์ ํ์ธํ ์ ์๋ค.cluster ์ํ ์ ๋ณด๋ฅผ ๊ฒ์ํด๋ณด์.
$ docker exec -it redis-master-1 bash
$ redis-cli -c -p 7001
127.0.0.1:7001> cluster nodes
30a99d668af3ddda16e2a9d3ee97fb53a5ebfa6d 192.168.56.100:7002@17002 myself,slave 22110f4ea10f11a8cb6ea283dedfc27c6ffabc07 0 1624434594000 3 connected
22110f4ea10f11a8cb6ea283dedfc27c6ffabc07 192.168.56.102:7001@17001 master - 0 1624434596533 3 connected 10923-16383
094af2ab1db0d147d7f475f3954429ae7d18dee0 192.168.56.102:7002@17002 master - 0 1624434595524 7 connected 5461-10922
c952f5ef4783b5c19129bc630b88e8e3bf602622 192.168.56.101:7001@17001 master,fail - 1624434574580 1624434573000 2 disconnected
5b56d458a0d8e64d5f40ece0a99713dcb9c70723 192.168.56.100:7001@17001 master - 0 1624434595524 1 connected 0-5460
e0d9ee09b593889cd093d217a16a0b535e6abef2 192.168.56.101:7002@17002 slave 5b56d458a0d8e64d5f40ece0a99713dcb9c70723 0 1624434595626 1 connected
094af2ab1db0d147d7f475f3954429ae7d18dee0 192.168.56.102:7002@17002 master
c952f5ef4783b5c19129bc630b88e8e3bf602622 192.168.56.101:7001@17001 master,fail
master "์๋" redis-master-2
๋
ธ๋๋ฅผ ์ฌ๊ธฐ๋ํ๋ค.
$ docker restart redis-master-2
์ฌ๊ธฐ๋์ ํด๋ redis-master-2
๋ master๋ก ์น๊ฒฉ๋์ง ์๋๋ค.
redis-slave-2
๊ฐ master๋ฅผ ์ ์งํ๋ค.
$ docker exec -it redis-master-1 bash
$ redis-cli -c -p 7001
127.0.0.1:7001> cluster nodes
30a99d668af3ddda16e2a9d3ee97fb53a5ebfa6d 192.168.56.100:7002@17002 myself,slave 22110f4ea10f11a8cb6ea283dedfc27c6ffabc07 0 1624434646000 3 connected
22110f4ea10f11a8cb6ea283dedfc27c6ffabc07 192.168.56.102:7001@17001 master - 0 1624434646576 3 connected 10923-16383
094af2ab1db0d147d7f475f3954429ae7d18dee0 192.168.56.102:7002@17002 master - 0 1624434646071 7 connected 5461-10922
c952f5ef4783b5c19129bc630b88e8e3bf602622 192.168.56.101:7001@17001 slave 094af2ab1db0d147d7f475f3954429ae7d18dee0 0 1624434647079 7 connected
5b56d458a0d8e64d5f40ece0a99713dcb9c70723 192.168.56.100:7001@17001 master - 0 1624434646575 1 connected 0-5460
e0d9ee09b593889cd093d217a16a0b535e6abef2 192.168.56.101:7002@17002 slave 5b56d458a0d8e64d5f40ece0a99713dcb9c70723 0 1624434646576 1 connected