• RedHat 계열의 CentOS 서버에서 Prometheus를 설치하고 systemd로 관리
  • Security Group, Firewall 등으로 9090번 포트에 대한 방화벽 해제가 필요

1. Prometheus 설치

  • 설치하고 싶은 Prometheus 버전의 경로를 잘 확인하여 다운받으면 됨

  • systemd를 사용하지 않고 /root/apps/prometheus-2.22.0.linux-amd64/pometheus를 실행해도 Prometheus 사용 가능

    $ pwd
    /root
    
    # 설치하는 컴포넌트들의 관리를 더 쉽게 하기 위해서 디렉토리 생성
    $ mkdir apps
    
    # 디렉토리 이동
    $ cd /root/apps
    
    # Prometheus 바이너리 파일이 들어 있는 압축 파일 설치
    $ wget https://github.com/prometheus/prometheus/releases/download/v2.22.0/prometheus-2.22.0.linux-amd64.tar.gz
    
    # 설치한 Prometheus 바이너리 파일 확인
    $ ls
    prometheus-2.22.0.linux-amd64.tar.gz
    
    # 압축 파일 해제
    $ tar zxvf prometheus-2.22.0.linux-amd64.tar.gz
    prometheus-2.22.0.linux-amd64/
    prometheus-2.22.0.linux-amd64/NOTICE
    prometheus-2.22.0.linux-amd64/prometheus
    prometheus-2.22.0.linux-amd64/consoles/
    prometheus-2.22.0.linux-amd64/consoles/node-cpu.html
    prometheus-2.22.0.linux-amd64/consoles/prometheus-overview.html
    prometheus-2.22.0.linux-amd64/consoles/node.html
    prometheus-2.22.0.linux-amd64/consoles/node-overview.html
    prometheus-2.22.0.linux-amd64/consoles/index.html.example
    prometheus-2.22.0.linux-amd64/consoles/prometheus.html
    prometheus-2.22.0.linux-amd64/consoles/node-disk.html
    prometheus-2.22.0.linux-amd64/console_libraries/
    prometheus-2.22.0.linux-amd64/console_libraries/prom.lib
    prometheus-2.22.0.linux-amd64/console_libraries/menu.lib
    prometheus-2.22.0.linux-amd64/promtool
    prometheus-2.22.0.linux-amd64/LICENSE
    prometheus-2.22.0.linux-amd64/prometheus.yml



2. Prometheus를 서비스로 생성하여 관리 → prometheus 실행을 systemd로 관리

  • Prometheus를 리눅스 systemd를 통해 서비스로 등록

    $ pwd
    /root/apps/prometheus-2.22.0.linux-amd64
    
    # 디렉토리 프로비저닝
    $ useradd --no-create-home --shell /bin/false prometheus
    
    # 생성한 prometheus 계쩡 확인
    $ cat /etc/passwd | grep prometheus
    prometheus:x:1001:1001::/home/prometheus:/bin/false
    
    # prometheus를 관리할 디렉토리 생성 및 명령어 복사
    $ mkdir /etc/prometheus
    $ mkdir /var/lib/prometheus
    $ cp ./prometheus /usr/local/bin/
    $ cp ./promtool /usr/local/bin/
    $ cp ./prometheus.yml /etc/prometheus/
    $ cp -r ./consoles /etc/prometheus
    $ cp -r ./console_libraries /etc/prometheus
    
    # 명령어 복사 확인
    $ ls /usr/local/bin/
    prometheus  promtool
    
    # prometheus 관련 내용 복사
    $ ls /etc/prometheus
    console_libraries  consoles
    
    # 유저:그룹 설정 -> prometheus 사용자 계정이 관리할 수 있도록 권한 변경
    $ chown prometheus:prometheus /etc/prometheus
    $ chown prometheus:prometheus /var/lib/prometheus
    $ chown prometheus:prometheus /usr/local/bin/prometheus
    $ chown prometheus:prometheus /usr/local/bin/promtool
    $ chown -R prometheus:prometheus /etc/prometheus/consoles
    $ chown -R prometheus:prometheus /etc/prometheus/console_libraries
    
    # 서비스 파일 등록
    $ cat << EOF | tee /etc/systemd/system/prometheus.service
    [Unit]
    Description=Prometheus 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
    EOF
  • 서비스 데몬 리로딩
    $ systemctl daemon-reload
  • prometheus 서비스 시작
    $ systemctl start prometheus
  • prometheus 서비스 상태 확인

    $ systemctl status prometheus
    ● prometheus.service - Prometheus Server
     Loaded: loaded (/etc/systemd/system/prometheus.service; disabled; vendor preset: disabled)
     Active: active (running) since Sat 2021-11-20 02:55:12 KST; 11h ago
    Main PID: 11780 (prometheus)
      Tasks: 14
     Memory: 48.4M
     CGroup: /system.slice/prometheus.service
             └─11780 /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_...
    
    Nov 20 12:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T03:00:03.377Z caller=head.go:889 component=tsdb msg="WAL checkpoint complete" first=2 last=3 duration=29.052021ms
    Nov 20 12:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T03:00:03.425Z caller=compact.go:440 component=tsdb msg="compact blocks" count=3 mint=1637344800000 maxt=1637366400000 ulid=01FMXMJQ5RJ914...ration=41.7039ms
    Nov 20 12:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T03:00:03.431Z caller=db.go:1152 component=tsdb msg="Deleting obsolete block" block=01FMX6V8HX8WMN15F7H841AKH0
    Nov 20 12:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T03:00:03.432Z caller=db.go:1152 component=tsdb msg="Deleting obsolete block" block=01FMXDPZSXA1G1VCYYE6JTVAQN
    Nov 20 12:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T03:00:03.433Z caller=db.go:1152 component=tsdb msg="Deleting obsolete block" block=01FMWZZH9W1MH7PN1NZBT06CG9
    Nov 20 14:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T05:00:03.321Z caller=compact.go:494 component=tsdb msg="write block" mint=1637373600000 maxt=1637380800000 ulid=01FMXVEE9X21CFW66VFRRZNAK...tion=59.826896ms
    Nov 20 14:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T05:00:03.329Z caller=head.go:809 component=tsdb msg="Head GC completed" duration=2.347334ms
    Nov 20 14:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T05:00:03.386Z caller=compact.go:440 component=tsdb msg="compact blocks" count=2 mint=1637344458250 maxt=1637366400000 ulid=01FMXVEEC8WEK8...tion=50.157896ms
    Nov 20 14:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T05:00:03.393Z caller=db.go:1152 component=tsdb msg="Deleting obsolete block" block=01FMXMJQ5RJ914MKYP3J4QZ7WT
    Nov 20 14:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T05:00:03.394Z caller=db.go:1152 component=tsdb msg="Deleting obsolete block" block=01FMWZNF1BR2H33QNDPE91K04T
    Hint: Some lines were ellipsized, use -l to show in full.



3. [서버 IP]:9090에 접속하여 prometheus의 UI 확인 가능

  • public IP:9090"에 접속하면 아래 UI를 확인 가능

+ Recent posts