1. 프로메테우스 스택 설치
이번 실습은 4장 Amazon EKS 원클릭 배포 환경에서 진행합니다.
그리고 새롭게 인프라를 배포하면 아래 기본 설정 명령을 입력 후 진행 바랍니다.
기본 설정 명령어
1.1. 프로메테우스 스택 설치
신규터미널 - 네임 스페이스 추가 및 모니터링
kubectl create ns monitoring
kubectl get ns
Bash
복사
# monitoring 네임 스페이스 추가 및 확인
watch kubectl get pod,svc,ingress,pv,pvc -n monitoring
Bash
복사
# [신규 터미널] pod, svc, ingress, pv, pvc 모니터링
helm repo 추가
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
Bash
복사
# helm chart repository 추가
프로메테우스 스택 설치
curl -s -O https://raw.githubusercontent.com/cloudneta/cnaeblab/master/_data/monitor-values.yaml
cat monitor-values.yaml | yh
Bash
복사
# values 파일 다운로드 및 확인
export MyDomain=$MyDomain CERT_ARN=$CERT_ARN
Bash
복사
# 환경 변수 선언
envsubst < monitor-values.yaml \
| helm install kube-prometheus-stack prometheus-community/kube-prometheus-stack \
--version 72.5.1 \
-f - --namespace monitoring
Bash
복사
# 프로메테우스 스택 배포
1.2. 프로메테우스 설치 확인
mornitoring 네임 스페이스에 helm list와 설치된 모든 자원 확인
helm list -n monitoring
Bash
복사
# monitoring 네임 스페이스에 helm list 확인
kubectl get-all -n monitoring
Bash
복사
# monitoring 네임 스페이스에 모든 자원 확인
StatefulSet, DaemonSet, Deployment 확인
kubectl get sts,ds,deploy -n monitoring
Bash
복사
# monitoring 네임 스페이스에 sts, ds, deploy 확인
kubectl describe sts -n monitoring
Bash
복사
# StatefulSet 상세 정보
kubectl describe ds -n monitoring
Bash
복사
# DaemonSet 상세 정보
crd, ServiceMonitors, TargetGroupBindings 확인
kubectl get crd | grep monitoring
Bash
복사
# monitoring 관련 crd 확인
kubectl get servicemonitors -n monitoring
Bash
복사
# monitoring 네임 스페이스에 ServiceMonitors 확인
kubectl get targetgroupbindings -n monitoring
Bash
복사
# monitoring 네임 스페이스에 TargetGroupBindings 확인
2. 프로메테우스 기본 사용
2.1. 모니터링 대상 확인
node-exporter 확인
kubectl describe ds -n monitoring | grep Port
Bash
복사
# node-exporter의 포트 정보
kubectl get svc,ep \
-n monitoring kube-prometheus-stack-prometheus-node-exporter
Bash
복사
# node-exporter의 서비스와 엔드포인트 확인
모니터링 대상별 /metrics 확인
ssh ec2-user@$N1 \
curl -s localhost:9100/metrics | tail
Bash
복사
# node-export - 노드에서 localhost로 9100 포트에 /metrics 접속
kubectl get servicemonitors -n monitoring
Bash
복사
# servicemonitors 확인
echo ">>>monitoring NS<<<"; kubectl get ep -n monitoring; \
echo ">>>kube-system NS<<<"; kubectl get ep -n kube-system; \
echo ">>>default NS<<<"; kubectl get ep
Bash
복사
# 네임 스페이스별 엔드포인트 확인
ssh ec2-user@$N1 \
curl -s localhost:10249/metrics | tail
Bash
복사
# kube-proxy - 노드에서 localhost로 10249 포트에 /metrics 접속
2.2. 프로메테우스 ingress 도메인 접속 및 확인
프로메테우스 ingress 정보 확인
kubectl get ingress -n monitoring kube-prometheus-stack-prometheus
Bash
복사
# 프로메테우스 ingress 정보 확인
kubectl describe ingress -n monitoring kube-prometheus-stack-prometheus
Bash
복사
# 프로메테우스 ingress 정보 확인 (상세)
echo -e "Prometheus Web URL = https://prometheus.$MyDomain"
Bash
복사
# 프로메테우스 ingress 도메인으로 웹 접속 URL 확인
프로메테우스 설정 메뉴
1.
Use local time : 출력 시간을 로컬 타임으로 변경
2.
Enable query history : PromQL 쿼리 히스토리 활성화
3.
Enable autocomplete : 자동 완성 기능 활성화
4.
Enable highlighting : 하이라이팅 기능 활성화
5.
Enable linter : 문법 오류 감지, 자동 코스 스타일 체크
프로메테우스 상단 메뉴
1.
쿼리(Query) : 프로메테우스 자체 검색 언어 PromQL을 이용하여 메트릭 정보를 조회 → 단순한 그래프 형태 조회
2.
경고(Alert) : 사전에 정의한 시스템 경고 정책(Prometheus Rules)에 대한 상황
3.
상태(Status) : 경고 메시지 정책(Rules), 모니터링 대상(Targets) 등 다양한 프로메테우스 설정 내역을 확인
프로메테우스 설정 확인
kubectl exec -it \
-n monitoring sts/prometheus-kube-prometheus-stack-prometheus \
-- head -n 50 /etc/prometheus/config_out/prometheus.env.yaml
Bash
복사
# 프로메테우스 설정 파일 확인
kubectl exec -it \
-n monitoring sts/prometheus-kube-prometheus-stack-prometheus \
-- cat /etc/prometheus/config_out/prometheus.env.yaml | grep job_name:
Bash
복사
# 프로메테우스 설정에서 job_name 확인
kubectl exec -it \
-n monitoring sts/prometheus-kube-prometheus-stack-prometheus \
-- cat /etc/prometheus/config_out/prometheus.env.yaml | grep node-exporter/ -A 9
Bash
복사
# 프로메테우스 설정에서 node-export 확인
kubectl get ep -n monitoring
kubectl get ep -n kube-system
Bash
복사
# 엔드포인트 확인
노드 그룹의 보안 그룹 수정
NGSGID=$(aws ec2 describe-security-groups \
--filters Name=group-name,Values='*ng1*' \
--query "SecurityGroups[*].[GroupId]" \
--output text)
Bash
복사
# 보안 그룹 ID 변수 지정
aws ec2 authorize-security-group-ingress \
--group-id $NGSGID \
--protocol '-1' \
--cidr 192.168.1.100/32
Bash
복사
# 작업용 인스턴스의 모든 트래픽 허용
PromQL 예시
1. 네임스페이스 및 파드 상태
count(kube_service_info) by (namespace)
Bash
복사
# [PromQL] 네임스페이스별 서비스 개수
count(kube_pod_info) by (namespace)
Bash
복사
# [PromQL] 네임스페이스별 파드 개수
kube_deployment_status_replicas_available{namespace="kube-system"}
Bash
복사
# [PromQL] kube-system 네임스페이스에 Deployment의 사용 가능한 파드 수
2. 노드 상태 점검
node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes
Bash
복사
# [PromQL] 노드 메모리 가용률
100 - (avg(rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
Bash
복사
# [PromQL] 전체 노드의 평균 CPU 사용률
3. 클러스터 요청량 확인
sum(rate(apiserver_request_total[5m])) by (verb)
Bash
복사
# [PromQL] API Server에 대한 요청량
4. 자원 사용량 - 메모리
sum(container_memory_working_set_bytes) by (pod)
Bash
복사
# [PromQL] 파드별 - 컨테이너가 사용 메모리
avg(process_resident_memory_bytes{job="apiserver"})
Bash
복사
# [PromQL] api-server 상주 메모리 평균
5. 자원 사용량 - CPU
1- avg(rate(node_cpu_seconds_total{mode="idle"}[1m]))
Bash
복사
# [PromQL] 전체 클러스터 노드의 CPU 사용량 평균
topk(5, sum(rate(container_cpu_usage_seconds_total{container!=""}[5m])) by (pod))
Bash
복사
# [PromQL] 파드별 CPU 사용량 Top 5
6. PVC 용량 확인
kubelet_volume_stats_used_bytes / kubelet_volume_stats_capacity_bytes
Bash
복사
# [PromQL] PVC 용량 확인
7. Prometheus 자체 메트릭
scrape_duration_seconds{job="node-exporter"}
Bash
복사
# [PromQL] node-export 스크랩 기간
8. 예측성 쿼리
predict_linear(node_filesystem_free_bytes{mountpoint="/"}[6h], 4 * 3600)
Bash
복사
# [PromQL] 4시간 후 디스크 공간 예측
3. 그라파나 대시보드 사용
3.1. 그라파나 정보 확인
그라파나 정보 확인
kubectl exec -it \
-n monitoring deploy/kube-prometheus-stack-grafana \
-- grafana cli \
--version
Bash
복사
# 그라파나 버전 확인
Note:
grafana-cli는 곧 제거될 예정이며, grafana cli를 권장합니다.
kubectl get ingress -n monitoring kube-prometheus-stack-grafana
Bash
복사
# 그라파나 ingress 확인
echo -e "Grafana Web URL = https://grafana.$MyDomain"
Bash
복사
# ingress 도메인으로 웹 접속 URL 확인 (기본 계정 - admin / prom-operator)
그라파나 메뉴 정보
1.
Home : 그라파나 기본 시작 페이지로 이동
2.
Bookmarks : 자주 사용하는 페이지나 대시보드를 북마크로 등록하고 접근
3.
Starred : 즐겨 찾기로 표시한 대시보드
4.
Dashboards : 전체 대시보드 목록 확인
5.
Explore : 등록된 데이터 소스에 쿼리를 실행하고, 즉각적인 시각화 표현
6.
Drilldown : 특정 대시보드나 패널에 하위 분석 링크를 생성하고 관리
7.
Alerting : 경고 정책을 설정하고 관리
8.
Connections : 연결할 데이터 소스를 추가하거나 편집
9.
Administration : 사용자, 팀, 조직, 플러그인, 보안 등을 설정
3.2. 그라파나 기본 대시보드 확인
그라파나 메뉴에서 Connections 확인
•
Connections → Data sources에 진입하면 데이터 소스에 자동으로 생성된 프로메테우스 확인
•
대상 데이터 소스를 클릭하면 상세 정보 출력
프로메테우스 접근 정보 확인
kubectl get svc,ep -n monitoring kube-prometheus-stack-prometheus
Bash
복사
# 프로메테우스의 서비스와 엔드포인트 주소 확인
테스트용 파드를 배포하고 확인
cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
name: netshoot-pod
spec:
containers:
- name: netshoot-pod
image: nicolaka/netshoot
command: ["tail"]
args: ["-f", "/dev/null"]
terminationGracePeriodSeconds: 0
EOF
Bash
복사
# 테스트용 파드 배포
kubectl get pod netshoot-pod
Bash
복사
# 생성된 파드 확인
테스트용 파드에서 프로메테우스 주소 확인
kubectl exec -it netshoot-pod \
-- nslookup kube-prometheus-stack-prometheus.monitoring
Bash
복사
# 프로메테우스 접속 주소로 nslookup 확인
kubectl exec -it netshoot-pod \
-- curl -s kube-prometheus-stack-prometheus.monitoring:9090/query -v ; echo
Bash
복사
# 프로메테우스 접속 주소로 query 경로 접근
테스트용 파드 삭제
kubectl delete pod netshoot-pod
Bash
복사
# 실습 자원 삭제
기본 대시보드 사용
•
Dashboards 메뉴에 진입해서 general 폴더 접근
•
Kubernetes / Networking / Cluster 선택
•
Kubernetes / Persistent Volumes 선택
•
Node Exporter / Nodes 선택
3.3. 그라파나 대시보드 사용
대시보드 유형
•
기본 대시보드 : 프로메테우스 스택을 통해 기본적으로 설치된 대시보드
•
공식 대시보드 : 링크
대시보드 추가 방법
•
Dashboard → New → Import → ID 입력 → Load → Prometheus → Import
추천 공식 대시보드
•
[Kubernetes / Views / Global] : 15757
•
[Kubernetes All-in-one Cluster Monitoring KR] : 23000
•
[Node Exporter Full] : 22152
•
[Node Exporter for Prometheus Dashboard based on 11074] : 15172
•
[kube-state-metrics-v2] : 13332
•
[Amazon EKS - AWS CNI Metrics] : 16032
4. 애플리케이션 모니터링 설정 및 대시보드 추가
4.1. NGINX 웹 서버 배포
helm repository 추가
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
Bash
복사
# bitnami helm repo 추가
NGINX 파라미터 파일 생성
cat <<EOT > nginx-values.yaml
service:
type: NodePort
ingress:
enabled: true
ingressClassName: alb
hostname: nginx.$MyDomain
path: /*
annotations:
alb.ingress.kubernetes.io/scheme: internet-facing
alb.ingress.kubernetes.io/target-type: ip
alb.ingress.kubernetes.io/listen-ports: '[{"HTTPS":443}, {"HTTP":80}]'
alb.ingress.kubernetes.io/certificate-arn: $CERT_ARN
alb.ingress.kubernetes.io/success-codes: 200-399
alb.ingress.kubernetes.io/load-balancer-name: $CLUSTER_NAME-ingress-alb
alb.ingress.kubernetes.io/group.name: study
alb.ingress.kubernetes.io/ssl-redirect: '443'
metrics:
enabled: true
service:
port: 9113
serviceMonitor:
enabled: true
namespace: monitoring
interval: 10s
EOT
cat nginx-values.yaml | yh
Bash
복사
# nginx values 파일 생성 및 확인
NGINX 배포 및 확인
helm install nginx bitnami/nginx \
--version 20.0.0 \
-f nginx-values.yaml
Bash
복사
# nginx 배포
kubectl get pod
kubectl get svc,ep nginx
Bash
복사
# 파드, 서비스, 엔드포인트 확인
4.2. 프로메테우스 서비스 모니터 확인
서비스 모니터 확인
kubectl get servicemonitor -n monitoring
Bash
복사
# 서비스 모니터 확인
kubectl get servicemonitor nginx -n monitoring -o json | jq
Bash
복사
# nginx 서비스 모니터 확인
kubectl get pod
kubectl get svc,ep nginx
Bash
복사
# 파드, 서비스, 엔드포인트 확인
NGINX 메트릭 확인
NGINXIP=$(kubectl get pod \
-l app.kubernetes.io/instance=nginx \
-o jsonpath={.items[0].status.podIP}); echo $NGINXIP
Bash
복사
# NGINX IP 변수 선언
curl -s http://$NGINXIP:9113/metrics
Bash
복사
# NGINX IP로 메트릭 확인
curl -s http://$NGINXIP:9113/metrics | grep ^nginx_connections_active
Bash
복사
# NGINX IP로 메트릭 확인 (필터링)
NGINX 접속
curl -s https://nginx.$MyDomain | sed -n '4p'
Bash
복사
# NGINX 1회 접속
while true; \
do \
curl -s https://nginx.$MyDomain -I | head -n 1; date; sleep 1; done
Bash
복사
# NGINX 1초 간격 접속
curl -s http://$NGINXIP:9113/metrics | grep ^nginx_connections_active
Bash
복사
# NGINX 메트릭 확인 - nginx_connections_active
4.3. NGINX 모니터링 대시보드 추가
NGINX 모니터링 대시보드 추가
•
NGINX 모니터링 대시보드 : 12708
NGINX 부하 발생
yum install -y httpd
Bash
복사
# httpd 설치
ab -c 100 -n 1000 https://nginx.$MyDomain/
Bash
복사
# 100회씩 총 1000회 접속 수행
5. 실습 환경 삭제
4장 전체 실습이 종료되어 Amazon EKS 원클릭 배포를 삭제해 모든 실습 환경을 삭제합니다.
실습 자원 삭제
helm uninstall -n kube-system kube-ops-view
helm uninstall nginx
helm uninstall -n monitoring kube-prometheus-stack
Bash
복사
# helm chart 삭제
watch kubectl get pod,svc,ingress,pv,pvc -n monitoring
kubectl delete pvc prometheus-kube-prometheus-stack-prometheus-db-prometheus-kube-prometheus-stack-prometheus-0 -n monitoring
Bash
복사
# 프로메테우스 PVC 삭제 (프로메테우스 스택에 의해 나머지 삭제된 후)
eksctl delete cluster --name $CLUSTER_NAME \
&& aws cloudformation delete-stack --stack-name $CLUSTER_NAME
Bash
복사
# Amazon EKS 원클릭 배포 삭제
Warning:
Amazon EKS 원클릭 배포의 삭제는 약 15분 정도 소요됩니다.
삭제가 완료될 때 까지 SSH 연결 세션을 유지합니다.
Warning:
만약에 CloudFormation 스택이 삭제되지 않는다면 수동으로 VPC(myeks-VPC )를 삭제 후 CloudFormation 스택을 다시 삭제해 주세요.
여기까지 4장의 모든 실습을 마칩니다.
수고하셨습니다 :)