본문 바로가기

Monitoring

[모니터링]프로메테우스+그라파나 모니터링 시스템 구축

실제 모니터링은 어떻게 진행되는지 알아보기 위해 모니터링 시스템을 구축해볼 것이다.

모니터링에서 중요한 것은 데이터를 질의할 수 있는 언어 PromQL에 대한 세부적인 학습보다 전체적으로 서비스를 모니터링할 때, 어떠한 메트릭을 봐야 하고, 그 메트릭을 어떻게 볼 수 있는지 아는 것이 더욱 중요하다.

따라서 이번 스프린트에서는 구체적인 모니터링 시스템 구축보다는 구축 후 SRE 모니터링의 주요 측정 항목인 "네가지 황금 시그널"을 살펴볼 것이다.

 


Prometheus 및 Grafana 설치

// 클론
git clone https://github.com/prometheus-operator/kube-prometheus.git

// 클론한 디렉토리에서 설치, kube-prometheus 배포
# STEP 1
kubectl create -f manifests/setup
# STEP 2
until kubectl get servicemonitors --all-namespaces ; do date; sleep 1; echo ""; done
# STEP 3
kubectl create -f manifests/

// port-forward를 통한 프로메테우스, 그라파나 노출
kubectl --namespace monitoring port-forward svc/prometheus-k8s 9090
kubectl --namespace monitoring port-forward svc/grafana 3000

nginx-ingress-controller 설치

// helm을 이용하여 설치
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update

// ingress-nginx 설치
helm install ingress-nginx ingress-nginx/ingress-nginx \
--set controller.metrics.enabled=true \
--set controller.metrics.serviceMonitor.enabled=true \
--set controller.metrics.serviceMonitor.additionalLabels.release="prometheus"

nginx-ingress-controller란?

공식문서를 빌리자면 다음과 같다.

An Ingress controller is a specialized load balancer for Kubernetes (and other containerized) environments.

Kubernetes Ingress controllers:

  • Accept traffic from outside the Kubernetes platform, and load balance it to pods (containers) running inside the platform
  • Can manage egress traffic within a cluster for services which need to communicate with other services outside of a cluster
  • Are configured using the Kubernetes API to deploy objects called “Ingress Resources”
  • Monitor the pods running in Kubernetes and automatically update the load‑balancing rules when pods are added or removed from a service

설치확인

// -A 다른 네임스페이스까지 모두 보인다.
kubectl get all -A

localhost:9090으로 접속하여 프로메테우스 타겟을 확인한다.

서비스에 인그레스 생성 후 붙이기

다음과 같은 아키텍처를 구성하기 위해 공식문서를 통해 간단한 인그레스를 작성한다.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: cozserver
  annotations:
spec:
  ingressClassName: nginx
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: cozserver
            port:
              number: 8055
// port-forward를 통해 인그레스를 접근가능하게 한다.
sudo kubectl port-forward service/ingress-nginx-controller 80

이제 localhost:80으로 서비스에 접속가능하다.

The Four Golden Signals

구글의 SRE 조직에서 제시한, 다른 건 안 봐도 되는데 4가지는 꼭 모니터링하도록 해라!라는 것을 해보겠다.

  • 트래픽, 오류(Traffic, Errors)
    • nginx_ingress_controller_requests
  • 대기 시간(Latency)
    • nginx_ingress_controller_request_duration_seconds_count
    • nginx_ingress_controller_request_duration_seconds_bucket
  • 포화 수준(Saturation)
    • node_cpu_seconds_total

nginx_ingress_controller_requests

어떤 파드에, 어떤 메서드로 어떤 엔드포인트에, 어떤 서비스, 호스트는 누구고, 인그레스, 상태 코드 등등 다양한 정보들을 얻을 수 있다. 트래픽을 볼 수 있고, 오류가 발생했는지 유무를 판단할 수 있다.

nginx_ingress_controller_request_duration_seconds_bucket

요청에 따른 latency가 어떤지도 그래프로 쉽게 알 수도 있다.

node_cpu_seconds_total

어떤 cpu의 포화도가 얼마인지도 알 수 있다.

 


이렇듯 프로메테우스를 통하여 어떤 애플리케이션과 연동하여 모니터링 모델을 구성해놓으면 프로메테우스가 pull 방식을 통해 받아온 다양한 메트릭을 확인할 수 있다. 이를 그라파나로 멋지게 볼 수도 있다.