Cloud/Kubernetes
kubernetes_sd_config on Prometheus
Jacob_baek
2023. 6. 13. 21:33
service discovery
아래 링크에서 볼수 있듯이 pod 혹은 endpoint, servicem, node 등 다양한 role 선택이 가능하다.
https://prometheus.io/docs/prometheus/latest/configuration/configuration/#pod
simple Service Discovery
아래는 kube-prometheus-stack 을 기반으로 작업한 내용이다.
다음 링크에 소개된데로 additional scrap 설정을 진행한다.
먼저 아래와 같은 service_discovery job을 하나 추가한다.
$ cat prometheus-additional.yaml
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace
regex: ([^:]+)(?::\d+)?;(\d+)
replacement: $1:$2
target_label: __address__
위 job이 지속적으로 scrap 대상을 조건에 맞게 변경하여 scrap을 수행할 것이다.
이후 아래와 같은 명령을 통해 secret을 생성한후
kubectl create secret generic additional-scrape-configs --from-file=$FILENAME --dry-run=client -oyaml > additional-scrape-configs.yaml
kubectl apply -f additional-scrape-configs.yaml -n monitoring
prometheus crd의 spec내에 아래 라인을 추가한다.
spec:
additionalScrapeConfigs:
key: prometheus-additional.yaml
name: additional-scrape-configs
이제 부터는 특정 annotation을 가진 pod들의 metric 을 가져오는 작업을 수행한다.
specific pod
이제 어떤 pod들에 metric을 가져올지를 정하기 위해 다음 annotation을 필요한 pod에 지정한다.
아래와 같이 metric을 출력하고있는 exporter가 존재하는 pod에 요청할 port number와 scrap : true를 지정한다.
metadata:
annotations:
prometheus.io/port: "8888"
prometheus.io/scrape: "true"
이후 해당 pod의 exporter에서 제공하고 있는 metric이 prometheus 에 수집되는것을 볼수 있다.