Redis Cluster x Docker

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

redis

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

Redis Cluster x Docker

๐ŸŽ ๋ชฉ์ฐจ

๊ฐœ์š”

Redis cluster mode๋ฅผ ๋‹ค๋ฃฌ๋‹ค.
๋ณธ๋ฌธ์—์„œ๋Š” Redis cluster๋ฅผ docker๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์„œ๋น„์Šคํ•œ๋‹ค.

1. Setup

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 ์‹œํ‚ฌ ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค.

2. Write 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"
    • container๊ฐ€ host network๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒํ•œ๋‹ค.

3. Create 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
    • node๊ฐ€ ์‚ฌ์šฉํ•  port๋ฅผ ์ง€์ •ํ•œ๋‹ค.
    • cluster๊ฐ„ ํ†ต์‹ ์€ 10000์„ ๋”ํ•œ port๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

4. Start cluster

4.1. start

$ docker-compose up -d
  • docker-compose up -d
    • ๊ธฐ๋ณธ์ ์œผ๋กœ ํ˜„์žฌ ๊ฒฝ๋กœ์˜ docker-compose.yamlํŒŒ์ผ์„ ๋ฐ”๋ผ๋ณธ๋‹ค.
    • -d์˜ต์…˜์€ ์ปจํ…Œ์ด๋„ˆ๋ฅผ daemonํ˜•ํƒœ๋กœ ์‹คํ–‰ํ•œ๋‹ค.

4.2. create cluster

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
    • container๋‚ด๋ถ€๋กœ ์ง„์ž…ํ•œ๋‹ค.
  • redis-cli --cluster create $IP_1:$PORT_1 ... $IP_N:$PORT_N
    • cluster๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค.

4.3. check

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
    • node ์ ‘์† ํ›„ ์‹คํ–‰ํ•˜๋ฉด cluster๋“ค์˜ ์ •๋ณด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
  • cluster nodes
    • node ์—ฐ๊ฒฐ ์ •๋ณด๋ฅผ ํ™•์ธํ•œ๋‹ค.

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

comment-user-thumbnail
2021๋…„ 12์›” 27์ผ

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 ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๋ฐ ํ˜น์‹œ ํ•ด๊ฒฐ๋ฒ•์„ ์•Œ ์ˆ˜ ์žˆ์„๊นŒ์š”?

1๊ฐœ์˜ ๋‹ต๊ธ€