1. 개요
운영을 하게 되면 항상 로직이 잘 돌아갈 수는 없다. 문제가 발생하면, 이를 해결해야 하는데 그 전에 원인을 빨리 파악하는 것이 중요하다. 이번에는 모니터링 구축을 위해 프로메테우스와 그라파나를 사용해보고자 한다.
2. 프로메테우스와 그라파나
서버를 운영할 때, CPU 사용률, 메모리 사용률, 요청 등 성능 측정을 위한 수치를 메트릭이라 하는 데 이를 저장하는 DB이다.

간단히 보면 Prometheus targets에서 메트릭을 가져와서 Prometheus server에 저장한다. 이를 Data visualization에 있는 대시보드로 편하게 볼 수 있는 데, 이 역할을 Grafana가 한다.
3. 적용
우선 다음 두 dependencies를 추가한다. 본 멀티 모듈 프로젝트에서는 API 모듈에 추가해주었다.
// Spring Actuator
implementation 'org.springframework.boot:spring-boot-starter-actuator'
//prometheus
implementation 'io.micrometer:micrometer-registry-prometheus'
application.yml에 다음을 추가한다. actuator에서 정보를 공개할 엔드포인트를 명시한다. 와일드 카드도 사용가능하나, 민감한 정보를 포함하기 때문에 가급적이면 필요한 엔드포인트만 여는 것이 좋다.
management:
endpoints:
web:
exposure:
include: health, prometheus

/actuator/prometheus로 요청을 보내면 다음과 같이 메트릭을 응답하는 것을 볼 수 있다. 이제 이를 Prometheus에 저장해야 한다.
우선 모니터링 용 ec2 인스턴스를 하나 파고, 보안 그룹을 다음과 같이 설정한다.

9090은 Prometheus, 3000은 Grafana 포트 번호이다. 각각 열어준다.
3-1. 프로메테우스

wget https://github.com/prometheus/prometheus/releases/download/v2.53.3/prometheus-2.53.3.linux-amd64.tar.gz
tar xzvf prometheus-2.53.3.linux-amd64.tar.gz
모니터링 서버에 접속해서 위와 같이 파일을 다운하고, 압축을 푼다.

폴더 내 prometheus.yml에 job을 다음과 같이 추가해준다. metrics_path로 엔드포인트를 설정하고, scrape_interval로 수집 기간을 설정한다(1m: 1분). static_configs에 서버 주소를 입력한다.
sudo mkdir -p /etc/prometheus
sudo mv prometheus console_libraries prometheus.yml consoles /etc/prometheus
sudo groupadd --system prometheus
sudo useradd --system -s /usr/sbin/nologin -g prometheus prometheus
sudo chown prometheus:prometheus /etc/prometheus -R
이제 이를 서비스로 등록한다. 우선 프로메테우스 계정을 만들고, /etc/prometheus에 파일을 옮긴 후 권한을 준다.
cd /etc/systemd/system
sudo vim prometheus.service

해당 경로에 다음과 같이 저장한다.
sudo mkdir -p /var/lib/prometheus
sudo chown prometheus:prometheus /var/lib/prometheus
sudo systemctl daemon-reload
sudo systemctl enable prometheus
sudo systemctl start prometheus
sudo systemctl status prometheus.service
storage.tsdb.path로 설정된 경로에 폴더를 하나 만들고, 권한을 프로메테우스 계정에 넘겨준 후 시스템을 재시작한다.

정상적으로 실행되는 것을 확인할 수 있었다.
3-2. 그라파나
https://grafana.com/grafana/download?platform=linux
Download Grafana | Grafana Labs
Overview of how to download and install different versions of Grafana on different operating systems.
grafana.com

sudo systemctl enable grafana-server.service
sudo systemctl start grafana-server.service
sudo systemctl status grafana-server.service
명령어를 사용하여 그라파나를 설치하고, 서비스로 등록한다.

3000번 포트로 접속하면 다음과 같은 화면이 뜬다. default 계정은 admin/admin이므로 로그인해준다.

Connections/Data sources에서 add datasource를 눌러 프로메테우스를 추가한다. 프로메테우스와 그라파나는 같은 서버에 존재하므로 로컬호스트로 접속할 수 있다.
https://grafana.com/grafana/dashboards/
Grafana dashboards | Grafana Labs
No results found. Please clear one or more filters.
grafana.com
Dashboards로 가서 new dashboard->import를 누르면 이미 만들어져있는 대시보드를 가져올 수 있다.

우측에 Copy ID to clipboard를 눌러 대시보드의 아이디를 복사할 수 있다.

이를 붙여넣기하고 Load, save를 누르면 추가할 수 있다.

정상적으로 메트릭을 가져와 보여주는 것을 확인할 수 있다.
'Proj > Tripot' 카테고리의 다른 글
Trouble Shooting - @Mock vs @Spy (1) | 2025.03.27 |
---|---|
k6을 이용한 성능 테스트 (1) | 2025.03.16 |
MySQL 백업하기 (0) | 2025.03.05 |
Trouble Shooting - nginx 용량 늘리기(feat. 스토리 등록 오류) (0) | 2025.03.04 |
앱 버전 저장 및 관리하기 (0) | 2025.03.02 |