본문 바로가기

리눅스

도커 컨테이너로 레디스 센티널(Redis Sentinel)을 구성하는 방법

반응형

도커 컨테이너로 레디스 센티널(Redis Sentinel)을 구성하는 방법

docker-compose.yaml 파일 생성

더보기
vim docker-compose.yaml
version: '3.9'
services:

#####master
  redis-master:
    image: bitnami/redis:latest
    container_name: redis-master
    hostname: redis-master
    environment:
      - REDIS_REPLICATION_MODE=master
      - ALLOW_EMPTY_PASSWORD=yes
    volumes:
      - ./data/master:/bitnami/redis
    ports:
      - 6379:6379
    networks:
      - redis-network

#####slave
  redis-slave-1:
    image: bitnami/redis:latest
    container_name: redis-slave-1
    hostname: redis-slave-1
    environment:
      - REDIS_REPLICATION_MODE=slave
      - REDIS_MASTER_HOST=redis-master
      - ALLOW_EMPTY_PASSWORD=yes
    volumes:
      - ./data/slave1:/bitnami/redis
    depends_on:
      - redis-master
    ports:
      - 6479:6379
    networks:
      - redis-network

  redis-slave-2:
    image: bitnami/redis:latest
    container_name: redis-slave-2
    hostname: redis-slave-2
    environment:
      - REDIS_REPLICATION_MODE=slave
      - REDIS_MASTER_HOST=redis-master
      - ALLOW_EMPTY_PASSWORD=yes
    volumes:
      - ./data/slave2:/bitnami/redis
    depends_on:
      - redis-master
    ports:
      - 6579:6379
    networks:
      - redis-network

##### sentinel
  redis-sentinel-1:
    image: bitnami/redis-sentinel:latest
    container_name: redis-sentinel-1
    hostname: redis-sentinel-1
    environment:
      - REDIS_SENTINEL_DOWN_AFTER_MILLISECONDS=3000
      - REDIS_MASTER_HOST=redis-master
      - REDIS_MASTER_PORT_NUMBER=6379
      - REDIS_MASTER_SET=mymaster
      - REDIS_SENTINEL_QUORUM=2
    depends_on:
      - redis-master
      - redis-slave-1
      - redis-slave-2
    ports:
      - 26379:26379
    networks:
      - redis-network

  redis-sentinel-2:
    image: bitnami/redis-sentinel:latest
    container_name: redis-sentinel-2
    hostname: redis-sentinel-2
    environment:
      - REDIS_SENTINEL_DOWN_AFTER_MILLISECONDS=3000
      - REDIS_MASTER_HOST=redis-master
      - REDIS_MASTER_PORT_NUMBER=6379
      - REDIS_MASTER_SET=mymaster
      - REDIS_SENTINEL_QUORUM=2
    depends_on:
      - redis-master
      - redis-slave-1
      - redis-slave-2
    ports:
      - 26380:26379
    networks:
      - redis-network

  redis-sentinel-3:
    image: bitnami/redis-sentinel:latest
    container_name: redis-sentinel-3
    hostname: redis-sentinel-3
    environment:
      - REDIS_SENTINEL_DOWN_AFTER_MILLISECONDS=3000
      - REDIS_MASTER_HOST=redis-master
      - REDIS_MASTER_PORT_NUMBER=6379
      - REDIS_MASTER_SET=mymaster
      - REDIS_SENTINEL_QUORUM=2
    depends_on:
      - redis-master
      - redis-slave-1
      - redis-slave-2
    ports:
      - 26381:26379
    networks:
      - redis-network

networks:
  redis-network:
    name: redis-network
    driver: bridge

 

컨테이너 실행

docker-compose up -d

컨테이너 목록 확인

docker-compose ps

컨테이너 역할 확인

  • redis master
docker-compose exec redis-master bash -c "redis-cli info | grep ^role"
$ docker-compose exec redis-master bash -c "redis-cli info | grep ^role"
role:master
  • redis slave
docker-compose exec redis-slave-1 bash -c "redis-cli info | grep ^role"
$ docker-compose exec redis-slave-1 bash -c "redis-cli info | grep ^role"
role:slave

$ docker-compose exec redis-slave-2 bash -c "redis-cli info | grep ^role"
role:slave
  • redis sentinel
docker-compose exec redis-sentinel-1 bash -c "redis-cli -p 26379 info | grep status"
$ docker-compose exec redis-sentinel-1 bash -c "redis-cli -p 26379 info | grep status"
master0:name=mymaster,status=ok,address=172.26.0.2:6379,slaves=2,sentinels=3

$ docker-compose exec redis-sentinel-2 bash -c "redis-cli -p 26379 info | grep status"
master0:name=mymaster,status=ok,address=172.26.0.2:6379,slaves=2,sentinels=3

$ docker-compose exec redis-sentinel-3 bash -c "redis-cli -p 26379 info | grep status"
master0:name=mymaster,status=ok,address=172.26.0.2:6379,slaves=2,sentinels=3

컨테이너 테스트

macos에 ip 명령이 없으면 여기 참고하시기를 바랍니다.

  • redis master
docker run -it --rm redis redis-cli -h $(ip route get 1.2.3.4 | awk '{ print $7 }' | egrep -v '^$') -p 6379
172.30.1.67:6379> set foo bar
OK
  • redis slave
docker run -it --rm redis redis-cli -h $(ip route get 1.2.3.4 | awk '{ print $7 }' | egrep -v '^$') -p 6479
172.30.1.67:6479> get foo
"bar"
  • redis sentinel
docker run -it --rm --network redis-network redis redis-cli -h $(ip route get 1.2.3.4 | awk '{ print $7 }' | egrep -v '^$') -p 26379
SENTINEL get-master-addr-by-name mymaster
172.30.1.67:26379> SENTINEL get-master-addr-by-name mymaster
1) "172.29.0.2"
2) "6379"

 

참고URL

- 하나하나 개발정리 : docker-compose / redis+sentinel+haproxy 세팅하기

- ip 명령어 : https://scbyun.com/1557

 

728x90
반응형