Monitoring

S3에 백업된 Prometheus 데이터를 Thanos Query와 Grafana로 조회하기

황동리 2025. 11. 8. 16:16
반응형

이전 글에서 Thanos 사용하여 Prometheus가 수집한 데이터를 s3 버킷으로 백업을 시켯습니다.
[https://ksh-cloud.tistory.com/184](Prometheus 가 수집한 메트릭 장기간 보관하기 — s3, Thanos)


그런데, 이 백업 시킨 과거의 데이터는 그냥 쿼리를 해서 사용을 할 수가 없습니다.


이러한 문제를 해결하기 위해, 아래 과정을 거칩니다.

  1. Thanos Store Gateway가 S3 블록을 읽어들이도록 설정
  2. Querier가 Store Gateway(+Sidecar) 를 “store API”로 붙도록 설정
  3. Grafana 데이터 소스를 Querier 로 교체

Thanos Store Gateway 란?

🎯 “과거 데이터를 다시 쿼리하기 위한 리더(reader)”


Thanos Store Gateway는 객체 스토리지(S3 등)에 저장된 TSDB 블록을 읽어서
Thanos Querier 등 다른 컴포넌트에 gRPC로 데이터를 제공하는 역할을 합니다.

🔄 데이터 흐름 요약

  1. Prometheus + Thanos Sidecar
  • Prometheus는 PVC에 실시간 데이터 저장
  • Thanos Sidecar는 일정 주기로 TSDB 블록을 생성하면 이를 S3로 업로드
  1. S3에 저장된 TSDB 블록
  • Prometheus가 오래된 블록은 삭제해도
  • S3에는 보존됨 (무제한 장기 보관 가능)
  1. Thanos Store Gateway
  • S3에 저장된 블록들을 Index + Chunk 형태로 읽음
  • gRPC 서버를 통해 Querier가 요청할 때 데이터를 응답함

Thanos Store Gateway 생성

  1. Thanos 네임스페이스 생성
kubectl create ns thanos
  1. Secret 생성
vim thanos-storage-config.yaml
---
type: S3
config:
  bucket: "dev-outcode-monitor-s3-bucket"
  endpoint: "s3.ap-northeast-1.amazonaws.com"
  region: "ap-northeast-1"
  access_key: "AKIAUP3EHOPETUEZKP4J"  
  secret_key: "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"
---

# 아래 명령어로 thanos 네임스페이스에서 사용할 Secret 생성
kubectl -n thanos create secret generic thanos-objstore-config --from-file=thanos.yaml=thanos-storage-config.yaml
  1. Thanos 설치 전 values.yaml 파일 수정
############################################
# A.  Store Gateway 활성화
############################################
storegateway:
  enabled: true

  ## 로컬 캐시용 PVC 필요 없으면 꺼도 됨
  persistence:
    enabled: true
    storageClass: "ebs-gp3-sc"
    size: 50Gi

  ## 서비스 노출 (Querier 가 gRPC 로 접근)
  service:
    type: ClusterIP
    ports:
      grpc: 10901
      http: 9090


############################################
# B.  Querier 가 Store Gateway를 바라보도록
############################################
query:
  enabled: true

  # ① DNS 디스커버리 방식을 쓸 경우
  dnsDiscovery:
    enabled: true
    sidecarsService: "prometheus-operated"             # (Sidecar만 검색하지 않을 거면 비워둠)
    sidecarsNamespace: "monitor"

  # ② Static store 목록을 직접 적는 쪽이 더 단순
  stores:
    # gRPC 주소 - Kubernetes 서비스명을 사용
    - dnssrv+_grpc._tcp.thanos-storegateway.thanos.svc.cluster.local

  ## (선택) downsampling/partial-response
  extraFlags:
    - --query.auto-downsampling
    - --query.partial-response

  ## Querier 서비스를 Grafana 가 접근할 수 있게
  service:
    type: ClusterIP
    ports:
      http: 9090

#############################################################################
C. 공통 S3 Secret 지정
#############################################################################
existingObjstoreSecret: thanos-objstore-config
existingObjstoreSecretItems:
  - key: thanos.yaml      # Secret 안 key
    path: objstore.yml    # 컨테이너 내부 마운트 파일명 (chart 규칙)
  1. helm 을 사용하여 Thanos 설치
helm repo add bitnami https://charts.bitnami.com/bitnami
helm install my-thanos bitnami/thanos --version 17.2.1 -f values.yaml
  1. Grafana 접속해서 Data Sources 등록


Connection에는 thanos-query의 서비스 도메인을 적어주면 됩니다.


그리고 save & test를 눌러 저장을 해줍니다.


이제 대시보드에 가서 Data source를 thanos로 변경해주고 잘나오는지 확인해주면 됩니다.

반응형