DevOps/Prometheus & Grapana

Prometheus 설치하기

치킨맛코드 2022. 9. 28. 20:29

프로메테우스를 설치하고 활용해 보도록 하자. 아래의 링크는 프로메테우스의 github이다. 이곳에서 원하는 사양의 버전을 다운받도록 하자.

 

환경은 Centos7을 바탕으로 진행하였다.

https://github.com/prometheus/prometheus/releases/

 

Releases · prometheus/prometheus

The Prometheus monitoring system and time series database. - prometheus/prometheus

github.com

어느 도구나 마찬가지이지만, 프로메테우스용 유저를 따로 생성하고 이 계정을 바탕으로 활용해 보도록 하자.

useradd prometheus
passwd prometheus
==> 프로메테우스용 리눅스 유저를 생성. 유저명은 prometheus이다.

먼저 tar파일을 다운받아 오도록 하자. 이 후 다운로드 페키지를 다운받자.

yum install -y wget
wget https://github.com/prometheus/prometheus/releases/download/v2.37.1/prometheus-2.37.1.linux-amd64.tar.gz
==> 프로메테우스를 다운받는다

 

이제 root 계정으로 아래의 명령어를 사용해 디렉토리를 생성하고 권한을 부여한 후 압축파일을 해제해도록 하자.

mkdir /etc/prometheus
==> etc 에 prometheus 라는 폴더를 생성한다.
mkdir /var/lib/prometheus
==> var/lib 에 prometheus 라는 폴더를 생성한다.
chown prometheus:prometheus /etc/prometheus
==> 프로메테우스라는 유저에 권한을 부여
chown prometheus:prometheus /var/lib/prometheus
==> 프로메테우스라는 유저에 권한을 부여
tar -xvzf prometheus-2.37.1.linux-amd64.tar.gz
==> 압축해제
mv prometheus-2.37.1.linux-amd64 prometheuspackage
==> 페키지 파일의 이름 바꾸기
cp prometheuspackage/prometheus /usr/local/bin/
cp prometheuspackage/promtool /usr/local/bin/
chown prometheus:prometheus /usr/local/bin/prometheus
chown prometheus:prometheus /usr/local/bin/promtool
==> usr/local/bin으로 페키지를 옮긴 후 권한부여 및 소유권 변경을 한다.
cp -r prometheuspackage/consoles /etc/prometheus
cp -r prometheuspackage/console_libraries /etc/prometheus
chown -R prometheus:prometheus /etc/prometheus/consoles
chown -R prometheus:prometheus /etc/prometheus/console_libraries
==> 콘솔 및 콘솔 라이브러리를 복사한다.

다음은 prometheus.yml파일을 생성한 후 아래의 내용을 기입하자. 생성된 파일의 소유권을 변경하는것은 잊지말자.

vi /etc/prometheus/prometheus.yml

global:
  scrape_interval: 10s

scrape_configs:
  - job_name: 'prometheus_master'
  scrape_interval: 5s
  static_configs:
    - targets: ['192.168.0.108:9090']
==> targets의 ip를 프로메테우스를 설치하는 서버의 ip : 포트번호 로 사용하도록 하자.
chown prometheus:prometheus /etc/prometheus/prometheus.yml
==> 소유권 변경

이제 설치를 위한 service파일을 생성하도록하자. 이전에 설정했던 설정값(권한부여, 위치 등)을 확인한 후 저장하자.

vi /etc/systemd/system/prometheus.service

[Unit]
Description=Prometheus
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을 설정해 주면 무사히 실행되는것을 확인할 수 있다.

systemctl daemon-reload
systemctl start prometheus
systemctl status prometheus
systemctl restart prometheus