Study/Troubleshooting

[Prometheus] Target 비활성화 문제 - 해결

마늘김 2023. 10. 22. 23:39

환경

  • Ubuntu 22.04.3 LTS
  • Kubernetes 1.28.2
  • Prometheus Community Version

증상

  • 몇 개의 Target이 'Connection refused' 메시지와 함께 메트릭 수집 불가

[사진 1] 설정된 Prometheus Target 중 일부의 메트릭 값 수집 불가

원인

  • Kubernetes의 Static Pod YAML의 metric에 관한 기본 설정값이 localhost로 설정되어 있기 때문에 'connection refused'가 발생
  • 따라서 Static Pod의 YAML 파일 편집이 필요

 

조치

  • /etc/kubernetes/manifests/ 경로의 Static Pod의 YAML 값 변경

[사진 2] /etc/kubernetes/manifests/ 경로에 위치한 Static Pod의 YAML 파일

#etcd.yaml
#spec.contaienrs 필드의 값 중 - --listen-metrics-urls 주소와
#livenessProbe.httpGet.host, startupProbe.httpGet.host의 값을 모두 0.0.0.0으로 변경
#또는 Control Plane의 노드 IP 주소로도 변경해도 무방함

apiVersion: v1
kind: Pod
metadata:
  annotations:
    kubeadm.kubernetes.io/etcd.advertise-client-urls: https://10.110.0.11:2379
  creationTimestamp: null
  labels:
    component: etcd
    tier: control-plane
  name: etcd
  namespace: kube-system
spec:
  containers:
  - command:
    - etcd
    - --advertise-client-urls=https://10.110.0.11:2379
    - --cert-file=/etc/kubernetes/pki/etcd/server.crt
    - --client-cert-auth=true
    - --data-dir=/var/lib/etcd
    - --experimental-initial-corrupt-check=true
    - --experimental-watch-progress-notify-interval=5s
    - --initial-advertise-peer-urls=https://10.110.0.11:2380
    - --initial-cluster=alpha-k8s-cp=https://10.110.0.11:2380
    - --key-file=/etc/kubernetes/pki/etcd/server.key
    - --listen-client-urls=https://127.0.0.1:2379,https://10.110.0.11:2379
    - --listen-metrics-urls=http://0.0.0.0:2381   #기존 127.0.0.1을 0.0.0.0으로 변경
    - --listen-peer-urls=https://10.110.0.11:2380
    - --name=alpha-k8s-cp
    - --peer-cert-file=/etc/kubernetes/pki/etcd/peer.crt
    - --peer-client-cert-auth=true
    - --peer-key-file=/etc/kubernetes/pki/etcd/peer.key
    - --peer-trusted-ca-file=/etc/kubernetes/pki/etcd/ca.crt
    - --snapshot-count=10000
    - --trusted-ca-file=/etc/kubernetes/pki/etcd/ca.crt
    image: registry.k8s.io/etcd:3.5.9-0
    imagePullPolicy: IfNotPresent
    livenessProbe:
      failureThreshold: 8
      httpGet:
        host: 0.0.0.0   #기존 127.0.0.1을 0.0.0.0으로 변경
        path: /health?exclude=NOSPACE&serializable=true
        port: 2381
        scheme: HTTP
      initialDelaySeconds: 10
      periodSeconds: 10
      timeoutSeconds: 15
    name: etcd
    resources:
      requests:
        cpu: 100m
        memory: 100Mi
    startupProbe:
      failureThreshold: 24
      httpGet:
        host: 0.0.0.0   #기존 127.0.0.1을 0.0.0.0으로 변경
        path: /health?serializable=false
        port: 2381
        scheme: HTTP
      initialDelaySeconds: 10
      periodSeconds: 10
      timeoutSeconds: 15
    volumeMounts:
    - mountPath: /var/lib/etcd
      name: etcd-data
    - mountPath: /etc/kubernetes/pki/etcd
      name: etcd-certs
  hostNetwork: true
  priority: 2000001000
  priorityClassName: system-node-critical
  securityContext:
    seccompProfile:
      type: RuntimeDefault
  volumes:
  - hostPath:
      path: /etc/kubernetes/pki/etcd
      type: DirectoryOrCreate
    name: etcd-certs
  - hostPath:
      path: /var/lib/etcd
      type: DirectoryOrCreate
    name: etcd-data
status: {}
#kube-controller-manager.yaml
#spec.contaienrs 필드의 값 중 - ----bind-address 주소와
#livenessProbe.httpGet.host, startupProbe.httpGet.host의 값을 모두 0.0.0.0으로 변경
#또는 Control Plane의 노드 IP 주소로도 변경해도 무방함

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    component: kube-controller-manager
    tier: control-plane
  name: kube-controller-manager
  namespace: kube-system
spec:
  containers:
  - command:
    - kube-controller-manager
    - --authentication-kubeconfig=/etc/kubernetes/controller-manager.conf
    - --authorization-kubeconfig=/etc/kubernetes/controller-manager.conf
    - --bind-address=0.0.0.0   #기존 127.0.0.1을 0.0.0.0으로 변경
    - --client-ca-file=/etc/kubernetes/pki/ca.crt
    - --cluster-name=kubernetes
    - --cluster-signing-cert-file=/etc/kubernetes/pki/ca.crt
    - --cluster-signing-key-file=/etc/kubernetes/pki/ca.key
    - --controllers=*,bootstrapsigner,tokencleaner
    - --kubeconfig=/etc/kubernetes/controller-manager.conf
    - --leader-elect=true
    - --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.crt
    - --root-ca-file=/etc/kubernetes/pki/ca.crt
    - --service-account-private-key-file=/etc/kubernetes/pki/sa.key
    - --use-service-account-credentials=true
    image: registry.k8s.io/kube-controller-manager:v1.28.2
    imagePullPolicy: IfNotPresent
    livenessProbe:
      failureThreshold: 8
      httpGet:
        host: 0.0.0.0   #기존 127.0.0.1을 0.0.0.0으로 변경
        path: /healthz
        port: 10257
        scheme: HTTPS
      initialDelaySeconds: 10
      periodSeconds: 10
      timeoutSeconds: 15
    name: kube-controller-manager
    resources:
      requests:
        cpu: 200m
    startupProbe:
      failureThreshold: 24
      httpGet:
        host: 0.0.0.0   #기존 127.0.0.1을 0.0.0.0으로 변경
        path: /healthz
        port: 10257
        scheme: HTTPS
      initialDelaySeconds: 10
      periodSeconds: 10
      timeoutSeconds: 15
... (이하 생략)

 위와 같이 Metric 수집이 되지 않는 Prometheus Target의 YAML 파일을 수정하고 저장하면 정상적으로 작동하는 것을 확인할 수 있음.

[사진 3] 정상적으로 Metric 값을 수집

추가

  • Kubernetes의 Static Pod 요소들은 코드 레벨에서 이미 Metric 값을 노출할 수 있도록 정의되어 있어 위와 같이 설정만 변경해 주면 Prometheus에서 수집이 가능
  • Static Pod의 경우 YAML 파일을 오류 없이 수정하면 자동으로 해당 내용으로 재시작 되므로 따로 Pod를 재시작할 필요는 없음