카테고리 없음

Docker-compose로 Redis Cluster 자동 구성하기

광82 2023. 9. 19. 10:30
반응형

Docker-compose로 Redis Cluster 자동 구성하기

이번 글에서는 docker-compose 로 redis-cluster를 구성해보겠습니다. 여기서 사용한 redis 이미지 및 redis / sentinel conf 파일 등 기본 환경은 제가 개인적으로 만든 것이기 때문에
이 글을 참고하시는 경우엔 본인의 환경에 맞게 설정이 필요합니다.

구성

* docker-compose.yml
    *   redis_cluster_1a
        * Dockerfile
            * docker-entrypoint.sh
    *   redis_cluster_2a
    *   redis_cluster_3a
    *   redis_cluster_1b
    *   redis_cluster_2b
    *   redis_cluster_3b

=> Master 3ea , Slave 3ea 생성

docker-compose.yml

version: '3'
services:
  redis_cluster_1a:
    image: redis_image:cluster
    build:
      context: ./redis1
      dockerfile: Dockerfile
    stdin_open: true
    tty: true
    environment:
      - REQUIREPASS=qhdks123
      - PORT=8001
      - HOST=172.19.0.20
    networks:
      redis_net:
        ipv4_address: 172.19.0.20
    container_name: redis_cluster_1a
  redis_cluster_2a:
    image: redis_image:cluster
    stdin_open: true
    tty: true
    environment:
      - REQUIREPASS=qhdks123
      - PORT=8001
      - HOST=172.19.0.21
    networks:
      redis_net:
        ipv4_address: 172.19.0.21
    depends_on:
      - redis_cluster_1a
    container_name: redis_cluster_2a
  redis_cluster_3a:
    image: redis_image:cluster
    stdin_open: true
    tty : true
    environment:
      - REQUIREPASS=qhdks123
      - PORT=8001
      - HOST=172.19.0.22
    networks:
      redis_net:
        ipv4_address: 172.19.0.22
    depends_on:
      - redis_cluster_2a
    container_name: redis_cluster_3a
  redis_cluster_1b:
    image: redis_image:cluster
    stdin_open: true
    tty : true
    environment:
      - REQUIREPASS=qhdks123
      - PORT=8001
      - HOST=172.19.0.23
    networks:
      redis_net:
        ipv4_address: 172.19.0.23
    depends_on:
      - redis_cluster_3a
    container_name: redis_cluster_1b
  redis_cluster_2b:
    image: redis_image:cluster
    stdin_open: true
    tty : true
    environment:
      - REQUIREPASS=qhdks123
      - PORT=8001
      - HOST=172.19.0.24
    networks:
      redis_net:
        ipv4_address: 172.19.0.24
    depends_on:
      - redis_cluster_1b
    container_name: redis_cluster_2b
  redis_cluster_3b:
    image: redis_image:cluster
    stdin_open: true
    tty : true
    environment:
      - REQUIREPASS=qhdks123
      - PORT=8001
      - HOST=172.19.0.25
    networks:
      redis_net:
        ipv4_address: 172.19.0.25
    depends_on:
      - redis_cluster_2b
    container_name: redis_cluster_3b

networks:
  redis_net:
    driver: bridge
    ipam:
     config:
       - subnet: 172.19.0.0/24

Dockerfile

FROM kimdubi/redis_image:latest

MAINTAINER kimdubi

USER root

ADD redis.conf /engn001/redis-stable/redis_8001.conf
ADD docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh

ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]

docker-entrypoint.sh

#!/bin/sh
set -e

# MASTER
#sed -i "s/bind 127.0.0.1/bind 0.0.0.0/g" /engn001/redis-stable/redis_8001.conf
sed -i "s/6379/$PORT/g" /engn001/redis-stable/redis_8001.conf
sed -i "s/requirepass foobared/requirepass $REQUIREPASS/g" /engn001/redis-stable/redis_8001.conf
sed -i "s/masterauth <master-password>/masterauth $REQUIREPASS/g" /engn001/redis-stable/redis_8001.conf
sed -i "s/redis.log/redis_$PORT/g" /engn001/redis-stable/redis_8001.conf
sed -i "s/redis.pid/redis_$PORT.pid/g" /engn001/redis-stable/redis_8001.conf

# CLUSTER
sed -i "s/#cluster-enabled yes/cluster-enabled yes/g" /engn001/redis-stable/redis_8001.conf
sed -i "s/#cluster-config-file nodes-6379.conf/cluster-config-file nodes_$PORT.conf/g" /engn001/redis-stable/redis_8001.conf
sed -i "s/#cluster-node-timeout 15000/cluster-node-timeout 5000/g" /engn001/redis-stable/redis_8001.conf

cp /engn001/redis-stable/redis_8001.conf /engn001/redis-stable/data

redis-server /engn001/redis-stable/redis_8001.conf &
sleep 10

printf 'yes\n' | redis-cli -p 8001 -a qhdks123 --cluster create 172.19.0.20:8001 172.19.0.21:8001 172.19.0.22:8001 172.19.0.25:8001 172.19.0.23:8001 172.19.0.24:8001 --cluster-replicas 1 &
sleep 10


/bin/bash

실행

docker-compose -f ./docker-compose.yml up -d --build

Building redis_cluster_1a
Step 1/6 : FROM kimdubi/redis_image:latest
 ---> a693415dce41
Step 2/6 : MAINTAINER kimdubi
 ---> Using cache
 ---> 824284b455ee
Step 3/6 : USER root
 ---> Using cache
 ---> c8c490ffb70d
Step 4/6 : ADD redis.conf /engn001/redis-stable/redis_8001.conf
 ---> Using cache
 ---> 1735069c6560
Step 5/6 : ADD docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
 ---> Using cache
 ---> 7fb4293ba344
Step 6/6 : ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
 ---> Using cache
 ---> c9ee06aa66de
Successfully built c9ee06aa66de
Successfully tagged redis_image:cluster
Creating redis_cluster_1a ... done
Creating redis_cluster_2a ... done
Creating redis_cluster_3a ... done
Creating redis_cluster_1b ... done
Creating redis_cluster_2b ... done
Creating redis_cluster_3b ... done

결과

[root@0b9693752f92 /]# redis-cli -p 8001 -a qhdks123 --cluster check 172.19.0.20:8001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.19.0.20:8001 (9b7201f6...) -> 0 keys | 5461 slots | 1 slaves.
172.19.0.21:8001 (289ed007...) -> 0 keys | 5462 slots | 1 slaves.
172.19.0.22:8001 (c3186498...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.19.0.20:8001)
M: 9b7201f6325351aaba31890e818171aa913d9e30 172.19.0.20:8001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: d1be9fc203d6a3a13841faf752831671ee9f623d 172.19.0.24:8001
   slots: (0 slots) slave
   replicates 289ed00739a84bb82bd55939e7fe3a064d8c9308
S: 7e63452401b51be9722f4d4ae65aae7cb4b3f692 172.19.0.25:8001
   slots: (0 slots) slave
   replicates c3186498062e6d1f4d1615cd76b1f9e780b435de
M: 289ed00739a84bb82bd55939e7fe3a064d8c9308 172.19.0.21:8001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 2fcc3aea104b1c95e278d26fa5dda043d8f1c5c7 172.19.0.23:8001
   slots: (0 slots) slave
   replicates 9b7201f6325351aaba31890e818171aa913d9e30
M: c3186498062e6d1f4d1615cd76b1f9e780b435de 172.19.0.22:8001
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
copy
  •  
반응형