5.14.2. Go 기반 Operator에 대한 사용자 정의 메트릭 노출
Operator 작성자는 controller-runtime/pkg/metrics 라이브러리의 글로벌 Prometheus 레지스트리를 사용하여 사용자 정의 지표를 게시할 수 있습니다.
사전 요구 사항
- Operator SDK를 사용하여 Go 기반 Operator가 생성됨
- OpenShift Container Platform 클러스터에 기본적으로 배포되는 Prometheus Operator
프로세스
Operator SDK 프로젝트에서
config/default/kustomization.yaml파일에서 다음 행의 주석을 제거합니다.../prometheusOperator에서 추가 지표를 게시하는 사용자 정의 컨트롤러 클래스를 생성합니다. 다음 예제에서는
위젯및위젯Failures수집기를 글로벌 변수로 선언한 다음, 컨트롤러의 패키지에init()함수에 등록합니다.예 5.29.
controllers/memcached_controller_test_metrics.gofilepackage controllers import ( "github.com/prometheus/client_golang/prometheus" "sigs.k8s.io/controller-runtime/pkg/metrics" ) var ( widgets = prometheus.NewCounter( prometheus.CounterOpts{ Name: "widgets_total", Help: "Number of widgets processed", }, ) widgetFailures = prometheus.NewCounter( prometheus.CounterOpts{ Name: "widget_failures_total", Help: "Number of failed widgets", }, ) ) func init() { // Register custom metrics with the global prometheus registry metrics.Registry.MustRegister(widgets, widgetFailures) }기본컨트롤러 클래스의 조정 루프의 일부에서 이러한 컬렉터에 기록하여 메트릭의 비즈니스 논리를 결정합니다.예 5.30.
controllers/memcached_controller.go파일func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { ... ... // Add metrics widgets.Inc() widgetFailures.Inc() return ctrl.Result{}, nil }Operator를 빌드하고 내보냅니다.
$ make docker-build docker-push IMG=<registry>/<user>/<image_name>:<tag>Operator를 배포합니다.
$ make deploy IMG=<registry>/<user>/<image_name>:<tag>OpenShift Container Platform 클러스터의 Prometheus 인스턴스에서 Operator의 서비스 모니터를 스크랩할 수 있도록 역할 및 역할 바인딩 정의를 생성합니다.
서비스 계정에 네임스페이스 메트릭을 스크랩할 수 있는 권한이 있도록 역할을 할당해야 합니다.
예 5.31.
config/prometheus/role.yaml역할apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: prometheus-k8s-role namespace: memcached-operator-system rules: - apiGroups: - "" resources: - endpoints - pods - services - nodes - secrets verbs: - get - list - watch예 5.32.
config/prometheus/rolebinding.yaml역할 바인딩apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: prometheus-k8s-rolebinding namespace: memcached-operator-system roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: prometheus-k8s-role subjects: - kind: ServiceAccount name: prometheus-k8s namespace: openshift-monitoring배포된 Operator의 역할 및 역할 바인딩을 적용합니다.
$ oc apply -f config/prometheus/role.yaml$ oc apply -f config/prometheus/rolebinding.yaml스크랩할 네임스페이스의 레이블을 설정하여 해당 네임스페이스에 대한 OpenShift 클러스터 모니터링을 활성화합니다.
$ oc label namespace <operator_namespace> openshift.io/cluster-monitoring="true"
검증
-
OpenShift Container Platform 웹 콘솔에서 메트릭을 쿼리하고 봅니다. 사용자 지정 컨트롤러 클래스에 설정된 이름(예:
widgets_total및widget_failures_total)을 사용할 수 있습니다.