Work/개발 노트

[k8s] kube-prometheus #1 - 설치

★용호★ 2020. 11. 18. 19:34

Amazon EKS를 사용하면서 초기에 Kubernetes 모니터링을 위해 Container Insights를 사용하였으나 500개 이상의 Custom Metric이 CloudWatch에 추가되면서 많은 비용이 발생하게 되었다. 비용 절감을 위해 Container Insights에서 현재는 Prometheus로 변경을 진행하였고, Prometheus를 간단하게 설치 할 수 있는 CoreOS의 kube-prometheus를 사용하였다. 

 

CoreOS에서 제공하는 kube-prometheus를 사용하면 Prometheus를 간단하게 설치할 수 있고, 설치 절차는 다음과 같다.

 

1. Git clone

git clone https://github.com/coreos/kube-prometheus.git

 

2. 의존성 문제로 인해 setup 모듈을 먼저 설치한다. 참고로 이 모듈들은 Kubernetes CRD로 제작되어 있다.

kubectl create -f manifests/setup

 

3. 곧바로 생성되지만 다음 명령으로 생성이 정상적으로 완료될 때까지 대기할 수 있다.

until kubectl get servicemonitors --all-namespaces ; do date; sleep 1; echo ""; done

 

4. rometheus 관련 Pod들을 생성하기 위한 CRD 생성이 완료되었다면 실제 Pod 및 Service와 같은 필요한 리소스들을 생성한다.

kubectl create -f manifests/

 

5. 미리 제작되어있는 스크립트에는 Prometheus 웹 UI에 접근할 수 있는 Service가 존재하지 않기 때문에 아래 매니페스트를 작성하여 Service를 생성한다.

apiVersion: v1
 kind: Service
 metadata:
   labels:
     app: prometheus
   name: prometheus
   namespace: monitoring
 spec:
   ports:
     - nodePort: 30803
       port: 9090
       protocol: TCP
       targetPort: 9090
   selector:
     app: prometheus
     prometheus: k8s
   type: NodePort

 

6. Grafana로 접속해서 Data Source를 추가한다. 추가 시 HTTP 경로에 Kubernetes 클러스터 내 Prometheus DNS 경로를 입력한다.

http://prometheus-k8s.monitoring.svc.cluster.local:9090

 

7. Grafana 대시보드 검색하여 이미 잘 만들어져 있는 Kuberntes Dashboard json 파일을 다운로드 받는다.

8. json파일 다운로드 후 Grafana에 Import 하고 나면 아래와 같이 대시보드 구성이 완료된다.