Myo-Kyeong Tech Blog

[Kubernetes] Prometheus & Alertmanager 로 쿠버네티스 임계치 초과 시 Slack 알림 받기 본문

DevOps/Kubernetes

[Kubernetes] Prometheus & Alertmanager 로 쿠버네티스 임계치 초과 시 Slack 알림 받기

myo-kyeong 2025. 3. 19. 00:55
728x90
반응형

 

PrometheusAlertmanager를 활용하여 쿠버네티스(Kubernetes) 시스템 리소스(CPU, 메모리, 디스크)가 특정 임계치를 초과했을 때 Slack (또는 Email, MS Teams 등) 으로 알람을 전송하는 방법에 대해 정리한 글입니다. 

 


 

Prometheus + Alertmanager 알람 전송 흐름 

 

출처 : https://kschoi728.tistory.com/73

 

  1. Prometheus가 메트릭을 수집 (CPU 사용률, 메모리 사용량, 디스크 사용량 등)
  2. PrometheusRule을 통해 특정 임계치를 초과하는 경우 알람을 발생
  3. Alertmanager로 알람을 전송
  4. AlertmanagerAlertmanagerConfig를 참고하여 알람을 Slack (또는  Email, MS Teams 등) 으로 전송

 

다음과 같이 쿠버네티스에서 임계치 초과 알람을 설정하려면 두 가지 구성 요소가 필요합니다.

  • PrometheusRule → 어떤 조건에서 알람을 발생시킬지 정의 (예: CPU 사용률이 80%를 넘으면 알람 발생)
  • AlertmanagerConfig → 알람을 어디로, 어떻게 보낼지 설정 (Slack, Email, MS Teams 등으로 알람 전송)

 

 

PrometheusRule 설정 (알람 트리거 조건)

  • 특정 임계치 초과했을 때 Prometheus가 알람 발생하도록 PrometheusRule을 설정
  • 예제 : CPU 사용률이 80% 초과 -> 알람 발생
    • prometheus-rule.yaml (CPU 임계치 알람 설정) 
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: node-cpu-usage   # 알람 규칙 이름
  namespace: monitoring
spec:
  groups: 
    - name: node_cpu_alerts
      rules:
        - alert: HighCPUUsage  # 알람 이름 설정
          expr: 100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80
          for: 5m
          labels:
            severity: critical
          annotations:
            summary: "High CPU Usage on {{ $labels.instance }}"
            description: "CPU usage on {{ $labels.instance }} exceeded 80% for more than 5 minutes."
  • 설명
    • expr : CPU 사용률이 80%를 초과하는지 확인하는 PromQL 쿼리
    • for: 5m: 5분 동안 유지되면 알람 발생 ( 예 : 5분 동안 CPU 사용률이 80%를 초과하면 알람 발생 ) 
    • labels: 알람 심각도 (critical)
    • annotations: Slack 에서 보일 내용
  • 설정 적용 
kubectl apply -f prometheus-rule.yaml

다음과 같이 설정을 적용하면 이제 CPU 사용률이 80% 넘기면 Alertmanager로 알람을 보냅니다!

 

 

AlertmanagerConfig 설정 (Slack으로 알람 전송)

  • Prometheus가 감지한 알람을 Alertmanager가 Slack으로 보낼 수 있도록 설정
  • 예제 : Slack으로 알람 전송
    • alertmanager-config.yaml (Slack 알람 설정)
apiVersion: monitoring.coreos.com/v1alpha1
kind: AlertmanagerConfig
metadata:
  name: slack-alerts  # 알람 이름 설정
  namespace: monitoring
spec:
  route:
    receiver: slack-notifications  
    group_by: ['alertname']  
    repeat_interval: 1h  # 같은 알람이 1시간에 한 번만 오도록 설정
  receivers:  
    - name: 'slack-notifications'
      slackConfigs:
        - send_resolved: true
          channel: '#alerts'  # Slack에서 알람을 받을 채널 이름 (#alerts)
          apiURL:
            name: slack-webhook
            key: webhook-url
          title: '{{ .CommonAnnotations.summary }}'
          text: '{{ .CommonAnnotations.description }}'
  • 설명
    • repeat_interval: 1h: 같은 알람이 다시 발생해도 1시간 동안 추가 알람을 보내지 않음 (알람이 너무 자주 울리지 않도록 설정 가능)
    • slackConfigs: Slack Webhook URL을 이용해 알람 전송
    • send_resolved: true: 알람이 해제되었을 때도 Slack에 메시지 전송
  • 설정 적용
kubectl apply -f alertmanager-config.yaml

 

 

728x90
반응형