본문 바로가기
Come on IT/DevOps

Prometheus 및 Grafana 설치 (feat. 쿠베네티스)

by JONGSKY 2024. 10. 7.
728x90
SMALL

1. Helm 설치

먼저 Helm이 설치되어 있지 않다면 Helm을 설치합니다.

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

2. Helm 리포지토리 추가

Prometheus와 Grafana 설치를 위한 Helm 차트 리포지토리를 추가합니다.

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

3. 네임스페이스 생성

Prometheus와 Grafana가 배포될 monitoring 네임스페이스를 생성합니다.

kubectl create namespace monitoring

4. Prometheus 및 Grafana 설치

Prometheus 및 Grafana를 설치합니다.

Prometheus 설치

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

Grafana 별도 설치

kube-prometheus-stack에 Grafana가 포함되어 있지만, 별도로 설치하여 최신 버전을 사용하거나 독립적으로 관리하고 싶다면 다음 명령어를 사용합니다.

helm install grafana grafana/grafana --namespace monitoring

5. NodePort로 Prometheus 및 Grafana 노출

Prometheus와 Grafana에 외부에서 접근할 수 있도록 NodePort로 서비스를 노출합니다.

Grafana 서비스 노출

kubectl patch svc grafana -n monitoring -p '{"spec": {"type": "NodePort"}}'

Prometheus 서비스 노출

kubectl patch svc prometheus-kube-prometheus-prometheus -n monitoring -p '{"spec": {"type": "NodePort"}}'

6. NodePort 확인

노출된 서비스를 확인하여 할당된 NodePort를 확인합니다.

 
kubectl get svc -n monitoring
출력예시:
 
NAME                                         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
grafana                                      NodePort    10.109.12.235    <none>        80:32376/TCP                 5m
prometheus-kube-prometheus-prometheus        NodePort    10.106.159.77    <none>        9090:32415/TCP               5m
...
 

여기서 Grafana의 NodePort는 32376, Prometheus의 NodePort는 32415입니다.

7. Grafana 관리자 비밀번호 확인

Grafana의 기본 관리자 계정 (admin)의 비밀번호를 확인합니다.

 
kubectl get secret --namespace monitoring grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo
 

출력된 비밀번호를 기록해둡니다.

8. Grafana 및 Prometheus 접속

외부에서 Grafana와 Prometheus에 접속하기 위해 노드의 외부 IP와 NodePort를 사용합니다.

  • Grafana 접속
http://<노드의 외부 IP>:<Grafana NodePort>
예: http://192.168.0.100:32376
  • Prometheus 접속
http://<노드의 외부 IP>:<Prometheus NodePort>
예: http://192.168.0.100:32415

 

브라우저에서 해당 주소로 접속하여 Grafana에 로그인합니다.

9. auw-ai 네임스페이스 모니터링 설정 (PodMonitor 사용)

auw-ai 네임스페이스의 모든 Pod를 자동으로 모니터링하기 위해 PodMonitor를 설정합니다.

PodMonitor 설정 파일 (podmonitor_auwai.yaml)

apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
  name: auw-ai-podmonitor
  namespace: monitoring
  labels:
    release: prometheus
spec:
  namespaceSelector:
    matchNames:
      - auw-ai
  selector:
    matchLabels:
      monitoring: enabled
  podMetricsEndpoints:
    - port: http
      path: /metrics
      interval: 15s
 

적용 방법

kubectl apply -f podmonitor_auwai.yaml
 

10. Pod에 라벨 추가

auw-ai 네임스페이스의 Deployment 템플릿에 monitoring: enabled 라벨을 추가하여 새로운 Pod가 생성될 때 자동으로 모니터링되도록 합니다.

Deployment 예시

apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-deployment
  namespace: auw-ai
spec:
  selector:
    matchLabels:
      app: example
  template:
    metadata:
      labels:
        app: example
        monitoring: enabled
    spec:
      containers:
      - name: example-container
        image: example-image
        ports:
        - containerPort: 8080
          name: http
 

이렇게 하면 해당 Deployment에서 생성된 Pod에 monitoring: enabled 라벨이 추가되고, PodMonitor에 의해 자동으로 메트릭이 수집됩니다.

11. Grafana에서 데이터 소스 추가

Grafana에서 Prometheus를 데이터 소스로 추가합니다.

  1. Grafana 로그인: 브라우저에서 Grafana에 접속하고, 앞서 확인한 관리자 비밀번호로 로그인합니다.
  2. 데이터 소스 추가:
    • 왼쪽 사이드바에서 **"설정" (톱니바퀴 아이콘) → "Data Sources"**를 클릭합니다.
    • "Add data source" 버튼을 클릭합니다.
    • **"Prometheus"**를 선택합니다.
    • URL에 Prometheus의 클러스터 내부 주소를 입력합니다:
       
    • "Save & Test" 버튼을 클릭하여 연결이 성공적으로 이루어졌는지 확인합니다.

12. 예시 대시보드 추가

Grafana에서 사전 정의된 대시보드를 임포트하여 모니터링을 시작할 수 있습니다.

1. 기본적인 Kubernetes 대시보드 추가

  • Kubernetes 클러스터 모니터링 (ID: 6417)
    1. 왼쪽 사이드바에서 **"+" → "Import"**를 클릭합니다.
    2. **"Import via grafana.com"**에서 Dashboard ID에 6417을 입력하고 **"Load"**를 클릭합니다.
    3. Prometheus 데이터 소스를 선택하고 **"Import"**를 클릭합니다.

2. auw-ai 네임스페이스 전용 대시보드 추가

  • 기존 대시보드에서 auw-ai 네임스페이스만 표시하도록 필터링하거나, 새로운 대시보드를 생성합니다.

대시보드 필터 설정 방법

  1. 대시보드 상단의 변수(Variables) 중 **"Namespace"**를 찾습니다.
  2. 드롭다운 메뉴에서 auw-ai 네임스페이스를 선택합니다.
  3. 대시보드의 모든 패널이 auw-ai 네임스페이스의 메트릭만 표시하도록 업데이트됩니다.

새로운 대시보드 생성 방법

  1. 왼쪽 사이드바에서 **"+" → "Dashboard"**를 클릭합니다.
  2. **"Add new panel"**을 클릭하여 새로운 패널을 추가합니다.
  3. "Query" 섹션에서 다음과 같이 auw-ai 네임스페이스의 메트릭을 조회하는 Prometheus 쿼리를 입력합니다.
    • CPU 사용량
      sum(rate(container_cpu_usage_seconds_total{namespace="auw-ai"}[5m])) by (pod)
    • 메모리 사용량
      sum(container_memory_usage_bytes{namespace="auw-ai"}) by (pod)
  4. 원하는 시각화 유형을 선택하고 **"Apply"**를 클릭합니다.
  5. 필요한 만큼 패널을 추가하여 대시보드를 구성합니다.
  6. 완료되면 "Save" 버튼을 클릭하여 대시보드를 저장합니다.

13. 요약

  1. Helm으로 Prometheus 및 Grafana 설치: Helm을 사용하여 Prometheus 및 Grafana를 설치합니다.
  2. NodePort로 서비스 노출: Prometheus와 Grafana 서비스를 NodePort로 노출하여 외부에서 접근할 수 있도록 설정합니다.
  3. Grafana 관리자 비밀번호 확인: Grafana의 관리자 비밀번호를 확인하여 로그인 준비를 합니다.
  4. PodMonitor 설정으로 auw-ai 네임스페이스 모니터링: PodMonitor를 설정하여 해당 네임스페이스의 모든 Pod를 자동으로 모니터링합니다.
  5. Deployment에 라벨 추가: Pod에 monitoring: enabled 라벨을 자동으로 추가하여 모니터링을 활성화합니다.
  6. Grafana에서 데이터 소스 및 대시보드 설정: Grafana에서 Prometheus를 데이터 소스로 추가하고, 예시 대시보드를 임포트하거나 생성하여 모니터링을 시작합니다.

 

이 과정을 따라 설정하면 Prometheus와 Grafana를 통해 Kubernetes 클러스터 및 auw-ai 네임스페이스의 리소스를 효과적으로 모니터링할 수 있습니다.

728x90
LIST