Prometheus ServiceMonitor 실습

Jerry(이정훈)
9 min readMar 31, 2021

오늘은 Prometheus 에서 수집하는 Metric 정보를 Operator ServiceMonitor 통하여 가져오는 것을 실습과 함께 알아 보겠습니다.

먼저, ServiceMonitor란 CRD(CustomResourceDefinition) 방식으로 Operator (코어OS 최초 시작)를 이용하여 Metric을 관리하는 방법 입니다. (자세한 내용은 인용 블로그 참조) Metric 추가 작업 시 prometheus.yml 파일에 manual하게 추가하는 것이 아니고 별도의 Kubernetes Object Servicemonitor (마치 configmap, deployment 처럼)를 이용합니다.

ServiceMonitor & Operator 구조

TL;DR

  • ServiceMonitor 이용하여 Prometheus Metric 추가 가능
  • Prometheus 재기동 없이 자동 추가, metric 설정 빠르게 확인 가능
  • Metic 내역만 별도로 관리하여 재사용성이 뛰어남
  • Prometheus Operator ServiceMonitor Selector의 key, value와 ServiceMonitor 동일하게 지정 ^^

Diamanti & Manage Kubernetes Service 문의 : leejunghoon@spkr.co.kr

The concept of software operators was introduced by CoreOS back in 2016. In a nutshell, an operator is any application-specific or domain-specific controller that extends the Kubernetes API to simplify deployment, configuration, and management of complex stateful applications on behalf of Kubernetes users.

<from supergiant>

Metric 추가 후 Prometheus 리스타트가 필요 없고 kube object 형태로 관리되니 가시성 및 재사용성이 뛰어 납니다. (이런 조그마한 개선들이 하나하나 모여야 가장 소중한 자원인 엔지니어 시간을 절약 할 수 있습니다.)

그럼, 실습으로 확인해 보겠습니다.

  1. Prometheus-stack Helm 설치
    먼저 Prometheus 설치는 prometheus-stack Helm을 사용합니다. 여러번 반복되는데, Helm을 사용하면 application 설치 시간을 많이 줄일 수 있습니다.

prometheus stack은 prometheus metric, alert rule은 물론 grafana dashboard 까지 default로 포함되어 여러모로 참 편리 합니다. prometheus 설치 시 반드시 사용하는 것을 추천 드립니다.

[spkr@erdia22 ~ (scluster:prometheus)]$ kgp(k get pod)
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
alertmanager-prometheus-kube-prometheus-alertmanager-0 2/2 Running 0 17h 10.10.100.39 dia03 <none> <none>
prometheus-grafana-cbcd6b857-5crvd 2/2 Running 0 17h 10.10.100.35 dia03 <none> <none>
prometheus-kube-prometheus-operator-8dc45cc65-hclsx 1/1 Running 0 17h 10.10.100.37 dia03 <none> <none>
prometheus-kube-state-metrics-6bfcd6f648-gq4jh 1/1 Running 0 17h 10.10.100.36 dia03 <none> <none>
prometheus-prometheus-kube-prometheus-prometheus-0 2/2 Running 1 13h 10.10.100.17 dia03 <none> <none>
prometheus-prometheus-node-exporter-4jjvr 1/1 Running 0 17h 172.17.16.161 dia01 <none> <none>
prometheus-prometheus-node-exporter-g6hzk 1/1 Running 0 17h 172.17.16.163 dia03 <none> <none>
prometheus-prometheus-node-exporter-mtbh9 1/1 Running 0 17h 172.17.16.162 dia02 <none> <none>
<설치 후 pod 확인 화면>

2. default servicemonitor 확인

Helm으로 설치하면 values.yml 설치 옵션에 따라 servicemonitor가 default로 추가 됩니다. 저는 default 설정에서 coredns, kubelet 등을 제외 하였습니다.

[spkr@erdia22 ~ (scluster:prometheus)]$ k get servicemonitors.monitoring.coreos.com
NAME AGE
prometheus-kube-prometheus-alertmanager 17h
prometheus-kube-prometheus-grafana 17h
prometheus-kube-prometheus-kube-state-metrics 17h
prometheus-kube-prometheus-node-exporter 17h
prometheus-kube-prometheus-operator 17h
prometheus-kube-prometheus-prometheus 17h

그럼, default로 설치되는 servicemonitor를 기반으로 새로운 servicemonitor 생성 하겠습니다. 먼저, 기존 yaml 설정을 export 합니다.

[spkr@erdia22 ~ (scluster:prometheus)]$ k get servicemonitors.monitoring.coreos.com prometheus-kube-prometheus-alertmanager -o yaml |k neat > alertmanager-servicemonitor.yml
  • endpoint : metric 해당 서비스의 port 이름으로 지정
  • selector: 해당 metric이 지정된 service object 의 label 지정

처음에는 약간 혼란스럽지만 service object 생성하듯이 label 기반으로 관리하므로 그리 어렵지는 않습니다.

여기서 주의해야 할 것은 label > release: prometheus 설정 입니다. 해당 key, value 기준으로 Prometheus servicemonitor를 추가 합니다. Prometheus Object를 확인하면

[spkr@erdia22 ~ (scluster:prometheus)]$ k describe prometheus
(중략)
Service Monitor Selector:
Match Labels:
Release: prometheus

(prometheus 자체도 operator로 관리합니다.) 위와 같이 service monitor selector 항목이 있습니다. 여러 개의 service monitor 중 release: prometheus 인 것만 나는 metric으로 등록하겠다는 의미이므로 service monitor 추가 시 위 항목을 label로 꼭 추가해야 합니다. (이걸 몰라서 2~3일 삽집 하였습니다.)

3. servicemonitor 추가

그럼, 위 default servicemonitor 기준으로 새로운 servicemonitor를 추가 하겠습니다.

  • label 에 release: prometheus , prometheus object의 key:value와 꼭 맞추어 줍니다.

apply 하시면 servicemonitor 확인이 가능합니다.

[spkr@erdia22 ~ (scluster:prometheus)]$ k get servicemonitors.monitoring.coreos.com
NAME AGE
nginx 13h
prometheus-kube-prometheus-alertmanager 17h
prometheus-kube-prometheus-grafana 17h
prometheus-kube-prometheus-kube-state-metrics 17h
prometheus-kube-prometheus-node-exporter 17h
prometheus-kube-prometheus-operator 17h
prometheus-kube-prometheus-prometheus 17h

Prometheus 관리 창에서 target으로 보시면 아래와 같이 nginx target 확인 가능합니다. 성공적으로 metric 추가 작업이 완료 되었습니다. 편리한 점은 prometheus pod restart 하지 않아도 약 15s 후에 자동으로 metric이 추가되었습니다.

모니터링은 정말 시간이 많이 필요로 하는 작업 입니다. 개별 상황에 따라 요구 사항이 다 달라서 표준화 하기도 불가능합니다. 한땀 한땀 작업 하는데 SeriveMonitor가 작은 도움이 되실 수 있습니다.

참조

--

--

Jerry(이정훈)

DevOps/Automation Engineer 60살까지 콘솔 잡는 엔지니어를 목표로 느리게 생각하고 있습니다.