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

지난 글에서 docker-compose 를 통해 mysql replication 구성하는 방법을 공유했습니다.
이번 글에서는 redis replication 구성하는 docker-compose.yml을 공유하겠습니다.
여기서 사용한 redis 이미지 및 redis / sentinel conf 파일 등 기본 환경은 제가 개인적으로 만든 것이기 때문에 이 글을 참고하시는 경우엔 본인의 환경에 맞게 설정이 필요합니다.
본문에서 사용된 docker-compose.yml 이나 dockerfile의 옵션 등은 이전 글 참고 부탁드립니다. (https://kimdubi.github.io/cloud/docker_mysql_repl/ )

구성

* docker-compose.yml
    * redis1
        * Dockerfile
            * docker-entrypoint.sh
    * redis2
        * Dockerfile
            * docker-entrypoint.sh
  • 총 두개의 컨테이너를 생성
  • 각각의 컨테이너에서 M-S redis 한대씩 생성하는 구성
  • redis1 <-> redis2 replication 구성이 아니라 각각 별개의 노드임

docker-compose.yml

version: '3'
services:
  redis1:
    image: redis_image:latest
    build:
      context: ./redis1
      dockerfile: Dockerfile
    stdin_open: true
    tty: true
    environment:
      - REQUIREPASS=qhdks123
      - PORT=8001
      - HOST=172.19.0.10
    volumes:
      - "../redis-data/redis1:/data"
    networks:
      redis_net:
        ipv4_address: 172.19.0.10
    restart: always
    container_name: redis1

  redis2:
    image: redis_image:latest
    build:
      context: ./redis2
      dockerfile: Dockerfile
    stdin_open: true
    tty: true
    environment:
      - REQUIREPASS=qhdks123
      - PORT=8001
      - HOST=172.19.0.11
    volumes:
      - "../redis-data/redis2:/data"
    networks:
      redis_net:
        ipv4_address: 172.19.0.11
    restart: always
    container_name: redis2

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

Dockerfile

FROM redis_image:latest 

MAINTAINER kimdubi 

USER root

ADD redis.conf /engn001/redis-stable/redis_8001.conf
ADD redis.conf /engn001/redis-stable/redis_8002.conf
ADD docker-entrypoint_1.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/port 6379/port 8001/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_8001.pid/g" /engn001/redis-stable/redis_8001.conf


# SLAVE
sed -i "s/bind 127.0.0.1/bind 0.0.0.0/g" /engn001/redis-stable/redis_8002.conf
sed -i "s/port 6379/port 8002/g" /engn001/redis-stable/redis_8002.conf
sed -i "s/# requirepass foobared/requirepass $REQUIREPASS/g" /engn001/redis-stable/redis_8002.conf
sed -i "s/# masterauth <master-password>/masterauth $REQUIREPASS/g" /engn001/redis-stable/redis_8002.conf
sed -i "s/redis.log/redis_8002/g" /engn001/redis-stable/redis_8002.conf
sed -i "s/redis.pid/redis_8002.pid/g" /engn001/redis-stable/redis_8002.conf



sed -i "s/# replicaof <masterip> <masterport>/replicaof $HOST 8001/g" /engn001/redis-stable/redis_8002.conf

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

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

/bin/bash

실행

kimdubi:conf nhn$ docker-compose -f ./docker-compose_cluster.yml up -d --build
Building redis1
Step 1/7 : FROM redis_image:latest
 ---> 12f2d1aceccf
Step 2/7 : MAINTAINER kimdubi
 ---> Running in 267c9a7305db
Removing intermediate container 267c9a7305db
 ---> 60c43e140621
Step 3/7 : USER root
 ---> Running in f1551ca18771
Removing intermediate container f1551ca18771
 ---> 5ea5e2f294bd
Step 4/7 : ADD redis.conf /engn001/redis-stable/redis_8001.conf
 ---> 18c79109484c
Step 5/7 : ADD redis.conf /engn001/redis-stable/redis_8002.conf
 ---> 315f0c1054b7
Step 6/7 : ADD docker-entrypoint_1.sh /usr/local/bin/docker-entrypoint.sh
 ---> 61115547bfdc
Step 7/7 : ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
 ---> Running in 86ab83398a30
Removing intermediate container 86ab83398a30
 ---> f787b6c79a50
Successfully built f787b6c79a50
Successfully tagged redis_image:latest
Building redis2
Step 1/7 : FROM redis_image:latest
 ---> f787b6c79a50
Step 2/7 : MAINTAINER kimdubi
 ---> Running in 2a5b37bea615
Removing intermediate container 2a5b37bea615
 ---> 29fe4d956d9f
Step 3/7 : USER root
 ---> Running in 04028334d2dd
Removing intermediate container 04028334d2dd
 ---> 4d2c2b07d2bb
Step 4/7 : ADD redis.conf /engn001/redis-stable/redis_8001.conf
 ---> cedb87ac51dc
Step 5/7 : ADD redis.conf /engn001/redis-stable/redis_8002.conf
 ---> b60e8de07c8e
Step 6/7 : ADD docker-entrypoint_1.sh /usr/local/bin/docker-entrypoint.sh
 ---> 681340e9e4b3
Step 7/7 : ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
 ---> Running in 3b3ec419dec0
Removing intermediate container 3b3ec419dec0
 ---> 8a52816eefd6
Successfully built 8a52816eefd6
Successfully tagged redis_image:latest
Creating redis1 ... done
Creating redis2 ... done

결과

kimdubi:conf nhn$ de redis1 /bin/bash
[root@0bf9cd2b4639 /]# ps -ef | grep redis
root        23     1  0 05:12 ?        00:00:00 redis-server 0.0.0.0:8001
root        29     1  0 05:13 ?        00:00:00 redis-server 0.0.0.0:8002
root        62    45  0 05:13 pts/1    00:00:00 grep --color=auto redis

[root@0bf9cd2b4639 /]# redis-cli -p 8002 -a qhdks123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:8002> info replication
# Replication
role:slave
master_host:172.19.0.10
master_port:8001
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:126
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:75fba13c9d4ab5653fb1634aa7cd4dbcb5c4e5f9
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:126
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:126