1. 개요
프로메테우스로 뭐라고 보고 싶은 사람들을 위한 친절한 가이드.
2. Nginx 설치
넘어간다..
3. Nginx 설정
다음과 같은 설정을 추가하고 Nginx를 내렸다 올린다. 이것이 뭘 의미하는지는 자세한 내용은 삵에서 열심히 검색해보자. 아마 삵 초창기부터 Nginx 모니터링에 대한 글이 있었던거 같다.
location /metrics { stub_status on; }
이제 Nginx가 모니터링 가능한 상태가 된 것이다.
$ curl http://127.0.0.1:80/metrics Active connections: 2 server accepts handled requests 13 13 42 Reading: 0 Writing: 1 Waiting: 1
4. nginx-prometheus-exporter 설정
위에서 띄운 Nginx에 붙어서 metric을 가져오는 역할을 하는 놈이라고 보면 된다. 대충 보면 해석되겠지만 9113으로 떠서 /prometheus라는 놈으로 노출한다.
우리는 이걸 Docker로 할 것이다. 그냥 아래처럼 실행하면 되는데, -nginx.scrape-uri
값만 자기 IP에 맞게 바꾼다.
$ sudo docker run -p 9113:9113 nginx/nginx-prometheus-exporter:0.4.2 -nginx.scrape-uri=http://172.17.0.1/metrics -web.telemetry-path=/prometheus 2019/11/11 11:57:22 Starting NGINX Prometheus Exporter Version=0.4.2 GitCommit=f017367 2019/11/11 11:57:22 NGINX Prometheus Exporter has successfully started
5. Prometheus Job 추가
prometheus.yml
에 아래와 같은 job을 추가한다.
위에서 우리는 9113에 /prometheus로 노출하는 놈을 하나 띄웠는데 Prometheus는 그걸 바라볼 것이다.
- job_name: 'nginx-prometheus-test' scrape_interval: 1m metrics_path: '/prometheus' static_configs: - targets: ['127.0.0.1:9113']
그리고 다시 실행한다.
$ ./prometheus --config.file=prometheus.yml level=info ts=2019-11-11T12:01:13.700Z caller=main.go:296 msg="no time or size retention was set so using the default time retention" duration=15d level=info ts=2019-11-11T12:01:13.700Z caller=main.go:332 msg="Starting Prometheus" version="(version=2.13.1, branch=HEAD, revision=6f92ce56053866194ae5937012c1bec40f1dd1d9)" level=info ts=2019-11-11T12:01:13.700Z caller=main.go:333 build_context="(go=go1.13.1, user=root@88e419aa1676, date=20191017-13:15:01)" level=info ts=2019-11-11T12:01:13.700Z caller=main.go:334 host_details="(Linux 4.15.0-1044-aws #46-Ubuntu SMP Thu Jul 4 13:38:28 UTC 2019 x86_64 ip-172-31-6-133 (none))" level=info ts=2019-11-11T12:01:13.700Z caller=main.go:335 fd_limits="(soft=1024, hard=1048576)" level=info ts=2019-11-11T12:01:13.700Z caller=main.go:336 vm_limits="(soft=unlimited, hard=unlimited)" level=info ts=2019-11-11T12:01:13.702Z caller=main.go:657 msg="Starting TSDB ..." level=info ts=2019-11-11T12:01:13.704Z caller=head.go:514 component=tsdb msg="replaying WAL, this may take awhile" level=info ts=2019-11-11T12:01:13.702Z caller=web.go:450 component=web msg="Start listening for connections" address=0.0.0.0:9090 level=info ts=2019-11-11T12:01:13.713Z caller=head.go:562 component=tsdb msg="WAL segment loaded" segment=0 maxSegment=6 level=info ts=2019-11-11T12:01:13.716Z caller=head.go:562 component=tsdb msg="WAL segment loaded" segment=1 maxSegment=6 level=info ts=2019-11-11T12:01:13.718Z caller=head.go:562 component=tsdb msg="WAL segment loaded" segment=2 maxSegment=6 level=info ts=2019-11-11T12:01:13.719Z caller=head.go:562 component=tsdb msg="WAL segment loaded" segment=3 maxSegment=6 level=info ts=2019-11-11T12:01:13.720Z caller=head.go:562 component=tsdb msg="WAL segment loaded" segment=4 maxSegment=6 level=info ts=2019-11-11T12:01:13.721Z caller=head.go:562 component=tsdb msg="WAL segment loaded" segment=5 maxSegment=6 level=info ts=2019-11-11T12:01:13.721Z caller=head.go:562 component=tsdb msg="WAL segment loaded" segment=6 maxSegment=6 level=info ts=2019-11-11T12:01:13.723Z caller=main.go:672 fs_type=EXT4_SUPER_MAGIC level=info ts=2019-11-11T12:01:13.723Z caller=main.go:673 msg="TSDB started" level=info ts=2019-11-11T12:01:13.723Z caller=main.go:743 msg="Loading configuration file" filename=prometheus.yml level=info ts=2019-11-11T12:01:13.739Z caller=main.go:771 msg="Completed loading of configuration file" filename=prometheus.yml level=info ts=2019-11-11T12:01:13.739Z caller=main.go:626 msg="Server is ready to receive web requests."
6. 확인방법
- http://<주소>:9090/targets 에 접속하면 뭐가 뜬다.
-
http://<주소>:9090/graph 에 접속한 후
Expressions에 delta(nginx_http_requests_total{job="nginx-prometheus-test"}[2m])
입력하고 Execute 누르고 Graph 탭 보면 뭐가 나온다.