본문 바로가기
카테고리 없음

Prometheus AWS EC2 Service Discovery

by 메이플라워 2021. 9. 17.

 

Prometheus 설정에서 ec2_sd_configs 태그를 사용하여 AWS 환경에서 운영중인 모든 EC2 인스턴스를 자동으로 찾을 수 있다.

 

[참고]

https://prometheus.io/docs/prometheus/latest/configuration/configuration/#ec2_sd_config

 

EC2 인스턴스에 Prometheus + Grafana 를 설치하였다.

 

Prometheus 인스턴스에 IAM 역할을 사용하여 모든 인스턴스를 찾을 수 있도록 적용해준다.

 

역할은 ec2-readonlyaccess로 만들고 AmazonEc2ReadOnlyAccess 정책을 연결한다. EC2의 DescribeInstance를 활용하는 것이다.

 

 

역할을 만들고 해당 역할을 Prometheus 인스턴스 설정에서 IAM 역할 연결/바꾸기 작업으로 앞에서 만든 역할을 연결해준다.

 

 

Prometheus의 설정파일 prometheus.yml에 다음과 같이 적용한다.

# AWS EC2 Service Discovery
  - job_name: 'node'
    ec2_sd_configs:
      - region: ap-northeast-2
        port: 9100
    relabel_configs:
      - source_labels: [__meta_ec2_tag_Name]
        target_label: instance
      - source_labels: [__meta_ec2_private_ip]
        regex: '(.*)'
        replacement: '${1}:9100'
        action: replace
        target_label: __address__
      - source_labels: [__meta_ec2_tag_group]
        target_label: group
      - source_labels: [__meta_ec2_instance_type]
        target_label: instance_type

 

Prometheus 서비스 재시작 후 Service Discovery 메뉴를 확인한다.

 

메트릭 수집을 위해서는 수집하고자 하는 인스턴스에 node_exporter 를 설치해야 한다.