Linux 환경에서 Docker 컨테이너를 통해 어플리케이션을 실행하여 간단한 PC 모니터링 시스템을 만들어보겠습니다. 본 글에서는 모니터링 PC에 Prometheus Exporter를 설치하고, 컨테이너를 통해 Prometheus와 Grafana를 서버를 실행하여 모니터링 시스템을 완성해보도록 하겠습니다.
Window Exporter 설치
Prometheus Exporter를 통해 모니터링하고자 하는 PC의 metric을 수집할 수 있습니다. 저희는 Window PC를 모니터링하기 위해 Window Exporter를 타겟 PC에 설치하여 metric을 수집해주겠습니다.
아래 Repository의 [Releases]에서 Window Exporter 패키지 파일을 다운로드 할 수 있습니다.
https://github.com/prometheus-community/windows_exporter
GitHub - prometheus-community/windows_exporter: Prometheus exporter for Windows machines
Prometheus exporter for Windows machines. Contribute to prometheus-community/windows_exporter development by creating an account on GitHub.
github.com
가장 최근 릴리즈 버전의 [Assets] 항목에서 64비트용 msi 파일을 다운로드 받아주겠습니다. 모니터링 타겟 PC가 32비트 Window인 경우에는 바로 위의 '386.msi'로 끝나는 파일을 다운로드 받아주시면 됩니다.
타겟 PC에서 cmd를 관리자 권한으로 실행한 후 다운로드 받은 msi 파일이 위치한 경로에서 설치 관련 명령어를 수행해줍니다. Exporter는 설치된 직후부터 바로 metric을 수집합니다.
아래의 명령어를 수행하면 Window Exporter는 default로 지정된 metric만을 수집합니다.
msiexec /i .\windows_exporter-0.16.0-amd64.msi
추가로 수집하고 싶은 metric이 있다면 ENABLED_COLLECTORS 파라미터를 활용하여 Exporter를 설치해주시면 됩니다. 예를 들어, 'memory' metric을 추가로 수집하고 싶다면 아래처럼 명령어를 수행해주면 됩니다.
msiexec /i .\windows_exporter-0.16.0-amd64.msi ENABLED_COLLECTORS=”[defaults],memory”
수집 가능한 metric 목록은 아래의 README.md의 [Collectors] 파트에서 확인하실 수 있습니다.
https://github.com/prometheus-community/windows_exporter/blob/master/README.md
GitHub - prometheus-community/windows_exporter: Prometheus exporter for Windows machines
Prometheus exporter for Windows machines. Contribute to prometheus-community/windows_exporter development by creating an account on GitHub.
github.com
ENABLED_COLLECTORS 외에도 원하는 세팅으로 Window Exporter를 설치할 수 있도록 다양한 파라미터가 존재합니다.
이름 | 설명 |
ENABLED_COLLECTORS | As the --collectors.enabled flag, provide a comma-separated list of enabled collectors |
LISTEN_ADDR | The IP address to bind to. Defaults to 0.0.0.0 |
LISTEN_PORT | The port to bind to. Defaults to 9182 |
METRICS_PATH | The path at which to serve metrics. Defaults to /metrics |
TEXTFILE_DIR | As the --collector.textfile.directory flag, provide a directory to read text files with metrics from |
REMOTE_ADDR | Allows setting comma separated remote IP addresses for the Windows Firewall exception (whitelist). Defaults to an empty string (any remote address) |
EXTRA_FLAGS | Allows passing full CLI flags. Defaults to an empty string |
설치 완료 후, http://localhost:9182/metrics에서 수집되는 데이터가 확인된다면 정상적으로 Exporter가 설치되었다고 할 수 있습니다.
Prometheus 설정파일 작성
도커 컨테이너를 통해 Prometheus 서버를 설치 및 실행하기 전에, 우선 Prometheus가 어떻게 metric을 수집할 것인지를 명세하는 prometheus.yml 파일을 작성해야 합니다. metric을 수집할 Endpoint, 수집 방식 등을 규칙에 맞게 작성합니다.
Linux 서버로 접속하여 grafana-prometheus라는 이름의 폴더를 하나 생성한 후 vi 편집기로 prometheus.yml 파일을 작성해주겠습니다.
[root@localhost otadmin]# mkdir grafana-prometheus
[root@localhost otadmin]# cd grafana-prometheus
[root@localhost grafana-prometheus]# vi prometheus.yml
모니터링할 자원의 Endpoint만 설정하고 나머지는 default 설정으로 놓겠습니다. 모니터링할 자원은 prometheus 서버와 Window Exporter를 설치한 타겟 PC입니다.
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['172.28.4.243:9090']
- job_name: 'win10'
static_configs:
- targets: ['172.27.176.1:9182]
프로메테우스 설정 파일 구성과 관련하여 더 자세한 사항은 아래의 포스팅을 참고해주시길 바랍니다.
https://b01nstudy.tistory.com/20
prometheus.yml 프로메테우스 설정파일 작성하기
b01nstudy.tistory.com
Prometheus 서버 설치 및 실행
docker run 명령어를 통해 prometheus 이미지를 가져와 컨테이너로 실행하도록 하겠습니다.
[root@localhost grafana-prometheus]# docker run -d -p 9090:9090 \
> -v /home/otadmin/grafana-prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
> -v /home/otadmin/grafana-prometheus/prometheus:/prometheus \
> --name=prometheus \
> prom/prometheus \
> --config.file=/etc/prometheus/prometheus.yml \
> --storage.tsdb.path=/prometheus
--config.file 옵션에는 prometheus.yml 설정 파일 경로를 설정해주고, --storage.tsdb.path 옵션에는 metric 저장소 경로를 설정해줍니다. docker run 명령어와 관련된 옵션들을 아래의 포스팅을 참고해주시길 바랍니다.
https://b01nstudy.tistory.com/21
[Docker] docker run 명령어 정리
b01nstudy.tistory.com
위 명령을 통해 컨테이너를 실행하면 컨테이너가 바로 Exited 상태가 되어버리는 오류가 발생할 수 있습니다.
Linux 상에서 호스트 파일 시스템의 특정 경로를 컨테이너로 마운트(mount)하였을 때 발생할 수 있는 접근 권한 이슈입니다. 아래의 포스팅을 참고하여 오류를 해결해주시면 됩니다.
https://b01nstudy.tistory.com/18
[Linux] err="open /prometheus/queries.active: permission denied"
아래 명령을 통해 Prometheus 이미지를 가져와 컨테이너를 실행하고자 할 때 컨테이너가 바로 Exited 상태가 되어버리는 에러가 발생할 수 있습니다. [root@localhost grafana-prometheus]# docker run -d -p 9090:9090
b01nstudy.tistory.com
명령이 정상적으로 실행되었다면 Linux 서버의 9090번 포트로 접속했을 때 아래와 같은 Prometheus 대시보드를 확인할 수 있습니다. Prometheus 서버에서 기본적으로 제공해주는 대시보드입니다.
[Status] - [Targets] 로 이동하여 모니터링 자원들의 상태를 확인했을 때, State가 DOWN이 아닌 UP으로 표시되어 있다면, 정상적으로 metric을 수집 중임을 알 수 있습니다.
Grafana 서버 설치 및 실행
Prometheus의 기본 대시보드만으로는 수집한 metric을 원하는 형태로 정리하여 보기 어렵습니다. 따라서 일반적으로 Prometheus와 Grafana를 연동하여 Grafana에서 제공하는 다양한 형태의 대시보드를 활용하여 metric을 시각화합니다.
Grafana 역시 이미지를 가져와 컨테이너로 실행해줍니다. 컨테이너를 삭제하면 대시보드 작업 내용이 함께 삭제되기 때문에, 작업 내용을 기록할 디렉토리를 호스트 파일 시스템에 생성한 후 해당 경로를 컨테이너로 마운트(mount) 해줍니다. 여기서, Prometheus와 동일하게 디렉토리 접근 권한 문제가 발생할 수 있으므로 Grafana 컨테이너 유저로 호스트 디렉토리의 소유자를 변경해줍니다.
[root@localhost grafana-prometheus]# mkdir grafana
[root@localhost grafana-prometheus]# chown -R 472:472 ./grafana
아래 명령을 통해 Grafana 컨테이너를 실행해줍니다.
[root@localhost grafana-prometheus]# docker run -d -p 3000:3000 \
> -v /home/otadmin/grafana-prometheus/grafana:/var/lib/grafana \
> --name grafana \
> grafana/grafana
컨테이너가 정상적으로 실행되었다면, Linux 서버의 3000번 포트로 접속하면 아래와 같은 Grafana 로그인 화면이 띄워집니다. Default 계정은 Username과 Password 모두 admin입니다.
Prometheus와 Grafana 연동
Prometheus를 통해 수집한 metric을 Grafana에서 시각화하기 위해 두 서비스를 연동해주도록 하겠습니다.
Grafana 좌측바의 [Configuration] - [Data Sources]를 클릭합니다.
[Add data source] 버튼을 클릭한 후 Prometheus를 선택합니다.
[Settings] - [HTTP]에서 [URL] 항목에는 Prometheus 서버 주소를 작성하고, [Allowed cookies] 항목에는 Server (default)를 입력합니다. 페이지 최하단의 [Save & test] 버튼을 클릭했을 때 'Data source is working'이라는 표시가 뜨면 연동 완료입니다.
Grafana 대시보드 템플릿 적용
작성중
다음 글에서는 docker-compose를 통해
Prometheus와 Grafana 컨테이너를 한 번에 구성해보도록 하겠습니다.
댓글