ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • K8s 노드 및 클러스터 리소스 사용량 분석
    kubernetes 2024. 12. 28. 22:07

    1. K8s 노드 및 클러스터 리소스 사용량 분석

    a) 메트릭 수집 도구 활용

    • Kubelet Metrics: 각 노드에서 Kubelet이 제공하는 메트릭을 통해 CPU, 메모리, 디스크, 네트워크 사용량을 모니터링
    • Prometheus & Grafana: 클러스터 전반의 메트릭을 수집하고 시각화하는 데 유용한 도구
    • Metrics Server: Kubernetes 네이티브 리소스 메트릭을 제공하며 HPA와 같은 기능에 사용

    b) 주요 리소스 분석 지표

    • Node Utilization: CPU, 메모리의 사용량 비율을 확인하여 노드 간 부하 분배를 분석
    • Pod Resource Requests/Usage: 각 Pod가 요청(requests)한 리소스와 실제 사용량(usage)을 비교
    • Evicted Pods: 메모리 부족 등으로 인해 축출된 Pod를 모니터링
    • Load Distribution: 클러스터의 워크로드가 특정 노드에 집중되어 있지 않은지 확인

    c) 리소스 사용량 시각화

    • kubectl top 명령어: Pod 및 노드의 실시간 리소스 사용량 확인
    kubectl top nodes
    kubectl top pods
    • Grafana Dashboard: 클러스터 사용량을 한눈에 볼 수 있는 대시보드 구성

    2. 리소스 과다 사용 감지 및 조정

    a) ResourceQuota 설정

    Namespace 단위로 리소스 사용을 제한하여 과도한 리소스 소비를 방지

    • 예시: CPU, 메모리, Pod 수 제한
    apiVersion: v1
    kind: ResourceQuota
    metadata:
      name: compute-quota
      namespace: example-namespace
    spec:
      hard:
        pods: "10"
        requests.cpu: "4"
        requests.memory: "8Gi"
        limits.cpu: "8"
        limits.memory: "16Gi"

    b) LimitRange 설정

    Pod, Container 단위로 리소스 요청(requests)과 상한(limits)을 정의하여 과다 사용 방지

    • 예시: Container당 CPU와 메모리 제한
       
    apiVersion: v1
    kind: LimitRange
    metadata:
      name: limits
      namespace: example-namespace
    spec:
      limits:
      - default:
          cpu: "500m"
          memory: "512Mi"
        defaultRequest:
          cpu: "250m"
          memory: "256Mi"
        type: Container

    c) 과다 사용 탐지 및 알림

    • Prometheus Alerts: 리소스 사용량 초과 시 알림 설정
    • Audit Logs: 특정 사용자가 과도한 리소스를 요청하는지 확인

    3. 워크로드 최적화 및 스케일링

    a) HPA (Horizontal Pod Autoscaler)

    • 기능: Pod의 수를 동적으로 조정
    • 기준: CPU 사용률, 메모리 사용률, 또는 사용자 정의 메트릭(Custom Metrics)
    • 설정 예시:
       
    apiVersion: autoscaling/v2
    kind: HorizontalPodAutoscaler
    metadata:
      name: hpa-example
    spec:
      scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: nginx-deployment
      minReplicas: 2
      maxReplicas: 10
      metrics:
      - type: Resource
        resource:
          name: cpu
          target:
            type: Utilization
            averageUtilization: 50

    b) VPA (Vertical Pod Autoscaler)

    • 기능: Pod 내 컨테이너의 requests와 limits를 동적으로 조정
    • 도입 효과: 리소스 과소/과다 요청을 방지하여 노드 활용 효율성 증가
    • 설정 예시:
    apiVersion: autoscaling.k8s.io/v1
    kind: VerticalPodAutoscaler
    metadata:
      name: vpa-example
      namespace: default
    spec:
      targetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: nginx-deployment
      updatePolicy:
        updateMode: "Auto"

    c) Cluster Autoscaler

    • 기능: 워크로드에 따라 클러스터의 노드 수를 자동으로 조정
    • 도입 사례: GKE, EKS, AKS 등 매니지드 클러스터에서 지원

    결론

    효율적인 리소스 관리 및 최적화를 위해서는 지속적인 모니터링과 정책 기반 제어가 필수적입니다. Kubernetes의 네이티브 기능(ResourceQuota, LimitRange, HPA, VPA)과 도구(Prometheus, Grafana)를 적절히 활용하면 안정성과 비용 효율성을 동시에 확보할 수 있습니다.

Designed by Tistory.