본문 바로가기

모니터링 도구/Grafana

EKS + 그라파나 + 프로메테우스 구성하기

이번 글에서는 헬름 차트를 사용해 그라파나, 프로메테우스를 설치하는 방법에 대해 알아보자.

# 헬름 설치

2021.10.24 - [Kubernetes & Docker/Helm] - Helm이란?

 

Helm이란?

Helm 차트를 활용한 애플리케이션 패키지 배포 쿠버네티스 애플리케이션 관리를 지원하는 도구 Helm 이란? Helm 은 쿠버네티스 package managing tool 이다. node.js 의 npm 과 비슷한 형태로 쿠버네티스

may9noy.tistory.com

# 헬름 레파지토리 추가

클라우드 셸을 사용해 헬름 레파지토리를 추가하자. 헬름 v3를 사용한다.

sudo snap install helm --classic
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

# 그라파나와 프로메테우스 배포

헬름 배포를 위해 그라파나와 프로메테우스의 values.yaml을 구성할 디렉토리를 하나 구성한다.

mkdir grafana_prometheus 
cd grafana_prometheus

다음의 명령을 실행해 values-prometheus.yaml 파일을 생성한다.
pv로 스토리지를 구성하고 15일간 데이터를 보존하도록 했다.

cat <<EOF > values-prometheus.yaml
server:
  enabled: true

  persistentVolume:
    enabled: true
    accessModes:
      - ReadWriteOnce
    mountPath: /data
    size: 100Gi
  replicaCount: 1

  ## Prometheus data retention period (default if not specified is 15 days)
  ##
  retention: "15d"
EOF

다음 명령을 실행해 values-grafana.yaml를 생성한다. pvc와 스토리지를 구성하여 설정정보를 유지할 수 있도록 구성했다.
아이디 패스워드는 admin//test1234로 설정하였고, 서비스가 생성될 때 접속하기 쉽도록 로드 밸런서로 구성했다.
실무에서는 모니터링 서비스가 외부로 노출되지 않도록 신경을 쓰며 작업을 해야한다.

cat << EOF > values-grafana.yaml
replicas: 1

service:
  type: LoadBalancer #Local환경 이라면, NodePort로 설정한다.

persistence:
  type: pvc
  enabled: true
  # storageClassName: default
  accessModes:
    - ReadWriteOnce
  size: 10Gi
  # annotations: {}
  finalizers:
    - kubernetes.io/pvc-protection

# Administrator credentials when not using an existing secret (see below)
adminUser: admin
adminPassword: test1234
EOF

헬름으로 values 파일들을 사용해 배포를 시작한다.

kubectl create ns prometheus
helm install prometheus prometheus-community/prometheus -f values-prometheus.yaml -n prometheus
helm install grafana grafana/grafana -f values-grafana.yaml -n prometheus

배포가 정상적으로 이루어 졌는지 확인해보자. 프로메테우스로 배포한 포드와 서비스가 잘 구성되었는지 확인한다.

kubectl get pod,svc -n prometheus
NAME                                    TYPE           CLUSTER-IP       EXTERNAL-IP                                                                    PORT(S)        AGE
service/grafana                         LoadBalancer   10.100.86.77     ac2bd09d268dc40e4b5ed72cd35ca9e4-1698522594.ap-northeast-2.elb.amazonaws.com   80:30507/TCP   55m
service/prometheus-alertmanager         ClusterIP      10.100.156.160   <none>                                                                         80/TCP         55m
service/prometheus-kube-state-metrics   ClusterIP      10.100.4.241     <none>                                                                         8080/TCP       55m
service/prometheus-node-exporter        ClusterIP      None             <none>                                                                         9100/TCP       55m
service/prometheus-pushgateway          ClusterIP      10.100.24.194    <none>                                                                         9091/TCP       55m
service/prometheus-server               ClusterIP      10.100.174.201   <none>                                                                         80/TCP         55m
ec2-user:~/environment/grafana_prometheus $

EXTERNAL-IP로 grafana 접속을 한다. 아이디와 패스워드는 admin//test1234다.
여기서는 http://ac2bd09d268dc40e4b5ed72cd35ca9e4-1698522594.ap-northeast-2.elb.amazonaws.com:80 이 될 것이다.
설정된 ID와 PASSWORD를 입력하고, 그라파나 서비스로 이동한다.

# 혹시 NodePort 로 생성을 한경우는 아래와 같을 것이다.

NodePort로 접근하기 위한 방법

kubectl edit services <service_name> -n <namespace>

kubectl edit services grafana -n prometheus

현재 설정된 타입을 LoadBalancer에서 NodePort로 변경하자.

저장 후 빠져 나오면 바로 적용이 된다.

NodePort 를 이용해 접근하자.

kubectl get nodes -o wide 명령을 활용해 node의 ip를 확인 후 해당 포트로 접근한다.
접속 URL : 해당 Node의 ip : 31910 이 될것이다.

k8s-master-node@k8smasternode-Virtual-Machine:~/grafana_prometheus$ kubectl get pod,svc -n prometheus
NAME                                                 READY   STATUS    RESTARTS   AGE
pod/grafana-74ccb44c87-l994t                         0/1     Pending   0          9s
pod/prometheus-alertmanager-688fdfff59-flt6n         0/2     Pending   0          23s
pod/prometheus-kube-state-metrics-58c5cd6ddb-ln492   0/1     Running   0          23s
pod/prometheus-node-exporter-hwfc5                   1/1     Running   0          23s
pod/prometheus-node-exporter-v4g2j                   1/1     Running   0          23s
pod/prometheus-pushgateway-7c8564df88-827vg          0/1     Running   0          23s
pod/prometheus-server-56dc7979b8-642kx               0/2     Pending   0          23s

NAME                                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/grafana                         NodePort    10.96.59.101     <none>        80:31910/TCP   9s
service/prometheus-alertmanager         ClusterIP   10.103.200.155   <none>        80/TCP         23s
service/prometheus-kube-state-metrics   ClusterIP   10.105.40.107    <none>        8080/TCP       23s
service/prometheus-node-exporter        ClusterIP   None             <none>        9100/TCP       23s
service/prometheus-pushgateway          ClusterIP   10.108.217.246   <none>        9091/TCP       23s
service/prometheus-server               ClusterIP   10.108.35.144    <none>        80/TCP         23s

프로메테우스 데이터를 그라파나로 가져오기 > Configuration의 Data sources로 접근한다.

데이터 추가를 위해 Add data source를 클릭하자.

데이터 소스 타입을 Prometheus로 선택한다.

HTTP 서버에 대한 URL 정보에 도메인 정보를 입력하자. 프로메테우스의 도메인 이름은 서비스를 확인하면 된다.

앞에서 확인한 정보에서 service/prometheus-server 이름을 확인할 수 있다.

여기서는 별도의 인증정보가 필요 없으므로 바로 save & test 버튼을 클릭한다.

- 정상적으로 연동이 되면 아래와 같이 데이터 소스가 정상적으로 연동되었다고 알림창이 뜬다.

대시보드 구성하기

대시보드를 구성하기 위해 +버튼 (Create)의 Import로 들어간다. Import는 외부에 사용자들이 미리구성해놓은 좋은 다양한 대시보드를 간단히 다운로드 받아서 설정할 수 있다.

​그라파나 사이트에 다양한 모양을 가진 대시보드들이 올라와 있다. 원하는 것을 찾고 ID를 가져와서 입력하면 된다.

https://grafana.com/grafana/dashboards/315

Kubernetes cluster monitoring (via Prometheus) dashboard for Grafana

Monitors Kubernetes cluster using Prometheus. Shows overall cluster CPU / Memory / Filesystem usage as well as individual pod, containers, systemd services statistics. Uses cAdvisor metrics only.

grafana.com

복사한 ID를 입력하고 Load 버튼을 누르면 바로 구성이 시작된다.

남은 정보를 마저 설정하고 Import를 누른다.

구성을 완료하면 다음과 같은 쿠버네티스 자원 사용 현황을 대시보드로 파악할 수 있다.

​한국 사람이 만든 한국 스타일의 대시보드도 있다. 13770번 대시보드를 동일한 방법으로 업로드해 활용해보자.

임포트한 화면은 다음과 같다. 오픈소스이고, 다양한 리소스를 제공하고 관리하기도 쉽고 무엇보다 설치도 쉽다.
좋은 오픈소스 인거 같아 나중에 꼭 제대로 활용해 보고싶다. 그라파나 + 프로메테우스!

- 여기까지 EKS에서 그라파나와 프로메테우스를 연동하여 모니터링 하는 방법에 대해 알아 보았다.