반응형

설명

CentOS8에서 Grafana와 Prometheus, Node exporter 등을 설치하여 모니터링을 구성해 보겠습니다. 설치로 들어가기 전에 잠깐 아래 Grafana에서 제공된 이미지를 보면, Grafana와 Prometheus, Node exporter의 관계를 이해할수 있습니다. 조금 단순화해보면, Node에서 수집한 각종 자료를 Prometheus가 저장하고, 이렇게 저장된 자료들을 Web UI인 Grafana에서 보고 모니터링을 할수 있습니다.

 

설치 환경

  • OS: VirtualBox VM (4 cores)
  • RAM: 8GB
  • HDD: 50GB
  • IP Address: 192.168.56.153

 

SELinux 비활성화

이 페이지에서 SELinux는 다루지 않으므로 비활성화 하겠습니다.

[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

 

 

Firewall

Firewall에 Grafana와 Prometheus는 서비스 항목이 있어서 바로 등록이 가능하지만, Node exporter는 별도 서비스를 생성해서 등록해야만 합니다. Grafana의 서비스 포트는 3000번이며 Prometheus는 9090번, Node exporter는 9100번을 사용합니다.

### Firewall에 Grafana 서비스 추가 ###
[root@localhost ~]# firewall-cmd --permanent --add-service=grafana
success

### Firewall에 Prometheus 서비스 추가 ###
[root@localhost ~]# firewall-cmd --permanent --add-service=prometheus
success


### Firewall에 Node exporter를 신규 서비스로 등록  ###
[root@localhost ~]# firewall-cmd --permanent --new-service=node_exporter
success

### Firewall에 Node exporter의 포트정보 등록  ###
[root@localhost ~]# firewall-cmd --permanent --service=node_exporter --add-port=9100/tcp
success


### Firewall에 Node exporter 서비스 추가 ###
[root@localhost ~]# firewall-cmd --permanent --add-service=node_exporter
success

### Firewall에 적용 ###
[root@localhost ~]# firewall-cmd --reload
success

### Firewall에서 적용상태 확인 ###
[root@localhost ~]# firewall-cmd --list-services
cockpit dhcpv6-client grafana node_exporter prometheus ssh

 

 

Repository

Grafana와 Prometheus/Node exporter의 Repository를 별도 구성합니다. Grafana의 경우 CentOS8에서도 패지키를 제공하지만, Grafana에서 제공하는 패키지 업데이트가 빠르기 때문에 별도로 구성하는 것이 좋습니다.

### Grafana Repository 추가 ###
[root@localhost ~]# echo -e "\
[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
" >> /etc/yum.repos.d/grafana.repo

 

### Prometheus/Node exporter Repository 추가 ###
[root@localhost ~]# echo -e "\
[prometheus]
name=prometheus
baseurl=https://packagecloud.io/prometheus-rpm/release/el/\$releasever/\$basearch
repo_gpgcheck=1
enabled=1
gpgkey=https://packagecloud.io/prometheus-rpm/release/gpgkey
       https://raw.githubusercontent.com/lest/prometheus-rpm/master/RPM-GPG-KEY-prometheus-rpm
gpgcheck=1
metadata_expire=300
" >> /etc/yum.repos.d/prometheus.repo

 

 

패키지 설치

Grafana Repository 추가가 완료되었다면, Grafana 패키지를 설치합니다.

### Grafana 패키지 설치 ###
[root@localhost ~]# dnf -y install grafana node_exporter prometheus2

 

 

서비스 활성화 및 시작

Grafana와 Prometheus, Node exporter 등의 패키지 설치가 완료되었다면, 서비스를 활성화 시킨후 시작합니다. 서비스 시작후에 상태를 확인하여 서비스에 문제가 없는지까지 확인합니다.

### 자동시작을 위해 서비스 등록 ###
[root@localhost ~]# systemctl enable grafana-server node_exporter prometheus
Created symlink /etc/systemd/system/multi-user.target.wants/grafana-server.service...
Created symlink /etc/systemd/system/multi-user.target.wants/node_exporter.service...
Created symlink /etc/systemd/system/multi-user.target.wants/prometheus.service...

### 서비스 활성화 ###
[root@localhost ~]# systemctl start grafana-server node_exporter prometheus

### 활성화된 서비스 확인 ###
[root@localhost ~]# systemctl systemctl status grafana-server node_exporter prometheus --no-pager
● grafana-server.service - Grafana instance
   Loaded: loaded (/usr/lib/systemd/system/grafana-server.service; enabled; vendor preset: disabled)
   Active: active (running) since ...
...
● node_exporter.service - Prometheus exporter for machine metrics, written in Go with pluggable metric collectors.
   Loaded: loaded (/usr/lib/systemd/system/node_exporter.service; enabled; vendor preset: disabled)
   Active: active (running) since ...
...
● prometheus.service - The Prometheus monitoring system and time series database.
   Loaded: loaded (/usr/lib/systemd/system/prometheus.service; enabled; vendor preset: disabled)
   Active: active (running) since ...

서비스가 정상적으로 시작되었다면, 아래와 같이 서비스 포트가 정상적으로 열려 있는지 확인합니다.

### Grafana/Prometheus/Node exporter 등의 서비스 포트 확인 ###
[root@localhost ~]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      955/sshd
tcp6       0      0 :::22                   :::*                    LISTEN      955/sshd
tcp6       0      0 :::3000                 :::*                    LISTEN      2302/grafana-server
tcp6       0      0 :::9090                 :::*                    LISTEN      2304/prometheus
tcp6       0      0 :::9100                 :::*                    LISTEN      2303/node_exporter

 

 

Grafana와 Prometheus 연결

「http://IP:3000」 로 접속합니다. 초기 로그인 정보는 admin/admin입니다. 로그인이 정상적으로 이루어지면 바로 패스워드를 변경할수 화면으로 이동되므로, 새로운 패스워드로 변경합니다.

로그인후, 좌측 메뉴에서 Configuration > Data sources > Add data source > Prometheus 순으로 이동합니다.

HTTP의 URL 항목에서 Prometheus에 접속할수 있는 「http://IP:9090」 형식의 URL을 입력합니다.

페이지 하단에 있는 「Save & test」 버튼을 클릭하여 「Data source is working」 메세지가 출력되는지 확인합니다. 이 메세지가 출력되었다면, Grafana와 Prometheus가 정상적으로 연결되었음을 의미합니다.

브라우저에서 「http://IP:9090」과 같은 URL을 입력하면 별도로 Prometheus에 접속할수도 있습니다.

 

 

Prometheus와 Node exporter 연결

Prometheus와 Node exporter의 연결은 Prometheus의 환경 파일에 Node exporter의 정보를 기입하는 것으로 가능합니다. 「job_name: "node_exporter"」와 같이 별도의 「job」을 생성하고 Prometheus 서비스를 재시작합니다.

### Prometheus 환경파일 편집 ###
vi /etc/prometheus/prometheus.yml

------------------------------------------------------------------------------------------------

# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]

  - job_name: "node_exporter"
    static_configs:
      - targets: ["192.168.56.153:9100"]

 

### Prometheus 서비스 재시작 ###
[root@localhost ~]# systemctl restart prometherus

### Prometheus 서비스 상태확인 ###
[root@localhost ~]# systemctl status prometheus
● prometheus.service - The Prometheus monitoring system and time series database.
   Loaded: loaded (/usr/lib/systemd/system/prometheus.service; enabled; vendor preset: disabled)
   Active: active (running) since ...
...

 

 

Grafana에서 Node exporter로 서버 정보 보기

Grafana 사이트에서 Node exporter와 관련된 Dashboard를 검색하여 마음에 드는 것을 선택한후 JSON 파일을 다운로드합니다. 이 페이지에서는 최근에 Grafana에 등록된 JSON 파일을 다운로드하여 사용합니다.

node-exporter-for-prometheus-dashboard-en-v20201010_rev9.json
0.10MB

Node exporter용 Dashboard JSON 파일을 다운로드 받았다면, Grafana의 좌측 메뉴에서 Dashboard > Manage > Import > Upload JSON file 순으로 클릭하여 다운로드 받은 파일을 업로드 합니다. 업로드가 완료되었다면, 「VictoriaMetrics」 항목에서 「Prometheus」를 선택한후 「Import」 버튼을 클릭합니다.

설정상에 문제가 없다면 아래와 같이 Node exporter를 통해 수집되는 서버 정보를 볼수 있습니다.

반응형

+ Recent posts