Cloud/k8s-CKA

[CKA] 15. k8s Monitoring (METRIC SERVER) and log

jinkwon.kim 2023. 3. 19. 01:30
728x90
반응형

개요

k8s의 cluster를 monitoring하는 방법과 Pod의 Log를 보는 방법을 알아보게습니다.

Monitoring 제품 종류

많은 Monitoring 제품이 존재합니다. 

그러나 k8s에서 제공하는 Metrics Server만 알아볼 것입니다. 추후에 다른  Monitoring이 필요하면 그때 다시 다루겠습니다.

Metrics Server

정의

METRIC SERVER란 k8s cluster의 metric 정보를 수집할 수 있게 제공해 주는 API입니다.

여기서 metric이란 지표라는 뜻 하며 예를 들어 보면 CPU 사용량, 메모리 사용량등을 말합니다.

https://kubernetes.io/ko/docs/tasks/debug/debug-cluster/resource-metrics-pipeline/

사용이유

HorizontalPodAutoscaler(HPA) 및 VerticalPodAutoscaler(VPA)를 사용하기 위해서 필요합니다.

HPA와 VPA는  사용자의 요구 사항을 만족할 수 있도록 workdload replica와 Resource를 조정하는 데에 METRIC API의 데이터를 이용합니다.

METRIC SERVER 설치

설치 사이트

https://github.com/kubernetes-sigs/metrics-server  아시트에 가보면 https://github.com/kubernetes-sigs/metrics-server#installation 에 metric server를 설치할 수 있게 해 주는 yaml 파일을 제공합니다.

 

GitHub - kubernetes-sigs/metrics-server: Scalable and efficient source of container resource metrics for Kubernetes built-in aut

Scalable and efficient source of container resource metrics for Kubernetes built-in autoscaling pipelines. - GitHub - kubernetes-sigs/metrics-server: Scalable and efficient source of container reso...

github.com

yaml 파일을 이용한 설치

아래의 yaml 파일을 적용하게 되면 metric server를 사용할 수 있습니다. 

 

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

 

그러나 정상 동작은 하지 않을 것입니다. kubelet describe로 metric-server pod를 보면 아래와 같은 error를 마주치게 됩니다. 

Readiness probe failed: HTTP probe failed with statuscode: 500

 

임시 해결 방법

설치된 Deployment / metrics-server에서 아래와 같이 --kubelet-insecure-tls 라는 커맨드를 추가 후 저장 해주시면 됩니다. 

완벽한 해결 방법은 TLS 인증서를 사용해야 합니다. 해당 방법은 Cluster에서 TLS 인증서 관리 방법을 학습한 후 다루겠습니다. 

 

#kubectl edit deployment metrics-server -n kube-system

 38       containers:
 39       - args:
 40         - --cert-dir=/tmp
 41         - --secure-port=4443
 42         - --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
 43         - --kubelet-use-node-status-port
 44         - --metric-resolution=15s
 45         - --kubelet-insecure-tls

확인

Node 정보와 Pod 정보가 잘 나오는 것을 확인할 수 있습니다.

METRIC SERVER동작 구조

METRIC SERVER의 동작 구조는 아래와 같습니다.

핵심

kubelet에 포함되어 있는 cAdivisor를 통하여 정보를 수집 후 API Server를 이용하여 공유하는 것이 METRIC SERVER의 주요 역할입니다.

동작 방식

각 Node에서 Container Resource 사용량 데이터 수집

cAdvisor는 실행 중인 컨테이너의 CPU, Memory, 네트워크 및 파일시스템 사용량과 같은 리소스 사용량 데이터를 실시간으로 수집합니다.

노드 및 파드 메트릭 수집

kubelet은 cAdvisor로부터 Node와 Pod Level의 Metric을 수집하고, 이러한 Metric을 k8s API Server와 다른 구성 요소(Horizontal Pod Autoscaler 및 Vertical Pod Autoscaler)와 공유합니다.

노드 리소스 사용량을 기반으로 스케줄링 및 용량 관리

k8s는 cAdvisor가 제공하는 Node Resource 사용량 정보를 사용하여 파드를 적절한 Node에 스케줄링하고, Node의 용량을 관리합니다.

Pod의 Container Log 확인

Pod의 container에서 발생한는 Log를 보는 방법을 알아보곘습니다. 

명령어 

#kubectl logs -f 여기서 -f는 계속해서 보겠다는 뜻입니다.

container가 1개만 있는 경우

#kubectl logs -f {pod 의 name}

container가 2개이상 있는 경우 

#kubectl logs -f {pod 의 name} {container name}

정리

1. k8s에서 node에서 사용되고 있는 resource를 확인하기 위해서는 metric server를 사용해야 합니다.

2. pod 내의 container log를 보기 위해서는 kubectl logs 를 사용하면 됩니다.

Next Post

[CKA] 16. k8s Rolling update and Rollback

 

728x90
반응형

'Cloud > k8s-CKA' 카테고리의 다른 글

[CKA] 17. command and argument  (0) 2023.03.25
[CKA] 16. k8s Rolling update and Rollback  (2) 2023.03.23
[CKA] 14. scheduler profile 설정  (0) 2023.03.07
[CKA] 13. multiple scheduler  (0) 2023.03.06
[CKA] 12. Static Pod  (0) 2023.03.05