Docker-compose로 Redis Sentinel 자동 구성하기
이번 글에서는 redis sentinel을 docker-compose 를 통해 구성하는 방법을 공유하겠습니다.
여기서 사용한 redis 이미지 및 redis / sentinel conf 파일 등 기본 환경은 제가 개인적으로 만든 것이기 때문에
이 글을 참고하시는 경우엔 본인의 환경에 맞게 설정이 필요합니다.
구성
* docker-compose.yml
* redis_master
* Dockerfile
* docker-entrypoint.sh
* redis_slave1
* Dockerfile
* docker-entrypoint.sh
* redis_slave2
* Dockerfile
* docker-entrypoint.sh
docker-compose.yml
version: '3'
services:
redis1:
image: redis_image:master
build:
context: ./redis1
dockerfile: Dockerfile
stdin_open: true
tty: true
environment:
- REQUIREPASS=qhdks123
- PORT=6001
- HOST=172.19.0.20
- MASTER_HOST=172.19.0.20
- MASTER_PORT=6001
- SENTINEL_PORT=7001
- QUORUM=2
networks:
redis_net:
ipv4_address: 172.19.0.20
restart: always
container_name: redis_master
redis2:
image: redis_image:slave
build:
context: ./redis2
dockerfile: Dockerfile
stdin_open: true
tty: true
environment:
- REQUIREPASS=qhdks123
- PORT=6001
- HOST=172.19.0.21
- MASTER_HOST=172.19.0.20
- MASTER_PORT=6001
- SENTINEL_PORT=7001
- QUORUM=2
networks:
redis_net:
ipv4_address: 172.19.0.21
depends_on:
- redis1
restart: always
container_name: redis_slave1
redis3:
image: redis_image:slave
build:
context: ./redis3
dockerfile: Dockerfile
stdin_open: true
tty : true
environment:
- REQUIREPASS=qhdks123
- PORT=6001
- HOST=172.19.0.22
- MASTER_HOST=172.19.0.20
- MASTER_PORT=6001
- SENTINEL_PORT=7001
- QUORUM=2
networks:
redis_net:
ipv4_address: 172.19.0.22
depends_on:
- redis1
restart: always
container_name: redis_slave2
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_6001.conf
ADD sentinel.conf /engn001/redis-stable/sentinel_7001.conf
ADD docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
docker-entrypoint.sh - Master
#!/bin/sh
set -e
# MASTER
#sed -i "s/bind 127.0.0.1/bind 0.0.0.0/g" /engn001/redis-stable/redis_6001.conf
sed -i "s/6379/$PORT/g" /engn001/redis-stable/redis_6001.conf
sed -i "s/requirepass foobared/requirepass $REQUIREPASS/g" /engn001/redis-stable/redis_6001.conf
sed -i "s/masterauth <master-password>/masterauth $REQUIREPASS/g" /engn001/redis-stable/redis_6001.conf
sed -i "s/redis.log/redis_$PORT/g" /engn001/redis-stable/redis_6001.conf
sed -i "s/redis.pid/redis_$PORT.pid/g" /engn001/redis-stable/redis_6001.conf
# SENTINEL
sed -i "s/26379/$SENTINEL_PORT/g" /engn001/redis-stable/sentinel_7001.conf
sed -i "s/master_ip master_port quorum/$MASTER_HOST $MASTER_PORT $QUORUM/g" /engn001/redis-stable/sentinel_7001.conf
sed -i "s/foobared/$REQUIREPASS/g" /engn001/redis-stable/sentinel_7001.conf
cp /engn001/redis-stable/redis_6001.conf /engn001/redis-stable/data
cp /engn001/redis-stable/sentinel_7001.conf /engn001/redis-stable/data
redis-server /engn001/redis-stable/redis_6001.conf &
sleep 10
redis-sentinel /engn001/redis-stable/sentinel_7001.conf &
sleep 10
/bin/bash
docker-entrypoint.sh - Slave
#!/bin/sh
set -e
# SLAVE
#sed -i "s/bind 127.0.0.1/bind 0.0.0.0/g" /engn001/redis-stable/redis_6001.conf
sed -i "s/6379/$PORT/g" /engn001/redis-stable/redis_6001.conf
sed -i "s/requirepass foobared/requirepass $REQUIREPASS/g" /engn001/redis-stable/redis_6001.conf
sed -i "s/masterauth <master-password>/masterauth $REQUIREPASS/g" /engn001/redis-stable/redis_6001.conf
sed -i "s/redis.log/redis_$PORT/g" /engn001/redis-stable/redis_6001.conf
sed -i "s/redis.pid/redis_$PORT.pid/g" /engn001/redis-stable/redis_6001.conf
sed -i "s/# replicaof <masterip> <masterport>/replicaof $MASTER_HOST $MASTER_PORT/g" /engn001/redis-stable/redis_6001.conf
# SENTINEL
sed -i "s/26379/$SENTINEL_PORT/g" /engn001/redis-stable/sentinel_7001.conf
sed -i "s/master_ip master_port quorum/$MASTER_HOST $MASTER_PORT $QUORUM/g" /engn001/redis-stable/sentinel_7001.conf
sed -i "s/foobared/$REQUIREPASS/g" /engn001/redis-stable/sentinel_7001.conf
cp /engn001/redis-stable/redis_6001.conf /engn001/redis-stable/data
cp /engn001/redis-stable/sentinel_7001.conf /engn001/redis-stable/data
redis-server /engn001/redis-stable/redis_6001.conf &
sleep 10
redis-sentinel /engn001/redis-stable/sentinel_7001.conf &
sleep 10
/bin/bash
실행
kimdubi:conf nhn$ docker-compose up -d --build
Building redis1
Step 1/7 : FROM redis_image:latest
---> a693415dce41
Step 2/7 : MAINTAINER kimdubi
---> Running in bdecdbf2d6f7
Removing intermediate container bdecdbf2d6f7
---> 5fc385f01aa0
Step 3/7 : USER root
---> Running in 5eee44db05cb
Removing intermediate container 5eee44db05cb
---> d4549d967334
Step 4/7 : ADD redis.conf /engn001/redis-stable/redis_6001.conf
---> 2055f6e1ab67
Step 5/7 : ADD sentinel.conf /engn001/redis-stable/sentinel_7001.conf
---> 2e87093ecc1b
Step 6/7 : ADD docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
---> a3549a5d4b59
Step 7/7 : ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
---> Running in 55c565c617c6
Removing intermediate container 55c565c617c6
---> 26542f033011
Successfully built 26542f033011
Successfully tagged redis_image:master
Building redis2
Step 1/7 : FROM redis_image:latest
---> a693415dce41
Step 2/7 : MAINTAINER kimdubi
---> Using cache
---> 5fc385f01aa0
Step 3/7 : USER root
---> Using cache
---> d4549d967334
Step 4/7 : ADD redis.conf /engn001/redis-stable/redis_6001.conf
---> Using cache
---> 2055f6e1ab67
Step 5/7 : ADD sentinel.conf /engn001/redis-stable/sentinel_7001.conf
---> Using cache
---> 2e87093ecc1b
Step 6/7 : ADD docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
---> 45302d0322cd
Step 7/7 : ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
---> Running in d76dc4496a3a
Removing intermediate container d76dc4496a3a
---> c21619894b01
Successfully built c21619894b01
Successfully tagged redis_image:slave
Building redis3
Step 1/7 : FROM redis_image:latest
---> a693415dce41
Step 2/7 : MAINTAINER kimdubi
---> Using cache
---> 5fc385f01aa0
Step 3/7 : USER root
---> Using cache
---> d4549d967334
Step 4/7 : ADD redis.conf /engn001/redis-stable/redis_6001.conf
---> Using cache
---> 2055f6e1ab67
Step 5/7 : ADD sentinel.conf /engn001/redis-stable/sentinel_7001.conf
---> Using cache
---> 2e87093ecc1b
Step 6/7 : ADD docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
---> Using cache
---> 45302d0322cd
Step 7/7 : ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
---> Using cache
---> c21619894b01
Successfully built c21619894b01
Successfully tagged redis_image:slave
Creating redis_master ... done
Creating redis_slave2 ... done
Creating redis_slave1 ... done
결과
- replication
[root@4295e99809a9 /]# redis-cli -p 6001 -a qhdks123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6001> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.19.0.22,port=6001,state=online,offset=731969,lag=1
slave1:ip=172.19.0.21,port=6001,state=online,offset=731969,lag=1
master_replid:1017909b02914bc2a85a92b766852f7ea057b261
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:732105
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:67108864
repl_backlog_first_byte_offset:1
repl_backlog_histlen:732105
- sentinel
[root@4295e99809a9 /]# redis-cli -p 7001 -a qhdks123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:7001> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=172.19.0.20:6001,slaves=2,sentinels=3