Amazon Linux 2023을 사용하고 있는 EC2 서버에서 Prometheus와 Grafana를 사용해 모니터링 환경을 구축하는 방법을 다시 설명하겠습니다. Amazon Linux 2023은 최신 패키지와 보안 기능을 제공하므로, 이를 고려하여 필요한 패키지 설치와 설정을 단계별로 안내하겠습니다.
Prometheus 설치:
wget과 tar를 지원하므로 이를 활용합니다.# 최신 버전 다운로드 (2024년 기준 최신 버전은 2.41.0일 수 있습니다)
wget <https://github.com/prometheus/prometheus/releases/download/v2.41.0/prometheus-2.41.0.linux-amd64.tar.gz>
tar xvfz prometheus-2.41.0.linux-amd64.tar.gz
cd prometheus-2.41.0.linux-amd64
Prometheus 설정 파일 수정:
prometheus.yml 파일을 수정하여 EC2 서버와 애플리케이션 메트릭을 수집할 수 있도록 설정합니다.global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090'] # Prometheus 자체 모니터링을 위한 기본 설정
- job_name: 'my_application'
static_configs:
- targets: ['localhost:8080'] # 모니터링할 애플리케이션의 엔드포인트
Prometheus 실행:
nohup 또는 & 기호를 사용해 백그라운드에서 실행할 수 있습니다../prometheus --config.file=prometheus.yml &
Prometheus 웹 UI 확인:
http://<EC2_INSTANCE_IP>:9090로 접속하여 Prometheus 웹 UI가 정상적으로 동작하는지 확인합니다.Grafana 설치:
# Grafana 설치를 위한 리포지토리 추가 및 설치
sudo tee /etc/yum.repos.d/grafana.repo <<EOF
[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
EOF
sudo yum install grafana -y
Grafana 서비스 시작 및 활성화:
sudo systemctl start grafana-server
sudo systemctl enable grafana-server
Grafana에 Prometheus 데이터 소스 추가:
http://<EC2_INSTANCE_IP>:3000로 접속하여 Grafana 웹 UI에 로그인합니다. 기본 로그인 정보는 admin/admin입니다.http://localhost:9090로 입력하고 "Save & Test" 버튼을 눌러 연결을 확인합니다.대시보드 생성 및 설정:
Spring Boot 애플리케이션에 Micrometer 및 Actuator 추가:
pom.xml 또는 build.gradle에 Micrometer와 Spring Boot Actuator를 추가하여 Prometheus와의 통합을 설정합니다.Gradle 예제:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'io.micrometer:micrometer-registry-prometheus'
}
Maven 예제:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
application.properties에 설정 추가:
management.endpoints.web.exposure.include=health,info,prometheus
management.endpoint.prometheus.enabled=true
/actuator/prometheus 엔드포인트에서 애플리케이션의 메트릭을 Prometheus 형식으로 노출할 수 있습니다.Prometheus에 애플리케이션 엔드포인트 추가:
prometheus.yml 파일의 scrape_configs 섹션에 Spring Boot 애플리케이션 엔드포인트를 추가합니다.- job_name: 'my_application'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:8080']
애플리케이션 메트릭 모니터링 확인:
서버 리소스 모니터링:
wget <https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz>
tar xvfz node_exporter-1.3.1.linux-amd64.tar.gz
cd node_exporter-1.3.1.linux-amd64
./node_exporter &
prometheus.yml에 Node Exporter 엔드포인트를 추가합니다.- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']
Grafana 대시보드에서 서버 및 애플리케이션 상태 확인: