Flux 커스텀 Prometheus 메트릭
kube-state-metrics를 사용해 Flux Prometheus 메트릭을 확장하는 방법을 설명해요.
기본적으로 Flux 표준 설치본은 컨트롤러와 그 내부 동작에 대한 특정 메트릭 집합을 내보내는데, 모든 사용자의 요구를 충족하지는 못할 수 있어요. 일부 메트릭은 모든 Flux 컨트롤러에 공통이고, 일부는 몇몇 컨트롤러에만 매우 특화되어 있어요. 이 메트릭들에 가능한 모든 정보 라벨을 추가하는 건 메트릭의 카디널리티(cardinality)를 높일 수 있어 현실적이지 않아요. 이 메트릭들은 Flux의 운영에 관한 것이므로, 커스텀 라벨을 추가하는 것이 Flux를 관리·유지하는 사람들에게 더 유용할 뿐 큰 이득이 되지 않아요. 대부분의 경우 Flux 커스텀 리소스를 통해 Flux와 상호작용하는 Flux 사용자들은 자신이 다루는 리소스에 대해 알고 싶어 해요. 예를 들어 GitRepository의 상태와 그 브랜치 또는 태그 참조 같은 것들이요. 이런 메트릭은 kube-prometheus-stack의 일부인 kube-state-metrics (KSM)를 사용해 스크래핑할 수 있어요. KSM은 리소스 메트릭에 커스텀 라벨을 추가하도록 구성할 수 있는데, 예를 들어 리소스의 status에서 얻은 어떤 값이나 팀 이름, 부서 이름 같은 임의의 값이 될 수 있어요.
kube-state-metrics 설정
kube-state-metrics는 kube-prometheus-stack을 사용해 전체 모니터링 스택과 함께 설치할 수 있어요. fluxcd/flux2-monitoring-example 레포지토리에는 Flux를 모니터링하도록 kube-prometheus-stack을 배포하고 구성하는 예제 설정이 포함되어 있어요. 이 설정들을 어떻게 커스터마이즈할 수 있는지 다음 섹션들에서 자세히 다룰게요.
모니터링 스택을 설치하는 데 Kube-prometheus-stack Helm 차트가 사용돼요. 차트 값에서 kube-state-metrics 관련 구성은 kube-state-metrics-config.yaml이라는 별도 파일에 존재해요. 이 파일은 KSM이 custom-resource-state-only 모드로 실행되도록 구성해요. 이 상태에서 KSM은 Kubernetes 코어 리소스에 대한 메트릭을 수집하지 않아요. rbac 섹션은 KSM이 Flux 커스텀 리소스를 나열(watch)할 수 있는 접근 권한을 제공해요. image-reflector-controller와 image-automation-controller를 사용하지 않는다면 API 그룹(image.toolkit.fluxcd.io)과 리소스(imagerepositories, imagepolicies, imageupdateautomations)는 제거할 수 있어요. customResourceState 섹션은 Flux 메트릭이 어떻게 구성되는지 정의해요.
배포되면 KSM은 kube-apiserver에서 Flux 리소스 메트릭을 수집해 구성된 대로 내보내기 시작해요.
커스텀 메트릭 추가
위 설정에서 사용된 예제 customResourceState 값은 name, exported_namespace, suspended, ready 등의 라벨을 가진 gotk_resource_info라는 메트릭을 추가해요.
- name: "resource_info"
help: "The current state of a GitOps Toolkit resource."
each:
type: Info
info:
labelsFromPath:
name: [metadata, name]
labelsFromPath:
exported_namespace: [metadata, namespace]
suspended: [spec, suspend]
ready: [status, conditions, "[type=Ready]", status]
...
이렇게 해서 Flux 리소스의 현재 상태가 제공돼요. Flux 리소스의 준비 상태(readiness)를 모니터링하는 데 사용할 수 있어요. 라벨을 더 추가하도록 편집할 수도 있는데, 자세한 내용은 다음 섹션에서 다룰게요.
마찬가지로 metrics 목록에 추가하면 더 많은 커스텀 메트릭을 추가할 수 있어요. 예를 들어 HelmRelease가 마지막으로 적용한 차트와 앱 버전에 대한 메트릭을 만들려면 HelmRelease 리소스 메트릭 섹션을 추가하세요:
...
customResourceState:
config:
spec:
resources:
- groupVersionKind:
group: helm.toolkit.fluxcd.io
version: "v2"
kind: HelmRelease
metricNamePrefix: gotk
metrics:
- name: "resource_info"
help: "The current state of a GitOps Toolkit resource."
each:
type: Info
info:
labelsFromPath:
name: [metadata, name]
labelsFromPath:
exported_namespace: [metadata, namespace]
suspended: [spec, suspend]
ready: [status, conditions, "[type=Ready]", status]
- name: "helmrelease_version_info"
help: "The version information of helm release resource."
each:
type: Info
info:
labelsFromPath:
chart_version: [status, history, "0", chartVersion]
app_version: [ status, history, "0", appVersion ]
labelsFromPath:
name: [metadata, name]
exported_namespace: [metadata, namespace]
chart_name: [spec, chart, spec, chart]
...
위 구성에서 HelmRelease에 대해 gotk_resource_info와 gotk_helmrelease_version_info 메트릭이 내보내져요.
# HELP gotk_resource_info The current state of a GitOps Toolkit resource.
# TYPE gotk_resource_info info
gotk_resource_info{customresource_group="helm.toolkit.fluxcd.io",customresource_kind="HelmRelease",customresource_version="v2",exported_namespace="monitoring",name="kube-prometheus-stack",ready="True"} 1
# HELP gotk_helmrelease_version_info The version information of helm release resource.
# TYPE gotk_helmrelease_version_info info
gotk_helmrelease_version_info{chart_mame="kube-prometheus-stack",chart_version="61.3.2",app_version="v0.75.1",customresource_group="helm.toolkit.fluxcd.io",customresource_kind="HelmRelease",customresource_version="v2",exported_namespace="monitoring",name="kube-prometheus-stack"} 1
커스텀 메트릭 라벨 추가
더 의미 있는 모니터링 메트릭을 만들기 위해 메트릭에 커스텀 라벨을 추가할 수 있어요. 예를 들어 클러스터의 모든 리소스에 공통적인 ownedBy 라벨, 객체 라벨에서 가져온 businessUnit 또는 department 이름 같은 것들이요.
예를 들어 GitRepository 객체에 department 라벨이 붙어 있다면:
apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata:
name: foo
namespace: bar
labels:
department: baz
spec:
interval: 1h
ref:
branch: main
url: https://github.com/fluxcd/flux2-monitoring-example
KSM customResourceState 값을 구성해서 부서 이름, 담당 팀 이름, Git 브랜치 이름을 추출하고 이를 GitRepository의 gotk_resource_info 메트릭의 라벨로 포함할 수 있어요.
...
customResourceState:
config:
spec:
resources:
- groupVersionKind:
group: source.toolkit.fluxcd.io
version: "v1"
kind: GitRepository
metricNamePrefix: gotk
commonLabels:
ownedBy: teamA
labelsFromPath:
department: [metadata, labels, department]
metrics:
- name: "resource_info"
help: "The current state of a GitOps Toolkit resource."
each:
type: Info
info:
labelsFromPath:
name: [metadata, name]
labelsFromPath:
exported_namespace: [metadata, namespace]
suspended: [spec, suspend]
ready: [status, conditions, "[type=Ready]", status]
branch: [spec, ref, branch]
...
위 구성은 다음과 같은 메트릭을 만들어내요:
gotk_resource_info{branch="main",customresource_group="source.toolkit.fluxcd.io",customresource_kind="GitRepository",customresource_version="v1",department="baz",exported_namespace="bar",name="foo",ownedBy="teamA",ready="True"} 1
여기에는 ownedBy="teamA", department="baz", branch="main" 라벨이 포함돼요. 마찬가지로 필요에 따라 더 많은 커스텀 라벨을 추가할 수 있어요.
메트릭 커스터마이즈에 대해 더 자세히 알아보려면 kube-state-metrics custom-resource state configuration 문서를 참고하세요.
더 알아보기 (Learn more)
출처: 문서