개발놀이터

Redis Sentinel 장애대응 Notification 구축하기 본문

CS 지식/데이터베이스

Redis Sentinel 장애대응 Notification 구축하기

마늘냄새폴폴 2024. 7. 17. 21:38

https://coding-review.tistory.com/533

 

Redis Sentinel 도커 배포하기

대기열 만들기를 진행한게 벌써 10일 전이네요... Redis Sentinel 정말 골치아픈 녀석이었습니다.  이번 포스팅에선 Redis Sentinel을 도커로 배포하면서 삽질했던 부분들을 정리하고자 글을 쓰게 되었

coding-review.tistory.com

 

이 포스팅에 대부분의 세팅이 담겨있습니다! 위의 링크를 참고해주세요. 

 

이번 배포에서 달라진 것이라면 슬랙과 연동하는 것입니다. Sentinel에선 모니터링과 더불어 알람을 제공해줍니다. 특정 상황이 되면 쉘 스크립트 파일을 실행하는 것이죠. 

 

바로 시작해보죠!

 

#!/bin/bash

EVENT_TYPE=$1
MASTER_NAME=$2
MASTER_IP=$3
MASTER_PORT=$4

echo "alert!!!"

if [ "$EVENT_TYPE" == "+sdown" ]; then
    curl -X POST -H 'Content-type: application/json' --data '{"text":"redis master node is subjectively down"}' https://hooks.slack.com/services/xxxxxxxxx/ooooooooooooo/uuuuuuuuuuuu
elif [ "$EVENT_TYPE" == "+odown" ]; then
    curl -X POST -H 'Content-type: application/json' --data '{"text":"redis master node is objectively down"}' https://hooks.slack.com/services/xxxxxxxxx/ooooooooooooo/uuuuuuuuuuuu
elif [ "$EVENT_TYPE" == "+switch-master" ]; then
    curl -X POST -H 'Content-type: application/json' --data '{"text":"new redis master elect other slave"}' https://hooks.slack.com/services/xxxxxxxxx/ooooooooooooo/uuuuuuuuuuuu
fi

 

일단 sentinel 폴더에 alert.sh를 만들어줍니다. 

 

version: '3.8'

services:
  was:
    image: was
    container_name: "was"
    ports:
      - "8080:8080"
    networks:
      - redis-net

  redis-master:
    image: redis:latest
    command: redis-server
    container_name: "redis-master"
    networks:
      - redis-net

  redis-slave-1:
    image: redis:latest
    command: redis-server --slaveof redis-master 6379
    links:
      - redis-master
    container_name: "redis-slave1"
    networks:
      - redis-net

  redis-slave-2:
    image: redis:latest
    command: redis-server --slaveof redis-master 6379
    links:
      - redis-master
    container_name: "redis-slave2"
    networks:
      - redis-net

  sentinel-1:
    build: sentinel
    ports:
      - "5000:26379"
    #image: sentinel
    env_file:
      - .env
    depends_on:
      - redis-master
      - redis-slave-1
      - redis-slave-2
    container_name: "sentinel1"
    volumes:
      - "./sentinel/alert.sh:/etc/redis/alert.sh"
    networks:
      - redis-net

  sentinel-2:
    build: sentinel
    ports:
      - "5001:26379"
      #image: sentinel
    env_file:
      - .env
    depends_on:
      - redis-master
      - redis-slave-1
      - redis-slave-2
    container_name: "sentinel2"
    volumes:
      - "./sentinel/alert.sh:/etc/redis/alert.sh"
    networks:
      - redis-net

  sentinel-3:
    build: sentinel
    ports:
      - "5002:26379"
      #image: sentinel
    env_file:
      - .env
    depends_on:
      - redis-master
      - redis-slave-1
      - redis-slave-2
    container_name: "sentinel3"
    volumes:
      - "./sentinel/alert.sh:/etc/redis/alert.sh"
    networks:
      - redis-net
networks:
  redis-net:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16

 

docker-compose.yml 에 sentinel 컨테이너에 volumes로 sentinel 내부에 쉘스크립트 파일을 위치시켜줍니다. 

 

그리고 Dockerfile을 수정해줍니다. 

 

FROM redis:latest

EXPOSE 26379

ADD sentinel.conf /etc/redis/sentinel.conf

RUN apt-get update && apt-get install -y curl

RUN mkdir -p /var/lib/redis && \
    chmod 777 /var/lib/redis && \
    chown redis:redis /etc/redis/sentinel.conf

COPY sentinel-entrypoint.sh /usr/local/bin/

RUN chmod +x /usr/local/bin/sentinel-entrypoint.sh

ENTRYPOINT ["sentinel-entrypoint.sh"]

 

추가된건 apt-get update와 curl을 설치하는 부분입니다. 슬랙으로 보내는 부분이 curl로 json을 요청하기 때문이죠. 

 

# Example sentinel.conf can be downloaded from http://download.redis.io/redis-stable/sentinel.conf

port 26379

dir /tmp

sentinel resolve-hostnames yes

sentinel notification-script mymaster /etc/redis/alert.sh 

sentinel monitor mymaster redis-master 6379 $SENTINEL_QUORUM

sentinel down-after-milliseconds mymaster $SENTINEL_DOWN_AFTER

sentinel parallel-syncs mymaster 1

sentinel failover-timeout mymaster $SENTINEL_FAILOVER

bind 0.0.0.0

 

그리고 sentinel.conf에 sentinel notification-script 부분을 추가해줍니다. 

 

그럼 이제 준비 끝!

 

이제 슬랙을 설치해줍니다. 

 

https://slack.com/intl/ko-kr/downloads/windows

 

Windows | 다운로드

모바일 기기나 데스크톱에 Slack을 무료로 다운로드하세요. iOS, Android, Mac, Windows 및 Linux용 앱으로 대화를 이어나가세요.

slack.com

 

그리고 채널을 만들어줍니다. 

 

저는 server_notification으로 만들었습니다. 

 

쭉쭉 진행해주시면 됩니다. 

 

https://api.slack.com/messaging/webhooks

 

Sending messages using incoming webhooks

Create an incoming webhook with a unique URL to which you send a JSON payload with message text and options.

api.slack.com

 

그리고 위의 링크에서 슬랙 웹훅을 설정해줘야합니다. 

 

안에 들어가시면 create a slack app 이라는 버튼이 있습니다. 그걸 눌러주시면?

 

 

워크 스페이스는 아까 슬랙에서 만든걸 선택해주면 됩니다. 

 

 

그리고 Incoming Webhooks를 선택해줍니다. 

 

 

Activate Incoming Webhooks를 On으로 바꿔주시면 아래와 같은 창이 나옵니다.

 

 

 

이제 이렇게 나온 화면에서 Copy를 누르면 링크가 복사됩니다. 

 

그걸 alert.sh에 적어주면 됩니다. 

 

그리고 마스터 노드를 죽여보면?

 

위의 사진은 sdown인 상태입니다.

 

 

 

이렇게 알람이 잘 오는 것을 확인할 수 있습니다. 

 

 

마치며

알람이랑 연결한 것은 또 처음이네요. 정말 재밌는 프로젝트였습니다. 이제 진짜 개발자같이 보이긴 하네요! 뿌듯하게 마무리 지으면서 포스팅도 마무리 지어보겠습니다. 긴 글 읽어주셔서 감사합니다. 오늘도 즐거운 하루 되세요!