🎛️ Prometheus ConfigMap 관리 및 최신버전 가이드
📑 목차
🔧 ConfigMap으로 Prometheus 설정 관리
💡 ConfigMap 기반 설정의 장점
왜 ConfigMap인가?
- 버전 관리: Git을 통한 설정 변경 이력 추적
- 롤백 가능: 잘못된 설정 즉시 되돌리기
- 환경별 관리: dev/staging/prod 환경별 다른 설정
- 실시간 적용: Pod 재시작 없이 설정 갱신 가능
🏗️ ConfigMap 구조 분석
아까 오늘 실습에서 사용한 구조를 개선해보겠습니다:
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-config
namespace: monitoring
labels:
app: prometheus
component: server
data:
prometheus.yml: |
# 🎯 글로벌 설정
global:
scrape_interval: 15s # 기본 수집 주기
evaluation_interval: 15s # 규칙 평가 주기
scrape_timeout: 10s # 수집 타임아웃
external_labels: # 외부 시스템용 라벨
cluster: 'production'
region: 'ap-northeast-2'
# 🚨 알람 규칙 파일
rule_files:
- /etc/prometheus/rules/*.yml
# 🎯 스크래핑 설정
scrape_configs:
# === 내부 모니터링 ===
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
scrape_interval: 5s # 자기 자신은 더 자주
- job_name: 'alertmanager'
static_configs:
- targets: ['alertmanager:9093']
# === 쿠버네티스 기본 모니터링 ===
- job_name: 'kubernetes-apiservers'
kubernetes_sd_configs:
- role: endpoints
scheme: https
tls_config:
ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
relabel_configs:
- source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
action: keep
regex: default;kubernetes;https
- job_name: 'kubernetes-nodes'
kubernetes_sd_configs:
- role: node
scheme: https
tls_config:
ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
insecure_skip_verify: true
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
relabel_configs:
- action: labelmap
regex: __meta_kubernetes_node_label_(.+)
- target_label: __address__
replacement: kubernetes.default.svc:443
- source_labels: [__meta_kubernetes_node_name]
regex: (.+)
target_label: __metrics_path__
replacement: /api/v1/nodes/${1}/proxy/metrics
# === 외부 서비스 모니터링 ===
- job_name: 'harbor'
static_configs:
- targets:
- 'host.docker.internal:9090' # 오늘 실습한 설정
metrics_path: '/metrics'
scrape_interval: 30s # Harbor는 30초마다
scrape_timeout: 10s
# === 애플리케이션 모니터링 ===
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
# prometheus.io/scrape: "true" 어노테이션이 있는 pod만 수집
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true
# 포트 설정
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace
regex: (.+)
target_label: __address__
replacement: ${1}
# 메트릭 경로 설정
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
# 🎯 알람 매니저 연동
alerting:
alertmanagers:
- static_configs:
- targets: ['alertmanager:9093']
# 📊 기본 알람 규칙
alerts.yml: |
groups:
- name: basic-alerts
rules:
- alert: HighCPUUsage
expr: 100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80
for: 5m
labels:
severity: warning
annotations:
summary: "High CPU usage detected"
description: "CPU usage is above 80% for more than 5 minutes"
- alert: HighMemoryUsage
expr: (1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)) * 100 > 85
for: 5m
labels:
severity: critical
annotations:
summary: "High memory usage detected"
description: "Memory usage is above 85% for more than 5 minutes"⚡ 실시간 설정 변경 방법
🔄 ConfigMap 수정 및 적용
방법 1: kubectl edit 사용 (빠른 수정)
# ConfigMap 직접 편집
kubectl edit configmap prometheus-config -n monitoring
# 변경 사항 확인
kubectl get configmap prometheus-config -n monitoring -o yaml방법 2: YAML 파일 업데이트 (권장)
# 1. 기존 설정 백업
kubectl get configmap prometheus-config -n monitoring -o yaml > prometheus-config-backup.yaml
# 2. 새 설정 적용
kubectl apply -f prometheus-config-updated.yaml
# 3. 변경 사항 확인
kubectl describe configmap prometheus-config -n monitoring방법 3: kubectl patch 사용 (부분 수정)
# Harbor 타겟만 변경 (예시)
kubectl patch configmap prometheus-config -n monitoring --type merge -p='
{
"data": {
"prometheus.yml": "global:\n scrape_interval: 15s\nscrape_configs:\n- job_name: harbor\n static_configs:\n - targets: [\"harbor.example.com:9090\"]\n"
}
}'🔄 설정 리로드 방법
자동 리로드 (권장 - 최신 Prometheus v2.45+)
# 1. Prometheus에 리로드 신호 보내기
kubectl exec -it prometheus-server-pod -n monitoring -- \
kill -HUP $(pidof prometheus)
# 또는 HTTP API 사용
kubectl port-forward svc/prometheus-server 9090:80 -n monitoring &
curl -X POST http://localhost:9090/-/reloadPod 재시작 (확실한 방법)
# Prometheus 서버 재시작
kubectl delete pod -l app=prometheus-server -n monitoring
# 상태 확인
kubectl get pods -n monitoring -w🎯 타겟 관리 완벽 가이드
📋 타겟 추가/제거 패턴
1. 정적 타겟 추가
# 새로운 외부 서비스 추가
scrape_configs:
- job_name: 'my-new-service'
static_configs:
- targets:
- 'service1.example.com:9090'
- 'service2.example.com:9090'
scrape_interval: 30s
metrics_path: '/actuator/prometheus' # Spring Boot
params:
format: ['prometheus']2. 동적 타겟 추가 (Service Discovery)
# 쿠버네티스 서비스 자동 발견
- job_name: 'kubernetes-services'
kubernetes_sd_configs:
- role: service
relabel_configs:
# 특정 어노테이션이 있는 서비스만 모니터링
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]
action: keep
regex: true
# 포트 매핑
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_port]
action: replace
target_label: __address__
regex: (.+)
replacement: $1🏷️ 라벨링 전략
# 환경별, 서비스별 라벨링
scrape_configs:
- job_name: 'web-servers'
static_configs:
- targets: ['web1:9090', 'web2:9090']
labels:
env: 'production'
team: 'frontend'
tier: 'web'
- targets: ['web-dev1:9090']
labels:
env: 'development'
team: 'frontend'
tier: 'web'🚫 타겟 필터링
# 특정 조건의 타겟만 수집
- job_name: 'filtered-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
# 특정 네임스페이스만
- source_labels: [__meta_kubernetes_namespace]
action: keep
regex: 'production|staging'
# 특정 라벨이 있는 pod만
- source_labels: [__meta_kubernetes_pod_label_monitoring]
action: keep
regex: 'enabled'
# 불필요한 라벨 제거
- regex: '__meta_kubernetes_pod_label_pod_template_.*'
action: labeldrop⏰ 수집 주기 및 성능 튜닝
🎛️ 수집 주기 최적화
서비스별 맞춤 설정
global:
scrape_interval: 15s # 기본값
scrape_configs:
# 중요한 시스템 - 더 자주
- job_name: 'critical-systems'
scrape_interval: 5s
static_configs:
- targets: ['database:9090', 'api-gateway:9090']
# 일반 애플리케이션 - 기본
- job_name: 'applications'
scrape_interval: 15s
static_configs:
- targets: ['app1:9090', 'app2:9090']
# 배치 작업 - 덜 자주
- job_name: 'batch-jobs'
scrape_interval: 60s
static_configs:
- targets: ['batch1:9090', 'batch2:9090']성능 고려사항
# 대용량 환경 최적화
global:
scrape_interval: 30s # 타겟이 많을 때 늘리기
scrape_timeout: 10s # 네트워크 지연 고려
evaluation_interval: 30s # 규칙 평가도 함께 조정
# 메트릭 크기 제한
scrape_configs:
- job_name: 'large-app'
static_configs:
- targets: ['large-app:9090']
metric_relabel_configs:
# 불필요한 메트릭 제거
- source_labels: [__name__]
regex: 'go_.*|process_.*' # Go runtime 메트릭 제거
action: drop
# 높은 cardinality 메트릭 제거
- source_labels: [__name__]
regex: 'http_requests_total'
target_label: __tmp_keep
replacement: 'yes'
- source_labels: [__tmp_keep, status_code]
regex: 'yes;[45].*' # 4xx, 5xx 에러만 유지
action: keep📊 스토리지 및 보존 정책
# Prometheus 서버 설정 (deployment.yaml에서)
args:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus/'
- '--storage.tsdb.retention.time=30d' # 30일간 보존
- '--storage.tsdb.retention.size=50GB' # 최대 50GB
- '--web.console.libraries=/etc/prometheus/console_libraries'
- '--web.console.templates=/etc/prometheus/consoles'
- '--web.enable-lifecycle' # 리로드 API 활성화
- '--web.enable-admin-api' # 관리 API 활성화🚀 Prometheus 최신버전 (v2.50+) 주요 변화
📅 최신 버전 타임라인
Prometheus 릴리스 정보 (2025년 기준)
- v2.50.x (2025.01): 현재 최신 LTS
- v2.49.x (2024.12): 이전 안정 버전
- v2.48.x (2024.11): 성능 개선 주력 버전
⚡ v2.50+ 주요 개선사항
1. 네이티브 히스토그램 지원 (혁신적!)
# 기존 방식 (버킷 기반)
histogram_quantile(0.95,
rate(http_request_duration_seconds_bucket[5m])
)
# 새로운 방식 (네이티브 히스토그램)
histogram_quantile(0.95,
rate(http_request_duration_seconds[5m])
)장점:
- 메모리 사용량 90% 감소
- 더 정확한 quantile 계산
- 동적 해상도 조정
2. PromQL 향상된 함수들
# 새로운 함수: histogram_count(), histogram_sum()
histogram_count(http_request_duration_seconds)
histogram_sum(http_request_duration_seconds)
# 개선된 함수: absent_over_time()
absent_over_time(up[1h]) # 1시간 동안 데이터가 없었는지 확인
# 새로운 함수: sgn() - 부호 함수
sgn(cpu_usage - 50) # CPU 사용량이 50% 초과면 1, 미만이면 -13. OTLP 네이티브 지원
# OpenTelemetry 메트릭 직접 수신 가능
scrape_configs:
- job_name: 'otlp-metrics'
static_configs:
- targets: ['app:4318'] # OTLP HTTP 엔드포인트
metrics_path: '/v1/metrics'
scrape_interval: 15s4. 향상된 메모리 관리
# 새로운 설정 옵션들
global:
# 메모리 사용량 제한
query_max_samples: 50000000 # 쿼리당 최대 샘플 수
# 스토리지 압축 개선
storage:
tsdb:
compression: 'snappy' # 기본 압축 알고리즘
wal_compression: true # WAL 압축 활성화🔧 v2.50+ 마이그레이션 가이드
설정 업데이트
# 1. 기존 Prometheus 버전 확인
kubectl exec -it prometheus-server-pod -n monitoring -- prometheus --version
# 2. 백업 생성
kubectl create backup prometheus-backup-$(date +%Y%m%d)
# 3. Helm 차트 업데이트
helm repo update
helm upgrade prometheus prometheus-community/prometheus \
--version 27.50.0 \
--set image.tag=v2.50.1 \
-n monitoring호환성 체크리스트
- PromQL 쿼리: 기존 쿼리가 새 버전에서 동작하는지 확인
- 알람 규칙: 새로운 함수로 최적화 가능한지 검토
- 대시보드: Grafana 대시보드 호환성 확인
- API 클라이언트: 외부 시스템의 API 호출 확인
💡 실무 베스트 프랙티스
🎯 ConfigMap 관리 전략
1. 환경별 ConfigMap 분리
# 개발 환경
prometheus-config-dev
├── scrape_interval: 30s
├── retention: 7d
└── targets: dev 서비스들
# 스테이징 환경
prometheus-config-staging
├── scrape_interval: 15s
├── retention: 14d
└── targets: staging 서비스들
# 프로덕션 환경
prometheus-config-prod
├── scrape_interval: 15s
├── retention: 30d
└── targets: 모든 프로덕션 서비스2. Git을 통한 설정 관리
# GitOps 방식으로 설정 관리
prometheus-configs/
├── environments/
│ ├── dev/prometheus-config.yaml
│ ├── staging/prometheus-config.yaml
│ └── prod/prometheus-config.yaml
├── rules/
│ ├── common-alerts.yaml
│ ├── application-alerts.yaml
│ └── infrastructure-alerts.yaml
└── scripts/
├── deploy.sh
└── validate.sh3. 설정 검증 자동화
#!/bin/bash
# validate-prometheus-config.sh
echo "🔍 Prometheus 설정 검증 중..."
# 1. YAML 문법 검증
yamllint prometheus-config.yaml
# 2. Prometheus 설정 파일 검증
docker run --rm -v $(pwd):/workspace \
prom/prometheus:latest \
promtool check config /workspace/prometheus.yml
# 3. 알람 규칙 검증
docker run --rm -v $(pwd):/workspace \
prom/prometheus:latest \
promtool check rules /workspace/alerts.yml
echo "✅ 설정 검증 완료!"📊 모니터링 모범 사례
1. 라벨 설계 원칙
# ✅ 좋은 라벨 설계
labels:
service: "user-api"
version: "v1.2.3"
environment: "production"
team: "backend"
# ❌ 피해야 할 라벨 설계
labels:
user_id: "12345" # 높은 cardinality
request_id: "abc-123" # 매번 바뀌는 값
timestamp: "2025-12-08" # 시간 정보2. 알람 규칙 계층화
groups:
# 1계층: 인프라 핵심 알람
- name: infrastructure-critical
rules:
- alert: NodeDown
expr: up{job="node-exporter"} == 0
for: 1m
labels:
severity: critical
# 2계층: 애플리케이션 알람
- name: application-warning
rules:
- alert: HighLatency
expr: histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m])) > 0.5
for: 5m
labels:
severity: warning
# 3계층: 비즈니스 알람
- name: business-info
rules:
- alert: LowUserRegistration
expr: rate(user_registrations_total[1h]) < 10
for: 30m
labels:
severity: info🚀 성능 최적화 팁
1. 쿼리 최적화
# ❌ 비효율적인 쿼리
sum(rate(http_requests_total[5m])) by (instance)
# ✅ 최적화된 쿼리
sum(rate(http_requests_total[5m])) by (service, status_code)2. 메트릭 생명주기 관리
# 메트릭 정리 규칙
metric_relabel_configs:
# 개발용 메트릭 제거
- source_labels: [__name__]
regex: '.*_debug_.*'
action: drop
# 오래된 라벨 정리
- regex: 'legacy_.*'
action: labeldrop
# 불필요한 정밀도 제거
- source_labels: [__name__, value]
regex: 'temperature_celsius;(.+)'
target_label: value
replacement: '${1:.1f}' # 소수점 1자리까지만📝 실습: 오늘의 Harbor 설정을 최신 베스트 프랙티스로 업그레이드
🔄 기존 설정 → 개선된 설정
# 🚀 개선된 Harbor 모니터링 설정
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-config-v2
namespace: monitoring
labels:
version: "v2.50.1"
managed-by: "gitops"
data:
prometheus.yml: |
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_timeout: 10s
external_labels:
cluster: 'local-dev'
prometheus_replica: 'prometheus-1'
scrape_configs:
# === 기본 모니터링 (개선) ===
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
scrape_interval: 5s
# === Harbor 모니터링 (개선) ===
- job_name: 'harbor'
static_configs:
- targets: ['host.docker.internal:9090']
metrics_path: '/metrics'
scrape_interval: 30s
scrape_timeout: 15s
params:
format: ['prometheus']
metric_relabel_configs:
# Harbor 관련 메트릭만 수집
- source_labels: [__name__]
regex: 'harbor_.*|registry_.*|docker_.*'
action: keep
# 불필요한 라벨 정리
- regex: '__meta_.*'
action: labeldrop
relabel_configs:
# 서비스 정보 추가
- target_label: service
replacement: 'harbor'
- target_label: team
replacement: 'platform'
# === 알람 규칙 (추가) ===
rule_files:
- '/etc/prometheus/alerts.yml'
alerts.yml: |
groups:
- name: harbor-monitoring
rules:
- alert: HarborDown
expr: up{job="harbor"} == 0
for: 2m
labels:
severity: critical
service: harbor
annotations:
summary: "Harbor service is down"
description: "Harbor has been down for more than 2 minutes"
- alert: HarborHighLatency
expr: harbor_request_duration_seconds{quantile="0.95"} > 1
for: 5m
labels:
severity: warning
service: harbor
annotations:
summary: "Harbor high latency detected"
description: "Harbor 95th percentile latency is {{ $value }}s"🔧 적용 방법
# 1. 새 ConfigMap 적용
kubectl apply -f prometheus-config-v2.yaml
# 2. Prometheus 리로드
kubectl port-forward svc/prometheus-server 9090:80 -n monitoring &
curl -X POST http://localhost:9090/-/reload
# 3. 설정 확인
kubectl logs -f prometheus-server-pod -n monitoring이제 Prometheus 설정 관리가 훨씬 체계적이고 최신 기술을 반영하게 되었습니다! 🚀