Connect Agent 측정항목을 Cloud Monitoring으로 내보내기

이 페이지에서는 VMware용 GKE, AWS 기반 GKE 또는 기타 등록된 Kubernetes 클러스터에서 Cloud Monitoring으로 Connect Agent 측정항목을 내보내는 방법을 설명합니다.

개요

VMware용 GKE 또는 AWS 기반 GKE 클러스터에서 Prometheus는 측정항목을 수집하여 클러스터 내에 로컬로 저장합니다. Google Cloud 콘솔에서 클러스터를 등록하면 클러스터에 Connect Agent라는 배포가 생성됩니다. Prometheus는 Connect Agent에서 Google 연결 오류와 열려 있는 연결 수와 같은 유용한 측정항목을 수집합니다. Cloud Monitoring에서 이러한 측정항목을 사용할 수 있게 하려면 다음을 수행해야 합니다.

  • 서비스를 사용하여 Connect Agent를 노출합니다.
  • Prometheus 측정항목을 스크레이핑하여 Cloud Monitoring으로 내보내는 단순한 구성요소인 prometheus-to-sd를 배포합니다.

그런 다음 Google Cloud 콘솔에서 Monitoring을 사용하거나 서비스로 포트를 전달하고 curl을 사용하여 측정항목을 봅니다.

Connect Agent 네임스페이스의 변수 만들기

Connect Agent는 일반적으로 네임스페이스 gke-connect에서 실행됩니다.

Connect Agent에는 hub.gke.io/project라는 라벨이 있습니다. HTTP 서버는 포트 8080에서 리슨합니다.

네임스페이스의 AGENT_NS 변수를 만듭니다.

AGENT_NS=$(kubectl get ns --kubeconfig KUBECONFIG -o jsonpath={.items..metadata.name} -l hub.gke.io/project=PROJECT_ID)

다음을 바꿉니다.

  • KUBECONFIG: 클러스터의 kubeconfig 파일
  • PROJECT_ID: 프로젝트 ID

Connect Agent 배포 노출

  1. 다음 구성을 gke-connect-agent.yaml이라는 YAML 파일에 복사합니다. 이 구성은 Connect Agent 배포를 노출하는 gke-connect-agent라는 서비스를 만듭니다.

    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: gke-connect-agent
      name: gke-connect-agent
    spec:
      ports:
      - port: 8080
        protocol: TCP
        targetPort: 8080
      selector:
        app: gke-connect-agent
      type: ClusterIP
  2. YAML 파일을 클러스터의 Connect Agent 네임스페이스에 적용합니다. 여기서 KUBECONFIG는 클러스터의 kubeconfig 파일 경로입니다.

    kubectl apply -n ${AGENT_NS} --kubeconfig KUBECONFIG -f gke-connect-agent.yaml
  3. roles/monitoring.metricWriter IAM 역할을 Fleet Google 서비스 계정에 결합합니다.

    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" \
        --role="roles/monitoring.metricWriter"

prometheus-to-sd 배포

  1. 다음 구성을 prometheus-to-sd.yaml이라는 YAML 파일에 복사합니다. 각 항목의 의미는 다음과 같습니다.

    • PROJECT_ID는 Google Cloud 프로젝트 ID입니다. 이 값을 찾는 방법 알아보기
    • CLUSTER_NAME은 Connect Agent가 실행되는 Kubernetes 클러스터의 이름입니다.
    • REGION은 클러스터가 실행되는 위치와 지리적으로 가까운 위치입니다. 클러스터가 실제로 위치한 리전과 지리적으로 가까운 Google Cloud 영역을 선택합니다.
    • ZONE은 온프렘 데이터 센터 근처 위치입니다. 트래픽 흐름과 지리적으로 가까운 Google Cloud 영역을 선택합니다.

    이 구성은 리소스 두 개를 만듭니다.

    • 배포에서 사용할 여러 변수를 선언하는 ConfigMap prom-to-sd-user-config
    • 단일 Pod에서 prometheus-to-sd를 실행하는 배포 prometheus-to-monitoring
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: prom-to-sd-user-config
    data:
      # The project that the Connect Agent uses. Accepts ID or number.
      project: PROJECT_ID
      # A name for the cluster, which shows up in Cloud Monitoring.
      cluster_name: CLUSTER_NAME
      # cluster_location must be valid (e.g. us-west1-a); shows up in Cloud Monitoring.
      cluster_location: REGION
      # A zone name to report (e.g. us-central1-a).
      zone: ZONE
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: prometheus-to-monitoring
    spec:
      progressDeadlineSeconds: 600
      replicas: 1
      revisionHistoryLimit: 2
      selector:
        matchLabels:
          run: prometheus-to-monitoring
      template:
        metadata:
          labels:
            run: prometheus-to-monitoring
        spec:
          containers:
            - args:
                - /monitor
                # 'gke-connect-agent' is the text that will show up in the Cloud Monitoring metric name.
                - --source=gke-connect-agent:http://gke-connect-agent:8080
                - --monitored-resource-types=k8s
                - --stackdriver-prefix=custom.googleapis.com
                - --project-id=$(PROM_PROJECT)
                - --cluster-name=$(PROM_CLUSTER_NAME)
                - --cluster-location=$(PROM_CLUSTER_LOCATION)
                - --zone-override=$(PROM_ZONE)
                # A node name to report. This is a dummy value.
                - --node-name=MyGkeConnectAgent
              env:
                - name: GOOGLE_APPLICATION_CREDENTIALS
                  value: /etc/creds/creds-gcp.json
                - name: PROM_PROJECT
                  valueFrom:
                    configMapKeyRef:
                      name: prom-to-sd-user-config
                      key: project
                - name: PROM_CLUSTER_NAME
                  valueFrom:
                    configMapKeyRef:
                      name: prom-to-sd-user-config
                      key: cluster_name
                - name: PROM_CLUSTER_LOCATION
                  valueFrom:
                    configMapKeyRef:
                      name: prom-to-sd-user-config
                      key: cluster_location
                - name: PROM_ZONE
                  valueFrom:
                    configMapKeyRef:
                      name: prom-to-sd-user-config
                      key: zone
              image: gcr.io/google-containers/prometheus-to-sd:v0.7.1
              imagePullPolicy: IfNotPresent
              name: prometheus-to-monitoring
              resources: {}
              terminationMessagePath: /dev/termination-log
              terminationMessagePolicy: File
              volumeMounts:
                - mountPath: /etc/creds
                  name: creds-gcp
                  readOnly: true
          restartPolicy: Always
          schedulerName: default-scheduler
          securityContext: {}
          terminationGracePeriodSeconds: 30
          volumes:
            - name: creds-gcp
              secret:
                defaultMode: 420
                # This secret is already set up for the Connect Agent.
                secretName: creds-gcp
  2. YAML 파일을 클러스터의 Connect Agent 네임스페이스에 적용합니다. 여기서 KUBECONFIG는 클러스터의 kubeconfig 파일 경로입니다.

    kubectl apply -n ${AGENT_NS} --kubeconfig KUBECONFIG -f prometheus-to-sd.yaml

측정항목 보기

콘솔

  1. Google Cloud 콘솔의 Monitoring 페이지로 이동합니다.

    Monitoring 페이지로 이동

  2. 왼쪽 메뉴에서 측정항목 탐색기를 클릭합니다.

  3. Connect Agent의 측정항목에는 custom.googleapis.com/gke-connect-agent/ 프리픽스가 붙습니다. 여기서 gke-connect-agent--source 인수에 지정된 문자열입니다. 예를 들면 custom.googleapis.com/gke-connect-agent/gkeconnect_dialer_connection_errors_total입니다.

cURL

  1. 셸에서 kubectl을 사용하여 gke-connect-monitoring 서비스로 포트를 전달합니다.

    kubectl -n ${AGENT_NS} port-forward svc/gke-connect-monitoring 8080
  2. 다른 셸을 열고 다음을 실행합니다.

    curl localhost:8080/metrics

삭제

이 주제에서 만든 리소스를 삭제하려면 다음을 실행합니다.

AGENT_NS=$(kubectl get ns --kubeconfig KUBECONFIG -o jsonpath={.items..metadata.name} -l hub.gke.io/project)
kubectl delete configmap prom-to-sd-user-config --kubeconfig KUBECONFIG -n ${AGENT_NS}
kubectl delete service gke-connect-agent --kubeconfig KUBECONFIG -n ${AGENT_NS}
kubectl delete deployment prometheus-to-monitoring --kubeconfig KUBECONFIG -n ${AGENT_NS}