Amazon Linux 2023을 사용하고 있는 EC2 서버에서 Prometheus와 Grafana를 사용해 모니터링 환경을 구축하는 방법을 다시 설명하겠습니다. Amazon Linux 2023은 최신 패키지와 보안 기능을 제공하므로, 이를 고려하여 필요한 패키지 설치와 설정을 단계별로 안내하겠습니다.

1단계: EC2 서버에서 Prometheus 설치 및 설정

  1. Prometheus 설치:

    # 최신 버전 다운로드 (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
    
    
  2. Prometheus 설정 파일 수정:

    global:
      scrape_interval: 15s
    
    scrape_configs:
      - job_name: 'prometheus'
        static_configs:
          - targets: ['localhost:9090']  # Prometheus 자체 모니터링을 위한 기본 설정
    
      - job_name: 'my_application'
        static_configs:
          - targets: ['localhost:8080']  # 모니터링할 애플리케이션의 엔드포인트
    
    
  3. Prometheus 실행:

    ./prometheus --config.file=prometheus.yml &
    
    
  4. Prometheus 웹 UI 확인:

2단계: Grafana 설치 및 설정

  1. 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
    
    
  2. Grafana 서비스 시작 및 활성화:

    sudo systemctl start grafana-server
    sudo systemctl enable grafana-server
    
    
  3. Grafana에 Prometheus 데이터 소스 추가:

  4. 대시보드 생성 및 설정:

3단계: 애플리케이션 메트릭 수집을 위한 설정

  1. Spring Boot 애플리케이션에 Micrometer 및 Actuator 추가:

    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>
    
    
  2. application.properties에 설정 추가:

    management.endpoints.web.exposure.include=health,info,prometheus
    management.endpoint.prometheus.enabled=true
    
    
  3. Prometheus에 애플리케이션 엔드포인트 추가:

    - job_name: 'my_application'
      metrics_path: '/actuator/prometheus'
      static_configs:
        - targets: ['localhost:8080']
    
    
  4. 애플리케이션 메트릭 모니터링 확인:

4단계: 서버 및 애플리케이션 상태 모니터링

  1. 서버 리소스 모니터링:

    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 &
    
    
    - job_name: 'node_exporter'
      static_configs:
        - targets: ['localhost:9100']
    
    
  2. Grafana 대시보드에서 서버 및 애플리케이션 상태 확인:

5단계: 모니터링 결과 분석 및 최적화