CentOS 8.2에서 Prometheus 및 node_exporter 설치 및 구성 가이드

개요

Prometheus는 오픈 소스 모니터링, 쿼리 및 경고 도구입니다. 이 기능이 풍부한 도구는 2012년 Soundcloud에서 처음 구축되었으며, 많은 기업에서 IT 인프라를 모니터링하고 모든 시스템이 원활하게 작동하도록 보장하는 데 사용됩니다. Prometheus를 사용하면 CPU 및 메모리 사용률과 같은 시계열 지표를 HTTP 프로토콜을 통해 쿼리하고 추출할 수 있으며, 이를 실시간 그래프로 시각화할 수 있습니다. 또한 노드나 서비스가 중단될 때 경고를 보내도록 Prometheus를 구성하고, 데이터 시각화를 강화하기 위해 Grafana와 같은 다른 타사 모니터링 도구와 통합할 수 있습니다. 이 가이드에서는 CentOS 8 / RHEL 8 시스템에 Prometheus를 설치하는 방법을 다룹니다.

테스트 환경

Prometheus 서버 Prometheus 클라이언트
호스트명 prometheusserver prometheusclient
IP 주소 192.168.6.140 192.168.6.160

1단계: Prometheus 사용자 및 그룹 생성

먼저 Prometheus를 위한 시스템 사용자를 생성합니다. 다음 명령을 실행합니다.

[root@prometheusserver ~]# useradd -m -s /bin/false prometheus
[root@prometheusserver ~]# id prometheus
uid=1005(prometheus) gid=1005(prometheus) groups=1005(prometheus)

시스템 사용자에게 /bin/false 옵션으로 로그인 권한이 없음을 확인할 수 있습니다.

2단계: Prometheus 구성 디렉토리 생성

Prometheus 사용자를 생성한 후, /etc/var 디렉토리에 Prometheus 구성 파일과 데이터를 저장할 디렉토리를 만듭니다.

[root@prometheusserver ~]# mkdir /etc/prometheus
[root@prometheusserver ~]# mkdir /var/lib/prometheus

/var/lib/prometheus의 소유권을 설정합니다.

[root@prometheusserver ~]# chown prometheus /var/lib/prometheus/

3단계: Prometheus tar 파일 다운로드

디렉토리를 준비한 후 Prometheus를 다운로드합니다. 최신 버전을 확인하려면 공식 다운로드 페이지를 방문하세요. 작성 시점 기준 최신 버전은 v2.23.0입니다. 또는 다음 명령을 실행합니다.

[root@prometheusserver ~]# dnf install wget -y
[root@prometheusserver ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.23.0/prometheus-2.23.0.linux-amd64.tar.gz

다운로드가 완료되면 tarball 파일을 압축 해제합니다.

[root@prometheusserver tmp]# tar zxvf prometheus-2.23.0.linux-amd64.tar.gz

이렇게 하면 prometheus-2.23.0.linux-amd64 디렉토리가 생성됩니다.

압축 해제된 디렉토리에는 prometheuspromtool이라는 두 개의 바이너리 파일이 있으며, 이를 /usr/local/bin 경로로 복사해야 합니다.

[root@prometheusserver tmp]# cd prometheus-2.23.0.linux-amd64
[root@prometheusserver prometheus-2.23.0.linux-amd64]# cp prometheus /usr/local/bin
[root@prometheusserver prometheus-2.23.0.linux-amd64]# cp promtool /usr/local/bin

4단계: Prometheus 구성 파일 생성

/etc/prometheus/prometheus.yml 파일을 생성하고 다음 구성을 붙여넣습니다.

[root@prometheus ~]# vi /etc/prometheus/prometheus.yml
# Global config
global:
  scrape_interval: 15s        # 스크래핑 간격: 15초 (기본값: 1분)
  evaluation_interval: 15s    # 규칙 평가 간격: 15초 (기본값: 1분)
  scrape_timeout: 15s          # 스크래핑 타임아웃 (기본값: 10초)

# 스크래핑할 엔드포인트를 포함한 스크래핑 구성 (여기서는 Prometheus 자체)
scrape_configs:
  - job_name: 'prometheus'    # 이 작업에서 스크래핑된 시계열에 'job=<job_name>' 레이블을 추가
    static_configs:
    - targets: ['localhost:9090']

이것은 로컬 시스템(Prometheus 서버)만 모니터링합니다.

다음으로, 방화벽에서 포트 9090을 허용합니다.

[root@prometheusserver /]# firewall-cmd --add-port=9090/tcp --permanent
success
[root@prometheusserver /]# firewall-cmd --reload
success

5단계: Prometheus 서버 Systemd 서비스 파일 생성

systemd로 Prometheus를 서비스로 관리하기 위해 시스템 파일을 생성합니다.

[root@prometheusserver /]# vi /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus Time Series Collection and Processing Server
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
    --config.file /etc/prometheus/prometheus.yml \
    --storage.tsdb.path /var/lib/prometheus/ \
    --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries

[Install]
WantedBy=multi-user.target

변경 사항을 적용하기 위해 systemctl을 다시 로드합니다.

[root@prometheusserver /]# systemctl daemon-reload

Prometheus를 시작하고 부팅 시 실행되도록 활성화합니다.

[root@prometheusserver /]# systemctl start prometheus
[root@prometheusserver /]# systemctl enable prometheus

Prometheus가 실행 중인지 확인합니다.

[root@prometheusserver /]# systemctl status prometheus

서비스가 포트 9090에서 수신 대기 중인지 확인할 수도 있습니다.

[root@prometheusserver /]# netstat -tunlp
또는
[root@prometheusserver /]# ss -antpl

이제 브라우저를 열고 서버 IP로 이동합니다.

http://<server-ip>:9090

예: http://192.168.6.140:9090

Status 탭을 클릭한 다음 Targets를 클릭하여 엔드포인트를 확인합니다.

6단계: node_exporter 설치 및 구성

node_exporter는 CPU, 메모리 사용률, 파일 시스템 및 네트워크 통계와 같은 다양한 Linux 시스템 메트릭을 수집하는 유틸리티입니다. 이 섹션에서는 Prometheus 서버와 원격 CentOS 8 Linux 호스트에 node_exporter를 설치하고 두 호스트의 시스템 메트릭을 모니터링합니다.

6.1 Prometheus 노드에 node_exporter용 시스템 사용자를 생성합니다.

[root@prometheusserver tmp]# useradd -m -s /bin/false node_exporter

6.2 Prometheus 다운로드 페이지에서 node_exporter tarball을 다운로드하거나 wget 명령을 사용합니다.

[root@prometheusserver tmp]# wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz

6.3 node_exporter 파일의 압축을 해제합니다.

[root@prometheusserver tdops]# tar zxvf node_exporter-1.0.1.linux-amd64.tar.gz

6.4 node_exporter 바이너리를 /usr/local/bin 경로로 복사합니다.

[root@prometheusserver tdops]# cp node_exporter-1.0.1.linux-amd64/node_exporter /usr/local/bin

6.5 복사된 node_exporter 파일의 소유권을 설정합니다.

[root@prometheusserver tmp]# chown node_exporter:node_exporter /usr/local/bin/node_exporter

6.6 node_exporter를 서비스로 실행하기 위해 systemd 서비스 파일을 생성합니다.

[root@prometheusserver tmp]# vi /etc/systemd/system/node_exporter.service
[Unit]
Description=Prometheus Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target

6.7 systemd 매니저를 다시 로드합니다.

[root@prometheusserver tmp]# systemctl daemon-reload

6.8 node_exporter 서비스를 시작하고 활성화합니다.

[root@prometheusserver tdops]# systemctl start node_exporter
[root@prometheusserver tdops]# systemctl enable node_exporter

6.9 서비스 상태를 확인합니다.

[root@prometheusserver tdops]# systemctl status node_exporter

6.10 서비스가 포트 9100에서 수신 대기 중인지 확인합니다.

[root@prometheusserver tdops]# netstat -pnltu | grep 9100
tcp6       0      0 :::9100                 :::*                    LISTEN      6419/node_exporter

6.11 방화벽에서 포트 9100을 엽니다.

[root@prometheusserver tdops]# firewall-cmd --add-port=9100/tcp --permanent
success
[root@prometheusserver tdops]# firewall-cmd --reload
success

6.12 Prometheus 구성 파일(prometheus.yml)에 node_exporter 대상을 추가합니다.

[root@prometheusserver tdops]# vi /etc/prometheus/prometheus.yml
scrape_configs:
  - job_name: 'node_exporter'
    static_configs:
    - targets: ['localhost:9100']

6.13 Prometheus 서비스를 다시 시작합니다.

[root@prometheusserver tdops]# systemctl restart prometheus

브라우저에서 Status > Targets를 확인하여 node_exporter 엔드포인트가 추가되었는지 확인합니다.

7단계: 원격 호스트에 node_exporter 설치

7.1 원격 호스트에서 node_exporter 사용자를 생성합니다.

[root@prometheusclient tmp]# useradd -m -s /bin/false node_exporter

7.2 node_exporter를 다운로드하고 압축을 해제합니다.

[root@prometheusclient tmp]# wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
[root@prometheusclient tmp]# tar zxvf node_exporter-1.0.1.linux-amd64.tar.gz

7.3 바이너리를 복사하고 소유권을 설정합니다.

[root@prometheusclient tmp]# cp node_exporter-1.0.1.linux-amd64/node_exporter /usr/local/bin
[root@prometheusclient tmp]# chown node_exporter:node_exporter /usr/local/bin/node_exporter

7.4 systemd 서비스 파일을 생성하고 서비스를 시작합니다.

[root@prometheusclient tmp]# vi /etc/systemd/system/node_exporter.service
[Unit]
Description=Prometheus Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target

[root@prometheusclient tmp]# systemctl daemon-reload
[root@prometheusclient tmp]# systemctl start node_exporter
[root@prometheusclient tmp]# systemctl enable node_exporter

7.5 서비스 상태를 확인합니다.

[root@prometheusclient tmp]# systemctl status node_exporter

8단계: Prometheus 서버에 원격 호스트 추가

8.1 Prometheus 서버에서 prometheus.yml 파일을 편집하여 원격 호스트의 node_exporter 대상을 추가합니다.

[root@prometheusserver tdops]# vi /etc/prometheus/prometheus.yml
scrape_configs:
  - job_name: 'node_exporter'
    static_configs:
    - targets: ['localhost:9100', '192.168.6.160:9100']

8.2 Prometheus 서비스를 다시 시작합니다.

[root@prometheusserver tdops]# systemctl restart prometheus

8.3 브라우저에서 Status > Targets를 확인하여 원격 호스트 엔드포인트가 추가되었는지 확인합니다.

8.4 메트릭을 확인하려면 curl을 사용합니다.

[root@prometheusclient tmp]# curl http://localhost:9100/metrics

또는 브라우저에서 http://192.168.6.160:9100/metrics에 접속합니다.

8.5 Prometheus UI에서 그래프를 생성하려면 원하는 메트릭을 선택하고 Execute를 클릭한 후 Graph 탭을 선택합니다.

태그: prometheus Node_Exporter CentOS linux 서버 모니터링

7월 3일 21:34에 게시됨