본문 바로가기
서버구축 (WEB,DB)

Kubernetes 클러스터 Kubeview와 Prometheus 리소스 모니터링

by 날으는물고기 2024. 9. 27.

Kubernetes 클러스터 Kubeview와 Prometheus 리소스 모니터링

Kubeview는 Kubernetes 클러스터의 리소스를 시각적으로 모니터링하고 관리할 수 있는 웹 애플리케이션입니다. Kubeview를 구성하는 방법은 다음과 같습니다.

  1. Kubernetes 클러스터: Kubeview를 배포할 Kubernetes 클러스터가 필요합니다.
  2. kubectl: 클러스터와 상호작용하기 위해 kubectl이 필요합니다.
  3. Helm (옵션): Helm을 사용하여 Kubeview를 쉽게 설치할 수 있습니다.

설치 방법

1. Helm을 사용하여 설치

Helm을 사용하면 Kubeview를 간단히 설치할 수 있습니다.

  1. Helm 설치 (이미 설치되어 있다면 생략)
    curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
  2. Kubeview Helm 차트 저장소 추가
    helm repo add kubeview https://benc-uk.github.io/kubeview/charts
    helm repo update
  3. Kubeview 설치
    helm install kubeview kubeview/kubeview

2. kubectl을 사용하여 수동으로 설치

  1. Kubeview의 YAML 파일을 다운로드합니다.
    curl -LO https://raw.githubusercontent.com/benc-uk/kubeview/main/deploy/k8s/kubeview.yaml
  2. YAML 파일을 적용하여 Kubeview를 설치합니다.
    kubectl apply -f kubeview.yaml

구성 확인

  1. Kubeview가 올바르게 배포되었는지 확인합니다.
    kubectl get pods -n kubeview
  2. 서비스가 실행 중인지 확인합니다.
    kubectl get svc -n kubeview
  3. 로드 밸런서 또는 NodePort를 통해 Kubeview 웹 인터페이스에 접근할 수 있습니다. 서비스의 외부 IP 또는 포트를 확인하여 브라우저에서 접속합니다.
    kubectl get svc -n kubeview

추가 설정

  • Role-Based Access Control (RBAC): 보안을 강화하기 위해 RBAC 설정을 적용할 수 있습니다. Kubeview는 클러스터 정보를 읽기만 하므로 read-only 접근 권한만 부여해야 합니다.
  • Ingress 설정: 클러스터에 Ingress 컨트롤러가 설치되어 있다면, Ingress를 설정하여 도메인을 통해 Kubeview에 접근할 수 있습니다.

예시) Ingress 설정

  1. Ingress 리소스 생성
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: kubeview-ingress
      namespace: kubeview
    spec:
      rules:
      - host: kubeview.example.com
        http:
          paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: kubeview
                port:
                  number: 80
  2. Ingress 적용
    kubectl apply -f ingress.yaml

위 단계를 통해 Kubeview를 Kubernetes 클러스터에 배포하고 구성할 수 있습니다. Kubeview를 사용하여 클러스터 리소스를 시각적으로 모니터링하고 관리할 수 있습니다.

Prometheus를 통해 Kubernetes 클러스터를 모니터링하는 방법은 매우 강력하고 유연하며, 클러스터 상태, 리소스 사용량, 애플리케이션 성능 등을 실시간으로 모니터링할 수 있습니다. Prometheus는 기본적으로 Kubernetes와 잘 통합되어 있으며, 그 설정 과정은 크게 Prometheus 설치, Kubernetes에서 필요한 리소스 설정, 그리고 Grafana와 같은 시각화 도구 연동으로 이루어집니다. 아래는 Prometheus를 Kubernetes 클러스터에 설치하고 모니터링하는 방법에 대한 단계별 설명입니다.

1. Prometheus 설치

Prometheus는 Helm 차트를 사용해 설치하는 것이 가장 간편합니다. Helm은 Kubernetes 패키지 매니저로, 여러 리소스를 자동으로 설정해 줍니다.

 

1.1 Helm 설치

Helm이 설치되어 있지 않다면, 다음 명령어로 설치할 수 있습니다.

curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash

1.2 Prometheus Helm 차트 추가

Prometheus 차트를 Helm 레포지토리에 추가하고 업데이트합니다.

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update

1.3 Prometheus 설치

Prometheus를 Kubernetes 클러스터에 설치합니다. prometheus라는 이름으로 네임스페이스를 생성한 후 Prometheus를 설치합니다.

kubectl create namespace prometheus
helm install prometheus prometheus-community/prometheus --namespace prometheus

2. Prometheus Operator 사용

Prometheus Operator는 보다 쉽게 Prometheus 인스턴스를 관리할 수 있게 해주는 도구입니다. 이를 사용하면 Prometheus 구성과 관리가 간단해지며, 모니터링 대상 서비스에 대한 자동 디스커버리 기능을 제공할 수 있습니다.

Prometheus Operator를 설치하는 방법은 다음과 같습니다.

helm install prometheus-operator prometheus-community/kube-prometheus-stack --namespace prometheus

3. Prometheus 설정

Prometheus는 기본적으로 Kubernetes의 ServiceMonitorPodMonitor를 사용하여 서비스 및 포드를 자동으로 모니터링합니다. 이를 위해서는 각 애플리케이션 또는 네임스페이스에서 Prometheus가 모니터링할 리소스를 지정해 주어야 합니다.

 

3.1 ServiceMonitor 설정

ServiceMonitor는 Prometheus가 특정 서비스를 모니터링하도록 지정하는 리소스입니다. 예를 들어, 네임스페이스 내의 특정 애플리케이션을 모니터링하려면 다음과 같이 설정합니다.

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: my-service-monitor
  namespace: prometheus
spec:
  selector:
    matchLabels:
      app: my-app
  endpoints:
  - port: web
    interval: 30s

위 설정은 my-app이라는 라벨을 가진 애플리케이션의 web 포트를 30초마다 모니터링하도록 설정합니다.

4. Grafana를 사용한 시각화

Prometheus로 수집한 데이터를 보다 직관적으로 시각화하려면 Grafana를 사용하는 것이 좋습니다. Grafana는 Prometheus 데이터를 시각화할 수 있는 다양한 대시보드를 제공합니다.

 

4.1 Grafana 설치

Grafana도 Prometheus와 마찬가지로 Helm을 사용해 설치할 수 있습니다.

helm install grafana grafana/grafana --namespace prometheus

4.2 Grafana 설정

Grafana를 설치한 후, Prometheus를 데이터 소스로 추가해야 합니다. 다음과 같은 설정으로 Grafana에서 Prometheus 데이터를 연결합니다.

  1. Grafana 웹 UI에 접속. (Helm 설치 시 자동으로 설정된 주소에서 접속)
  2. 좌측 메뉴에서 Configuration -> Data Sources 선택.
  3. Add data source를 클릭한 후, Prometheus 선택.
  4. Prometheus 서버 주소를 입력. (예: http://prometheus-server.prometheus.svc:9090)

4.3 Grafana 대시보드 설정

Prometheus와 연동한 후 Kubernetes 클러스터 상태를 모니터링할 수 있는 다양한 대시보드를 가져올 수 있습니다. Grafana에서 제공하는 Kubernetes 관련 대시보드는 Grafana 사이트나 community 대시보드에서 가져올 수 있습니다. Grafana 대시보드 ID는 6417 등을 사용할 수 있으며, 이를 통해 Kubernetes 클러스터 상태를 종합적으로 확인할 수 있습니다.

5. Prometheus 알림 설정

Prometheus는 Alertmanager와 통합되어 경고 알림을 설정할 수 있습니다. Alertmanager를 통해 이메일, Slack, PagerDuty 등으로 알림을 보낼 수 있습니다.

 

5.1 Alertmanager 설정

Prometheus 설정 파일에서 Alertmanager를 추가합니다.

alerting:
  alertmanagers:
  - static_configs:
    - targets:
      - alertmanager:9093

5.2 알림 규칙 설정

알림 규칙은 Prometheus의 설정 파일을 통해 설정합니다. 예를 들어, 특정 메모리 사용량을 초과했을 때 알림을 보낼 수 있도록 다음과 같이 설정할 수 있습니다.

groups:
- name: example
  rules:
  - alert: HighMemoryUsage
    expr: node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100 < 10
    for: 5m
    labels:
      severity: critical
    annotations:
      summary: "메모리 사용량 경고"
      description: "메모리 사용량이 10% 미만입니다."

이 설정은 5분 동안 메모리 사용량이 10% 미만일 경우, 경고를 발생시킵니다.

 

Prometheus를 통해 Kubernetes 클러스터를 모니터링하면 클러스터 내부 리소스 사용 현황, 애플리케이션 상태, 성능 문제 등을 실시간으로 감시할 수 있습니다. Grafana와 연동하면 데이터를 시각화하여 한눈에 클러스터 상태를 파악할 수 있으며, Alertmanager를 통해 문제 발생 시 자동으로 알림을 받을 수 있습니다.

Prometheus와 Grafana를 활용한 Kubernetes 모니터링은 확장성과 유연성이 매우 뛰어나기 때문에, 지속적으로 클러스터 상태를 확인하고 관리하기에 적합합니다.

728x90

댓글