5.9.3. 서비스 모니터

ServiceMonitorService 오브젝트에서 Endpoints를 검색하고 해당 Pod를 모니터링하도록 Prometheus를 구성하는 Prometheus Operator에서 제공하는 사용자 정의 리소스입니다.

Operator SDK를 사용하여 생성한 Go 기반 Operator에서는 GenerateServiceMonitor() 도우미 함수에서 Service 오브젝트를 가져와서 이를 기반으로 ServiceMonitor 오브젝트를 생성할 수 있습니다.

추가 리소스

5.9.3.1. 서비스 모니터 생성

Operator 작성자는 metrics.CreateServiceMonitor() 도우미 함수를 사용하여 생성한 모니터링 서비스에 대한 서비스 대상 검색 기능을 추가하여 새로 생성한 서비스를 허용할 수 있습니다.

사전 요구 사항

  • Operator SDK를 사용하여 Go 기반 Operator가 생성됨
  • Kubernetes 기반 클러스터에 Prometheus Operator가 배포됨

프로세스

  • Operator 코드에 metrics.CreateServiceMonitor() 도우미 함수를 추가합니다.

    import(
        "k8s.io/api/core/v1"
        "github.com/operator-framework/operator-sdk/pkg/metrics"
        "machine.openshift.io/controller-runtime/pkg/client/config"
    )
    func main() {
    
        ...
        // Populate below with the Service(s) for which you want to create ServiceMonitors.
        services := []*v1.Service{}
        // Create one ServiceMonitor per application per namespace.
        // Change the below value to name of the Namespace you want the ServiceMonitor to be created in.
        ns := "default"
        // restConfig is used for talking to the Kubernetes apiserver
        restConfig := config.GetConfig()
    
        // Pass the Service(s) to the helper function, which in turn returns the array of ServiceMonitor objects.
        serviceMonitors, err := metrics.CreateServiceMonitors(restConfig, ns, services)
        if err != nil {
            // Handle errors here.
        }
        ...
    }