Kubernetes/Kubernetes Best Practices

[Scenario Playbook - 심화편 | Medium Level #12] Kubernetes Metrics Server와 Prometheus를 이용한 모니터링 구축

ygtoken 2025. 3. 17. 12:01
728x90

 

쿠버네티스 클러스터에서는 리소스 사용량을 모니터링하고, 이상 징후를 감지하는 것이 중요합니다.

이를 위해 Metrics Server와 Prometheus를 활용하여 실시간 리소스 모니터링을 구축하는 방법을 다룹니다.

 


📌 글에서 다루는 상황들

 

1. Metrics Server를 설치하여 kubectl top 명령어로 리소스 사용량 확인

2. Prometheus를 설치하여 상세한 메트릭 수집 및 시각화

3. kubectl을 활용한 모니터링 데이터 조회 및 PromQL 활용법

 

각 문제를 실무에서 바로 활용할 수 있도록 Manifest 템플릿과 예상 결과 값을 제공합니다.

 


1️⃣ Metrics Server를 설치하여 kubectl top 명령어로 리소스 사용량 확인

 

❓ 문제 상황

 

운영팀에서 Pod 및 노드의 CPU, 메모리 사용량을 실시간으로 확인할 수 있도록 설정해야 합니다.

이를 위해 Metrics Server를 설치하고, kubectl top 명령어를 활용하여 모니터링을 수행해야 합니다.

모니터링 대상: Pod 및 Node의 CPU, 메모리 사용량

kubectl top 명령어를 사용하여 실시간 모니터링

 

✅ 어떻게 해결할 수 있을까요?

 


🛠️ 해결 방법

 

1. Metrics Server를 설치하여 리소스 사용량을 수집합니다.

 

2. kubectl top 명령어를 활용하여 Pod 및 Node의 사용량을 조회합니다.

 


✅ Metrics Server 설치 방법

 

🔹 Metrics Server 배포 (Helm 사용)

helm repo add metrics-server https://kubernetes-sigs.github.io/metrics-server/
helm repo update
helm install metrics-server metrics-server/metrics-server

 

🔹 설치 확인

kubectl get deployment -n kube-system metrics-server

 

💡 예상 출력 값

NAME             READY   UP-TO-DATE   AVAILABLE   AGE
metrics-server   1/1     1            1           5m

Metrics Server가 정상적으로 배포됨

 


📌 적용 후 예상 결과 값

 

1. Pod 리소스 사용량 확인

kubectl top pod

 

💡 예상 출력 값

NAME          CPU(cores)   MEMORY(bytes)   
web-app-1     50m          200Mi
web-app-2     30m          150Mi

 

2. Node 리소스 사용량 확인

kubectl top node

 

💡 예상 출력 값

NAME          CPU(cores)   MEMORY(bytes)   
node-1        250m         1Gi
node-2        180m         900Mi

kubectl top 명령어를 사용하여 리소스 사용량을 실시간으로 확인 가능

 


2️⃣ Prometheus를 설치하여 상세한 메트릭 수집 및 시각화

 

❓ 문제 상황

 

운영팀에서 쿠버네티스 리소스에 대한 장기적인 모니터링 데이터를 저장하고, 시각화할 수 있도록 Prometheus를 구축해야 합니다.

이를 위해 Prometheus를 Helm Chart를 이용해 설치하고, Grafana를 연동해야 합니다.

Prometheus를 이용한 Pod 및 Node의 메트릭 수집

Grafana를 활용하여 시각적으로 모니터링

 

✅ 어떻게 해결할 수 있을까요?

 


🛠️ 해결 방법

 

1. Prometheus 및 Grafana를 Helm Chart를 통해 설치합니다.

 

2. Prometheus에서 메트릭을 수집하고, Grafana에서 시각화합니다.

 


✅ Prometheus 및 Grafana 설치 방법

 

🔹 Helm을 사용한 Prometheus 설치

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

 

🔹 설치된 Prometheus 리소스 확인

kubectl get pods -n default | grep prometheus

 

💡 예상 출력 값

prometheus-0                      2/2     Running   0     2m
alertmanager-0                    2/2     Running   0     2m
grafana-0                          2/2     Running   0     2m

Prometheus 및 Grafana가 정상적으로 설치됨

 


 

📌 적용 후 예상 결과 값

 

1. Prometheus 웹 UI에 접근 (포트 포워딩 사용)

kubectl port-forward svc/prometheus-kube-prometheus-prometheus 9090

웹 브라우저에서 http://localhost:9090에 접속하면 Prometheus UI를 확인할 수 있음.

 

2. Grafana 웹 UI에 접근

kubectl port-forward svc/prometheus-grafana 3000

웹 브라우저에서 http://localhost:3000에 접속 후, 기본 사용자(admin)와 비밀번호(prom-operator)로 로그인 가능.

 

Prometheus에서 메트릭을 수집하고, Grafana에서 시각적으로 모니터링 가능

 


3️⃣ kubectl을 활용한 모니터링 데이터 조회 및 PromQL 활용법

 

❓ 문제 상황

 

운영팀에서 현재 클러스터에서 CPU 및 메모리 사용량이 높은 Pod을 빠르게 찾고, 메트릭 데이터를 분석해야 합니다.

kubectl 및 PromQL을 활용하여 현재 리소스 사용량과 이상 징후를 모니터링해야 합니다.

 

✅ 어떻게 해결할 수 있을까요?

 


🛠️ 해결 방법

 

1. kubectl top 명령어를 사용하여 현재 가장 높은 리소스를 사용 중인 Pod을 찾습니다.

 

2. PromQL을 활용하여 특정 시간 동안의 리소스 사용량을 조회합니다.

 


✅ PromQL을 활용한 리소스 모니터링

 

🔹 CPU 사용량이 가장 높은 Pod 조회

rate(container_cpu_usage_seconds_total[5m])

 

💡 예상 출력 값 (Prometheus UI)

container_cpu_usage_seconds_total{container="web-app"}  0.002
container_cpu_usage_seconds_total{container="db"}  0.003

 

🔹 특정 Pod의 메모리 사용량 조회

container_memory_usage_bytes{pod="web-app-1"}

 

💡 예상 출력 값

container_memory_usage_bytes{pod="web-app-1"}  524288000

kubectl 및 PromQL을 활용하여 현재 클러스터 상태를 실시간으로 모니터링 가능

 

728x90