5.14.2. Go 기반 Operator에 대한 사용자 정의 메트릭 노출


Operator 작성자는 controller-runtime/pkg/metrics 라이브러리의 글로벌 Prometheus 레지스트리를 사용하여 사용자 정의 지표를 게시할 수 있습니다.

사전 요구 사항

  • Operator SDK를 사용하여 Go 기반 Operator가 생성됨
  • OpenShift Container Platform 클러스터에 기본적으로 배포되는 Prometheus Operator

프로세스

  1. Operator SDK 프로젝트에서 config/default/kustomization.yaml 파일에서 다음 행의 주석을 제거합니다.

    ../prometheus
  2. Operator에서 추가 지표를 게시하는 사용자 정의 컨트롤러 클래스를 생성합니다. 다음 예제에서는 위젯위젯Failures 수집기를 글로벌 변수로 선언한 다음, 컨트롤러의 패키지에 init() 함수에 등록합니다.

    예 5.29. controllers/memcached_controller_test_metrics.go file

    package 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)
    }
  3. 기본 컨트롤러 클래스의 조정 루프의 일부에서 이러한 컬렉터에 기록하여 메트릭의 비즈니스 논리를 결정합니다.

    예 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
    }
  4. Operator를 빌드하고 내보냅니다.

    $ make docker-build docker-push IMG=<registry>/<user>/<image_name>:<tag>
  5. Operator를 배포합니다.

    $ make deploy IMG=<registry>/<user>/<image_name>:<tag>
  6. 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
  7. 배포된 Operator의 역할 및 역할 바인딩을 적용합니다.

    $ oc apply -f config/prometheus/role.yaml
    $ oc apply -f config/prometheus/rolebinding.yaml
  8. 스크랩할 네임스페이스의 레이블을 설정하여 해당 네임스페이스에 대한 OpenShift 클러스터 모니터링을 활성화합니다.

    $ oc label namespace <operator_namespace> openshift.io/cluster-monitoring="true"

검증

  • OpenShift Container Platform 웹 콘솔에서 메트릭을 쿼리하고 봅니다. 사용자 지정 컨트롤러 클래스에 설정된 이름(예: widgets_totalwidget_failures_total )을 사용할 수 있습니다.
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 문서 정보

Legal Notice

Theme

© 2026 Red Hat
맨 위로 이동