(Grafana 공식 문서) Grafana를 코드 기반으로 자동화(provisioning)

Category
log-central
Status
Published
Tags
Grafana
Provisioning
Description
Published
Slug

[1] 튜토리얼 문서 – Provision dashboards and data sources

  • Grafana를 수동 설정 없이 코드로 자동화
  • 컨테이너나 CI/CD 환경에서 필수
 

설정 파일 위치

  • Grafana 설정은 기본적으로 /etc/grafana/provisioning/ 경로에 있음
    • datasources/: 데이터 소스 설정
    • dashboards/: 대시보드 설정
    • notifiers/: 알림 채널 설정
    • alerting/: 알림 룰 설정
    •  

1. 데이터 소스 자동 등록

파일 위치: /etc/grafana/provisioning/datasources/datasource.yaml
apiVersion: 1 datasources: - name: Prometheus type: prometheus access: proxy url: http://prometheus:9090 isDefault: true
의미
  • Prometheus라는 이름의 데이터 소스를 http://prometheus:9090에 연결
  • 기본(default) 데이터 소스로 설정
 

2. 대시보드 자동 등록

파일 위치: /etc/grafana/provisioning/dashboards/dashboard.yaml
apiVersion: 1 providers: - name: 'default' folder: '프로비저닝된 대시보드' type: file disableDeletion: false options: path: /var/lib/grafana/dashboards
대시보드 JSON 파일 위치 예시
  • /var/lib/grafana/dashboards/my-dashboard.json
의미
  • Grafana는 지정된 폴더에서 .json 대시보드 파일을 자동으로 불러옴
  • 대시보드가 '프로비저닝된 대시보드'라는 폴더에 들어감
 

[2] 공식 문서 – Provisioning 문서

 

데이터 소스(Data Sources)

  • 파일 위치: /etc/grafana/provisioning/datasources/*.yaml
  • 설정 예
datasources: - name: Loki type: loki url: http://loki:3100 jsonData: maxLines: 1000
  • 다양한 데이터 소스 지원: Prometheus, Loki, Elasticsearch 등
 

대시보드(Dashboards)

  • 파일 위치: /etc/grafana/provisioning/dashboards/*.yaml
  • type: file이면 .json 파일을 읽고 등록
  • type: plugin이면 플러그인 기반 대시보드 로딩
providers: - name: default folder: Provisioned type: file options: path: /var/lib/grafana/dashboards
  • 폴더 구조로 구분 가능
  • JSON은 Grafana에서 Export한 파일 그대로 사용 가능
 

알림 채널(Notifiers)

  • Slack, Email 등 알림 채널도 코드로 설정 가능
notifiers: - name: slack-channel type: slack uid: slack-notifier is_default: true settings: url: https://hooks.slack.com/services/XXX
 

알림 룰(Alerting)

  • Grafana 9 이상부터는 Alert Rules도 코드로 정의 가능
  • groups로 묶어서 조건/대상/수신처 설정
groups: - name: example-group folder: Alerts interval: 1m rules: - uid: high-cpu title: High CPU usage condition: B > 80 data: - refId: B queryType: timeSeriesQuery relativeTimeRange: from: 300 to: 0
 

실제 디렉터리 구조 예시

/etc/grafana/provisioning/ ├── dashboards/ │ └── dashboard.yaml ├── datasources/ │ └── datasource.yaml ├── notifiers/ │ └── notifier.yaml ├── alerting/ │ └── alerts.yaml

사용 시나리오 요약

기능
설정 파일
주요 속성
목적
데이터 소스
datasources/*.yaml
name, type, url
수집기 자동 연결
대시보드
dashboards/*.yaml
path, folder
시각화 자동 구성
알림 채널
notifiers/*.yaml
type, url
Slack 등 알림
알림 룰
alerting/*.yaml
rules, condition
경고 자동 설정

  • 프로비저닝으로 만든 리소스는 Grafana UI에서 삭제할 수 없음 (수정은 불가, 삭제는 비활성화)
  • 설정이 바뀌면 Grafana를 재시작 또는 reload API 호출 필요
    • /api/admin/provisioning/dashboards/reload