Redis cluster mode๋ฅผ ๋ค๋ฃฌ๋ค.
๋ณธ๋ฌธ์์๋ Redis cluster๋ฅผ docker๋ฅผ ํ์ฉํ์ฌ ์๋น์คํ๋ค.
Redis cluster ๊ถ์ฅ ์ค์ ์ด๋ค.
์๋ ์ค์ ์ ํด์ฃผ์ง ์์ผ๋ฉด WARNING
์ด ๋ฐ์ํ๋ค.
# ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด ํ์ฉ๋์ ์ด๊ณผํ ๊ฒฝ์ฐ, overcommit์ ์ฒ๋ฆฌํ๋ ๋ฐฉ์ ๊ฒฐ์ ํ๋ ๊ฐ์ "ํญ์"์ผ๋ก ๋ณ๊ฒฝ
$ sudo sysctl vm.overcommit_memory=1
$ sudo echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
$ sudo sysctl -a | grep vm.overcommit
# ์๋ฒ ์์ผ์ Accept๋ฅผ ๋๊ธฐํ๋ ์์ผ ๊ฐ์ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ณ๊ฒฝ
$ sudo sysctl -w net.core.somaxconn=1024
$ sudo echo "net.core.somaxconn=1024" >> /etc/sysctl.conf
$ sudo sysctl -a | grep somaxconn
# THP(Transparent Huge Pages) ๊ธฐ๋ฅ์ด Enable ๋์ด ์๋ ๊ฒฝ์ฐ Redis์์๋ ์ด๋ฅผ Disable ์ํฌ ๊ฒ์ ๊ถ์ฅํ๋ค.
docker-compose.yaml
Redis๋ docker network๊ฐ ์๋ host network๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
version: '3'
services:
redis-master-1:
container_name: redis-master-1
image: redis:6.2.3
network_mode: "host"
command: redis-server /etc/redis.conf
volumes:
- ./redis-master-1.conf:/etc/redis.conf
restart: always
ports:
- 7001:7001
- 17001:17001
redis-master-2:
container_name: redis-master-2
image: redis:6.2.3
network_mode: "host"
command: redis-server /etc/redis.conf
volumes:
- ./redis-master-2.conf:/etc/redis.conf
restart: always
ports:
- 7002:7002
- 17002:17002
redis-master-3:
container_name: redis-master-3
image: redis:6.2.3
network_mode: "host"
command: redis-server /etc/redis.conf
volumes:
- ./redis-master-3.conf:/etc/redis.conf
restart: always
ports:
- 7003:7003
- 17003:17003
network_mode: "host"
redis.conf
cluster node๊ฐ ์ฌ์ฉํ redis.conf
๋ฅผ ์์ฑํ๋ค.
# redis-master-1.conf
port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 3000
appendonly yes
# redis-master-2.conf
port 7002
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 3000
appendonly yes
# redis-master-3.conf
port 7003
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 3000
appendonly yes
port $PORT
$ docker-compose up -d
docker-compose up -d
docker-compose.yaml
ํ์ผ์ ๋ฐ๋ผ๋ณธ๋ค.-d
์ต์
์ ์ปจํ
์ด๋๋ฅผ daemonํํ๋ก ์คํํ๋ค.redis-cli --cluster create ...
๋ฅผ ํตํด ํด๋ฌ์คํฐ๋ฅผ ๊ตฌ์ฑํ๋ค.
์ถ๋ ฅ๋๋ ๋ด์ฉ์ ํตํด node๋ค์๊ฒ ๋ถ๋ฐฐ๋๋ hash slot์ ํ์ธํ ์ ์๋ค.
$ docker exec -it redis-master-1 bash
$ redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: 218ffd51a2d025a6af640bd9325db0de38750242 127.0.0.1:7001
slots:[0-5460] (5461 slots) master
M: f1f6a1c8fea47794c07184ccb9b15c9ba31f2033 127.0.0.1:7002
slots:[5461-10922] (5462 slots) master
M: 885bbe387799a7d1f724d2aa4bee267b14935823 127.0.0.1:7003
slots:[10923-16383] (5461 slots) master
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 127.0.0.1:7001)
M: 218ffd51a2d025a6af640bd9325db0de38750242 127.0.0.1:7001
slots:[0-5460] (5461 slots) master
M: f1f6a1c8fea47794c07184ccb9b15c9ba31f2033 127.0.0.1:7002
slots:[5461-10922] (5462 slots) master
M: 885bbe387799a7d1f724d2aa4bee267b14935823 127.0.0.1:7003
slots:[10923-16383] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
docker exec -it $CONTAINER_NAME bash
redis-cli --cluster create $IP_1:$PORT_1 ... $IP_N:$PORT_N
cluster node์ ์ ์ํ์ฌ cluster info
์ cluster nodes
๋ฅผ ํตํด ํด๋ฌ์คํฐ์ ์ ๋ณด ๋ฑ์ ํ์ธํ ์ ์๋ค.
$ 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:3
cluster_size:3
cluster_current_epoch:3
cluster_my_epoch:1
cluster_stats_messages_ping_sent:107
cluster_stats_messages_pong_sent:111
cluster_stats_messages_sent:218
cluster_stats_messages_ping_received:109
cluster_stats_messages_pong_received:107
cluster_stats_messages_meet_received:2
cluster_stats_messages_received:218
127.0.0.1:7001> cluster nodes
f1f6a1c8fea47794c07184ccb9b15c9ba31f2033 127.0.0.1:7002@17002 master - 0 1624425301254 2 connected 5461-10922
885bbe387799a7d1f724d2aa4bee267b14935823 127.0.0.1:7003@17003 master - 0 1624425302260 3 connected 10923-16383
218ffd51a2d025a6af640bd9325db0de38750242 127.0.0.1:7001@17001 myself,master - 0 1624425301000 1 connected 0-5460
redis-cli -c -p 7001
-c
: cluster๋ชจ๋๋ก ์ ์ํ๋ค.-p
: ์ ์ํ node์ ํฌํธcluster info
cluster nodes
mac์์ ๋์ผํ๊ฒ ์ค์ตํด๋ณด๊ณ ์์ต๋๋ค. docker-compose ์ฌ์ฉํ์ฌ redis cluster ๊ตฌ์ฑ์ ํ๊ณ ํ ์คํธํด๋ณด๊ณ ์ถ์๋ฐ์. 4.2๋จ๊ณ์ redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003์์ ๊ณ์
Could not connect to Redis at 127.0.0.1:7001 Connection refused ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋๋ฐ ํน์ ํด๊ฒฐ๋ฒ์ ์ ์ ์์๊น์?