새소식

기술/Application

[Kubernetes Monitoring] Grafana에 Prometheus Data Source 추가하기

  • -

안녕하세요. 오늘은 Grafana에 Prometheus Data Source를 추가하는 방법에 대해서 소개하고자 합니다.

 

Kubernetes에서 모니터링 시 Prometheus를 이용하여 Metric을 수집하고, 수집된 Metric을 Grafana에서 Graph를 보는 것이 보편적인 Kubernetes의 모니터링 방식입니다.

 

본 포스팅은 Prometheus와 Grafana가 다소 낯선 분들을 위해 조금씩 작성하는 포스트입니다.

 

 

Kubernetes 환경에서 Grafana와 Prometheus를 활용하려면 먼저 data source를 추가해야 합니다.

 

본 글은 Prometheus와 Grafana가 설치되었다는 가정하에 작성합니다.

 

순서는 다음과 같습니다.

 

1. Prometheus의 EndPoint(URL)을 확인하기

 

2. Grafana Configuration -> Add data source

 

3. Grafana Explore에서 Prometheus Metric 조회하기

 

 

 

먼저 Grafana의 EndPoint 확인을 위해 cli에서 kubectl get svc를 입력하여 Prometheus 서비스의 이름을 확인합니다.

저는 Prometheus가 monitoring 이라는 namespace를 사용하기 때문에 -n monitoring 이란 옵션을 주었습니다.

root@node1:~/grafana# kubectl get svc -n monitoring
NAME                                            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
argo-cd-argocd-application-controller           ClusterIP   10.233.12.11    <none>        8082/TCP                     2d7h
argo-cd-argocd-application-controller-metrics   ClusterIP   10.233.27.5     <none>        8082/TCP                     2d7h
argo-cd-argocd-dex-server                       ClusterIP   10.233.10.44    <none>        5556/TCP,5557/TCP,5558/TCP   2d7h
argo-cd-argocd-redis                            ClusterIP   10.233.4.143    <none>        6379/TCP                     2d7h
argo-cd-argocd-repo-server                      ClusterIP   10.233.27.6     <none>        8081/TCP                     2d7h
argo-cd-argocd-repo-server-metrics              ClusterIP   10.233.57.218   <none>        8084/TCP                     2d7h
argo-cd-argocd-server                           NodePort    10.233.21.79    <none>        80:32107/TCP,443:31295/TCP   2d7h
argo-cd-argocd-server-metrics                   ClusterIP   10.233.41.30    <none>        8083/TCP                     2d7h
grafana                                         NodePort    10.233.38.159   <none>        3000:30030/TCP               2d7h
grafana-image-renderer                          ClusterIP   10.233.22.105   <none>        8080/TCP                     2d7h
prometheus-kube-prometheus-operator             NodePort    10.233.1.104    <none>        8080:30080/TCP               2d7h
prometheus-kube-prometheus-prometheus           NodePort    10.233.48.255   <none>        9090:30090/TCP               2d7h
prometheus-kube-prometheus-prometheus-thanos    NodePort    10.233.62.209   <none>        10901:30901/TCP              2d7h
prometheus-kube-state-metrics                   ClusterIP   10.233.21.161   <none>        8080/TCP                     2d7h
prometheus-node-exporter                        ClusterIP   10.233.50.82    <none>        9100/TCP                     2d7h
prometheus-operated                             ClusterIP   None            <none>        9090/TCP                     2d7h

 

우리가 사용할 Data Source가 담긴 서비스는 prometheus-kube-prometheus-prometheus 입니다.

 

저는 Prometheus를 따로 사용하는 용도가 있어서 NodePort로 개방해 놓았는데, 보통은 ClusterIP로 연결해서 사용합니다.

 

NodePort로 오픈 했다고 가정했을 때 Node IP:Node Port로 URL을 사용할 수 있지만, 상황에 따라 바뀔 수 있기 때문에 Kubernetes에서 사용하는 CoreDNS를 이용하여 URL을 입력합니다.

 

 

Kubernetes는 서비스 생성 시 다음의 주소로 DNS**가 생성됩니다.

 

http://서비스 이름.네임스페이스.svc.cluster.local:port number 혹은 https://가 될 수 있습니다.

 

prometheus-kube-prometheus-prometheus를 사용하게 되면 아래와 같이 됩니다.

 

prometheus-kube-prometheus-prometheus.monitoring.svc.cluster.local:9090

 

이제 입력해보겠습니다.

 

 

Grafana의 좌측 메뉴에서 톱니바퀴(Configuration)을 선택-> Data Sources를 선택합니다. 

 

 

Add data source를 선택합니다.

 

Prometheus를 선택합니다.

 

Name에 Data Source의 이름을 지정한 뒤, URL 부분에 위에서 확인한 URL을 입력합니다.

이후 맨 아래에 있는 Save & Test 버튼을 클릭하여 정상적으로 추가가 되었는지 확인합니다.

 

정상적으로 추가되었을 경우 아래와 같이 표시 됩니다.

 

이제 추가한 Data Source를 Grafana Explore에서 확인해보겠습니다.

 

Grafana 좌측 메뉴의 나침반(Explore)를 선택->Query 입력창으로 갑니다.

 

Explore 옆은 Data Source를 선택할 수 있는 펼침 메뉴가 있고, Metrics 옆에는 Query를 입력할 수 있는 창이 있습니다.

 

컨테이너의 CPU Usage 상황을 보기위해 아래와 같은 Query를 입력해보겠습니다.

 

container_cpu_usage_seconds_total

정상적으로 Metric 값을 수집 중이라면, Explore에서 아래와 같이 Metric 값을 조회할 수 있습니다.

 

 

** Kubernetes DNS 관련해서는 아래 문서를 읽으시면 더 많은 정보를 알 수 있습니다.

<Kubernetes DNS-Based Service Discovery>

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.