5.11. Prometheus를 사용하여 기본 제공 모니터링 구성

이 가이드에서는 Prometheus Operator 및 Operator 작성자 세부 정보를 사용하여 Operator SDK에서 제공하는 기본 제공 모니터링 지원에 대해 설명합니다.

5.11.1. Prometheus Operator 지원

Prometheus는 오픈 소스 시스템 모니터링 및 경고 툴킷입니다. Prometheus Operator는 OpenShift Container Platform과 같은 Kubernetes 기반 클러스터에서 실행되는 Prometheus 클러스터를 생성, 구성, 관리합니다.

Helper 함수는 기본적으로 Operator SDK에 있으며 Prometheus Operator가 배포된 클러스터에서 사용하기 위해 생성한 Go 기반 Operator의 지표를 자동으로 설정합니다.

5.11.2. 사용자 정의 메트릭 노출

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

사전 요구 사항

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

절차

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

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

    예 5.6. 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.7. controllers/memcached_controller.go file

    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 인스턴스에서 서비스 모니터를 스크랩할 수 있도록 역할 및 역할 바인딩 정의를 생성합니다.

    서비스 계정에 네임스페이스의 메트릭을 스크랩할 수 있는 권한이 있도록 역할을 할당해야 합니다.

    예 5.8. config/prometheus/role.yaml 역할

    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: prometheus-k8s-role
      namespace: <operator_namespace>
    rules:
      - apiGroups:
          - ""
        resources:
          - endpoints
          - pods
          - services
          - nodes
          - secrets
        verbs:
          - get
          - list
          - watch

    예 5.9. 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 )을 사용할 수 있습니다.