Engineering/etc.

grafana와 prometheus를 이용한 모니터링 시스템 구축해보기

mutjang2 2025. 4. 8. 18:21

구성요소

 

Prometheus: 시스템 혹은 어플레케이션의 메트릭 정보를 수집하고 저장하는 모니터링 수집도구

Grafana: 프론트엔드 데이터 대시보드 툴. Prometheus가 수집한 메트릭 정보를 grafana로 시각화 표현 예정

Node exporter: 노드에 위치하여 prometheus에게 메트릭 정보를 보고하는 에이전트.

 

시퀀스는 

1) node exporter가 해당 노드의 메트릭 정보 수집

2) prometheus는  설정한 규칙에 알맞게 data를 pull

3) grafana는 prometheus에 쿼리 후, 결과를 대시보드에 시각화하여 표출

 


Installation enviroment

  • VMs
    node1, node2
  • node_exporter-installed
    node1, node2
  •  prometheus-installed
    node1
  • grafana-installed
    node1

 

 


 

 

node_exporter 설치

 

  • 아래 url로 이동

 

https://prometheus.io/download/#node_exporter

 

Download | Prometheus

An open-source monitoring system with a dimensional data model, flexible query language, efficient time series database and modern alerting approach.

prometheus.io

 

  • 최신 릴리즈 버전의 링크 복사

 

 

  • wget 명령어로 해당 파일 다운로드 & 압축 풀기
$ wget https://github.com/prometheus/node_exporter/releases/download/v1.9.1/node_exporter-1.9.1.linux-amd64.tar.gz
$ tar xvzf node_exporter-1.9.1.linux-amd64.tar.gz

$ ls -al
total 21704
drwxr-xr-x   2 nts  si         56 Apr  2 00:23 .
drwxr-xr-x. 12 root root     4096 Apr  8 13:56 ..
-rw-r--r--   1 nts  si      11357 Apr  2 00:23 LICENSE
-rwxr-xr-x   1 nts  si   22204245 Apr  2 00:19 node_exporter
-rw-r--r--   1 nts  si        463 Apr  2 00:23 NOTICE

 

  • 실행 파일을 실행해보고, 프로세스가 잘 뜨는지 확인
$ ./node_exporter &
[1] 1760505

$ ps -ef | grep -i node
root         801       1  0  2024 ?        00:00:00 /usr/sbin/mcelog --ignorenodev --daemon --foreground
root     1760505 1759770  0 13:58 pts/0    00:00:00 ./node_exporter
root     1760515 1759770  0 13:59 pts/0    00:00:00 grep --color=auto node

$ netstat -anp | grep 1760505
tcp6       0      0 :::9100                 :::*                    LISTEN      1760505/./node_expo

 

  • 백그라운드에서 자동으로 프로세스가 뜰 수 있도록 데몬 등록을 해주기
$ kill -9 1760505

$ pwd
/home/node_exporter-1.9.1.linux-amd64

# 편집기를 열어 아래 내용 입력
$vi /etc/systemd/system/node_exporter.service

[Unit]
Description=Node Exporter

[Service]
ExecStart=/home/node_exporter-1.9.1.linux-amd64/node_exporter                                                                                                                                   

[Install]
WantedBy=default.target

$ systemctl start node_exporter.service
$ systemctl enable node_exporter.service

 


 

prometheus 설치

 

  • 아래 url 이동

https://prometheus.io/download/#prometheus

 

Download | Prometheus

An open-source monitoring system with a dimensional data model, flexible query language, efficient time series database and modern alerting approach.

prometheus.io

 

  • LTS 버전 다운로드 & 압축 해제

$ wget https://github.com/prometheus/prometheus/releases/download/v2.53.4/prometheus-2.53.4.linux-amd64.tar.gz
$ tar xvzf prometheus-2.53.4.linux-amd64.tar.gz

$ cd prometheus-2.53.4.linux-amd64/
$ ls -al
total 261308
drwxr-xr-x   4 ansible  118       132 Mar 19 00:08 .
drwxr-xr-x. 11 root    root       267 Apr  8 15:37 ..
drwxr-xr-x   2 ansible  118        38 Mar 19 00:05 console_libraries
drwxr-xr-x   2 ansible  118       173 Mar 19 00:05 consoles
-rw-r--r--   1 ansible  118     11357 Mar 19 00:05 LICENSE
-rw-r--r--   1 ansible  118      3773 Mar 19 00:05 NOTICE
-rwxr-xr-x   1 ansible  118 137836884 Mar 18 23:52 prometheus
-rw-r--r--   1 ansible  118       934 Mar 19 00:05 prometheus.yml
-rwxr-xr-x   1 ansible  118 129719117 Mar 18 23:52 promtool

 

 

  • prometheus 관련 디렉토리 생성 및 파일 이동
#바이너리 이동
$ mv prometheus /usr/bin/
$ mv promtool /usr//bin/

#디렉토리 생성
$ mkdir /etc/prometheus
$ mkdir -p /var/lib/prometheus/data
$ mkdir /usr/share/prometheus

$ mv prometheus.yml /etc/prometheus/
$ mv consol* /usr/share/prometheus/

 

  • prometheus 데몬 등록
$ vi /etc/systemd/system/prometheus.service
[Unit]
Description=The Prometheus monitoring system and time series database.
Documentation=https://prometheus.io
After=network.target

[Service]
EnvironmentFile=-/etc/default/prometheus
User=prometheus
ExecStart=/usr/bin/prometheus $PROMETHEUS_OPTS
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
RestartSec=5s
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

 

  • prometheus 환경 파일 작성
$ cat << EOF >/etc/default/prometheus 
PROMETHEUS_OPTS='--config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus/data --web.console.libraries=/usr/share/prometheus/console_libraries --web.console.templat
es=/usr/share/prometheus/consoles'
EOF

 

  • prometheus 계정 생성
sudo useradd --no-create-home --shell /bin/false prometheus

 

  • prometheus.yml 파일 수정
$ vi /etc/prometheus/prometheus.yml

## job_name 부분 수정
  - job_name: "node1"
    static_configs: 
    - targets: ["172.16.22.133:9100"]
  - job_name: "node2"
    static_configs:
    - targets: ["172.16.22.131:9100"]
  - job_name: "local"
  	static_configs:
    - targets: ["localhost:9090"]

 

 

 

설정한 포트에 접속해보니 웹 화면이 잘 표출된다.

 


 

grafana 설치

 

  • grafana 패키지 설치
$ yum install -y grafana

 

  • 프로세스 구동
$ systemctl start grafana-server.service

$ ps -ef | grep grafana
grafana  3054374       1 11 17:45 ?        00:00:01 /usr/sbin/grafana-server --config=/etc/grafana/grafana.ini --pidfile=/var/run/grafana/grafana-server.pid --packaging=rpm cfg:default.paths.logs=/var/log/grafana cfg:default.paths.data=/var/lib/grafana cfg:default.paths.plugins=/var/lib/grafana/plugins cfg:default.paths.provisioning=/etc/grafana/provisioning

$ netstat -anp | grep 3054374
tcp6       0      0 :::3000                 :::*                    LISTEN      3054374/grafana-ser

 

기본 설정이 3000번 포트에 all binding 하도록 되어있는 듯. 해당 포트로 웹 접속 해보니, 로그인 화면이 나온다.

 

초기 계정 정보인 admin/admin 으로 로그인하고, 비밀번호 변경 후 다시 로그인해준다.

 

  • grafana & prometheus 연동

설정 아이콘 > Data sources
프로메테우스 선택

 

설정 정보 입력 후, 연동 마무리.

 

 

  • grafana 대시보드 가져오기

 

Dashboards > Import

 

 

Node Exporter Full 대시보드의 ID인 1860을 가져와준다.

 

완성. 아주 간편하게 모니터링 시스템을 구축해보았다. 디테일한 설정은 조금 만져보면서 익혀봐야겠다.