
들어가며
이번 글에서는 현대 모니터링 스택의 표준인 Prometheus와 Grafana를 활용하여 Kubernetes 환경의 MSA 서비스들을 효과적으로 모니터링하는 방법을 알아보겠습니다.
Prometheus는 SoundCloud에서 개발한 오픈소스 모니터링 시스템으로, 현재 CNCF(Cloud Native Computing Foundation)의 졸업 프로젝트입니다. 시계열 데이터베이스를 기반으로 메트릭을 수집, 저장, 조회할 수 있는 강력한 모니터링 도구입니다.
Pull 기반 메트릭 수집
다른 모니터링 시스템과 달리 Prometheus는 타겟 애플리케이션에서 메트릭을 주기적으로 가져오는(Pull) 방식을 사용합니다. 이를 통해 네트워크 문제나 서비스 장애 시에도 안정적으로 동작합니다.
Service Discovery
Kubernetes, Consul, EC2 등 다양한 환경에서 자동으로 모니터링 대상을 발견하고 추가할 수 있어 동적 환경에 최적화되어 있습니다.
PromQL (Prometheus Query Language)
강력한 쿼리 언어를 통해 복잡한 메트릭 분석과 알림 조건을 정의할 수 있습니다.
Alerting
Alertmanager와 연동하여 임계값 초과 시 다양한 채널(Slack, Email, PagerDuty 등)로 알림을 발송할 수 있습니다.

Prometheus Server
Alertmanager
Exporters
Pushgateway
Grafana는 메트릭 데이터를 시각화하는 오픈소스 대시보드 플랫폼입니다. Prometheus뿐만 아니라 InfluxDB, MySQL, PostgreSQL 등 다양한 데이터 소스를 지원하여 통합 모니터링 대시보드를 구성할 수 있습니다.
다양한 시각화 옵션
템플릿 변수
알림 기능
대시보드 공유
완벽한 모니터링 스택
클라우드 네이티브
확장성
# Prometheus와 Grafana 설치
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install prometheus prometheus-community/kube-prometheus-stack --namespace monitoring --create-namespace
kube-prometheus-stack에 포함된 구성요소
# Grafana 서비스를 LoadBalancer로 변경
kubectl patch svc prometheus-grafana -n monitoring -p '{"spec": {"type": "LoadBalancer"}}'
# Prometheus 서비스도 LoadBalancer로 변경
kubectl patch svc prometheus-kube-prometheus-prometheus -n monitoring -p '{"spec": {"type": "LoadBalancer"}}'
# 서비스 상태 확인
kubectl get svc -n monitoring prometheus-grafana prometheus-kube-prometheus-prometheus
# Grafana 관리자 패스워드 확인
kubectl get secret prometheus-grafana -n monitoring -o jsonpath="{.data.admin-password}" | base64 -d
# 결과: prom-operator
접속 정보
http://공인IP:포트번호adminprom-operator의존성 추가 (build.gradle)
// Prometheus 메트릭 수집을 위한 의존성
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'io.micrometer:micrometer-registry-prometheus'
애플리케이션 설정 (application.yml)
# Prometheus 설정 추가
management:
endpoints:
web:
exposure:
include: health,info,prometheus
endpoint:
prometheus:
enabled: true
metrics:
export:
prometheus:
enabled: true
tags:
application: ${spring.application.name}
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authorize -> authorize
.requestMatchers("/actuator/**").permitAll() // actuator 엔드포인트 허용
// 나머지 설정
);
return http.build();
}
}
랜덤 포트에서 고정 포트로 변경
# auth-service.yml
server:
port: 8081
# user-service.yml
server:
port: 8082
# health-service.yml
server:
port: 8083
# 방화벽 설정
sudo ufw allow 8081 # auth-service
sudo ufw allow 8082 # user-service
sudo ufw allow 8083 # health-service
Prometheus Operator가 사용하는 CRD(Custom Resource Definition)로, Prometheus가 어떤 서비스의 메트릭을 수집할지 정의합니다.
sudo vi msa-service-monitor.yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: msa-service-monitor
namespace: monitoring
labels:
release: prometheus
spec:
selector:
matchLabels:
app.kubernetes.io/part-of: msa
namespaceSelector:
matchNames:
- msa-apps
endpoints:
- port: http
path: /actuator/prometheus
interval: 15s
# ServiceMonitor 적용
kubectl apply -f msa-service-monitor.yaml
# 서비스에 필요한 레이블 추가
kubectl label service auth-service config-service user-service health-service gateway-service eureka-service -n msa-apps app.kubernetes.io/part-of=msa
# 서비스 포트 이름을 http로 수정
kubectl patch svc auth-service -n msa-apps --type='json' -p='[{"op": "replace", "path": "/spec/ports/0/name", "value": "http"}]'
kubectl patch svc user-service -n msa-apps --type='json' -p='[{"op": "replace", "path": "/spec/ports/0/name", "value": "http"}]'
kubectl patch svc health-service -n msa-apps --type='json' -p='[{"op": "replace", "path": "/spec/ports/0/name", "value": "http"}]'
kubectl patch svc gateway-service -n msa-apps --type='json' -p='[{"op": "replace", "path": "/spec/ports/0/name", "value": "http"}]'
kubectl patch svc config-service -n msa-apps --type='json' -p='[{"op": "replace", "path": "/spec/ports/0/name", "value": "http"}]'
kubectl patch svc eureka-service -n msa-apps --type='json' -p='[{"op": "replace", "path": "/spec/ports/0/name", "value": "http"}]'
Prometheus UI 접속: http://공인IP:9090
Grafana 접속: http://공인IP:32575
http://공인IP:9090 (Prometheus URL)추천 대시보드 ID
Import 과정
1. Import Dashboard 클릭
2. 대시보드 ID 입력 후 Load
3. Prometheus 데이터 소스 선택
4. Import 클릭
http_server_requests_totalhttp_server_requests_secondshttp_server_requests_total{status=~"4..|5.."}jvm_threads_live_threadsjvm_memory_used_bytes{area="heap"}jvm_gc_pause_secondsjvm_classes_loaded_classessystem_cpu_usagesystem_memory_usagesystem_disk_io_bytes마치며
Prometheus와 Grafana를 활용한 모니터링 시스템은 MSA 환경에서 필수적인 인프라입니다. 실시간 메트릭 수집과 시각화를 통해 시스템의 상태를 정확히 파악하고, 문제 발생 시 빠르게 대응할 수 있습니다.