8.2. 사용자 정의 프로젝트에 대한 메트릭 컬렉션 설정

ServiceMonitor 리소스를 생성하여 사용자 정의 프로젝트의 서비스 끝점에서 메트릭을 스크랩할 수 있습니다. 애플리케이션은 Prometheus 클라이언트 라이브러리를 사용하여 메트릭을 /metrics 표준 이름에 노출한다고 가정합니다.

이 섹션에서는 사용자 정의 프로젝트에 샘플 서비스를 배포한 후 서비스 모니터링 방법을 정의하는 ServiceMonitor 리소스를 만드는 방법에 대해 설명합니다.

8.2.1. 샘플 서비스 배포

사용자 정의 프로젝트에서 서비스 모니터링을 테스트하기 위해 샘플 서비스를 배포할 수 있습니다.

프로세스

  1. 서비스 구성에 대한 YAML 파일을 생성합니다. 이 예에서는 prometheus-example-app.yaml이라고 합니다.
  2. 파일에 다음 배포 및 서비스 구성 세부 정보를 추가합니다.

    apiVersion: v1
    kind: Namespace
    metadata:
      name: ns1
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: prometheus-example-app
      name: prometheus-example-app
      namespace: ns1
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: prometheus-example-app
      template:
        metadata:
          labels:
            app: prometheus-example-app
        spec:
          containers:
          - image: ghcr.io/rhobs/prometheus-example-app:0.4.2
            imagePullPolicy: IfNotPresent
            name: prometheus-example-app
    ---
    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: prometheus-example-app
      name: prometheus-example-app
      namespace: ns1
    spec:
      ports:
      - port: 8080
        protocol: TCP
        targetPort: 8080
        name: web
      selector:
        app: prometheus-example-app
      type: ClusterIP

    이 구성은 사용자 정의 ns1 프로젝트에 prometheus-example-app이라는 서비스를 배포합니다. 이 서비스는 사용자 정의 version 메트릭을 노출합니다.

  3. 클러스터에 구성을 적용합니다.

    $ oc apply -f prometheus-example-app.yaml

    서비스를 배포하는 데 시간이 다소 걸립니다.

  4. Pod가 실행 중인지 확인할 수 있습니다.

    $ oc -n ns1 get pod

    출력 예

    NAME                                      READY     STATUS    RESTARTS   AGE
    prometheus-example-app-7857545cb7-sbgwq   1/1       Running   0          81m

8.2.2. 서비스 모니터링 방법 지정

서비스에서 노출하는 메트릭을 사용하려면 /metrics 끝점에서 메트릭을 스크랩하도록 OpenShift Container Platform 모니터링을 구성해야 합니다. 서비스를 모니터링해야 하는 방법을 지정하는 ServiceMonitor(CRD) 또는 Pod를 모니터링해야 하는 방법을 지정하는 PodMonitor CRD를 사용하여 이 작업을 수행할 수 있습니다. 전자에는 Service 오브젝트가 필요하지만 후자에는 필요하지 않으며 Prometheus가 Pod에서 노출하는 메트릭 끝점에서 메트릭을 직접 스크랩할 수 있습니다.

다음 프로세스에서는 사용자 정의 프로젝트에서 서비스에 대한 ServiceMonitor 리소스를 생성하는 방법을 보여줍니다.

사전 요구 사항

  • cluster-admin 클러스터 역할 또는 monitoring-edit 클러스터 역할의 사용자로 클러스터에 액세스할 수 있습니다.
  • 사용자 정의 프로젝트에 대한 모니터링을 활성화했습니다.
  • 이 예제에서는 prometheus-example-app 샘플 서비스를 ns1 프로젝트에 배포했습니다.

    참고

    prometheus-example-app 샘플 서비스는 TLS 인증을 지원하지 않습니다.

절차

  1. ServiceMonitor 리소스 구성에 대한 YAML 파일을 생성합니다. 이 예제에서 파일은 example-app-service-monitor.yaml이라고 합니다.
  2. 다음 ServiceMonitor 리소스 구성 세부 정보를 추가합니다.

    apiVersion: monitoring.coreos.com/v1
    kind: ServiceMonitor
    metadata:
      labels:
        k8s-app: prometheus-example-monitor
      name: prometheus-example-monitor
      namespace: ns1
    spec:
      endpoints:
      - interval: 30s
        port: web
        scheme: http
      selector:
        matchLabels:
          app: prometheus-example-app

    이는 버전 메트릭이 포함된 prometheus-example-app 샘플 서비스에서 노출하는 메트릭을 스크랩하는 ServiceMonitor 리소스를 정의합니다.

    참고

    사용자 정의 네임스페이스의 ServiceMonitor 리소스는 동일한 네임스페이스에서 서비스만 검색할 수 있습니다. 즉 ServiceMonitor 리소스의 namespaceSelector 필드는 항상 무시됩니다.

  3. 클러스터에 구성을 적용합니다.

    $ oc apply -f example-app-service-monitor.yaml

    ServiceMonitor 리소스를 배포하는 데 시간이 다소 걸립니다.

  4. ServiceMonitor 리소스가 실행 중인지 확인할 수 있습니다.

    $ oc -n ns1 get servicemonitor

    출력 예

    NAME                         AGE
    prometheus-example-monitor   81m