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

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

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

4.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.3.0
            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