CLOUD/Monitoring

[Prometheus] Helm prometheus operator에 Nginx 추가하기

헐리 2022. 2. 18. 15:29

서버에 Nginx 돌리기

#설치
생략

#systemctl start nginx

 

Nginx Config 파일 생성후 nginx 재시작

# /etc/nginx/conf.d/nginx.conf

server {
    listen <nginx 호스트 IP>;
    server_name status.localhost;
    keepalive_timeout 0;

    access_log off;

    allow all;

    location /metrics {
        stub_status on;
    }
}

 

Nginx Prometheus Exporter 설정

Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-prometheus-exporter
  labels:
    app: nginx-prometheus-exporter
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-prometheus-exporter
  template:
    metadata:
      labels:
        app: nginx-prometheus-exporter
    spec:
      containers:
      - name: nginx-prometheus-exporter
        image: nginx/nginx-prometheus-exporter:0.4.2
        ports:
        - containerPort: 9113
        args:
        - --nginx.scrape-uri=http://<nginx 호스트 ip>/metrics

 

Service

apiVersion: v1
kind: Service
metadata:
  name: nginx-prometheus-exporter
spec:
  selector:
    app: nginx-prometheus-exporter
  ports:
    - protocol: TCP
      port: 9113
      targetPort: 9113

 

Prometheus Job 추가

https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/additional-scrape-config.md

 

GitHub - prometheus-operator/prometheus-operator: Prometheus Operator creates/configures/manages Prometheus clusters atop Kubern

Prometheus Operator creates/configures/manages Prometheus clusters atop Kubernetes - GitHub - prometheus-operator/prometheus-operator: Prometheus Operator creates/configures/manages Prometheus clus...

github.com

 

prometheus-additional.yaml  이름의 config 파일 만들기

  - job_name: 'nginx-prometheus-test'
    scrape_interval: 1m
    metrics_path: '/metrics'
    static_configs:
      - targets: ['<nginx 호스트 ip>:9113']

 

위의 config 파일로 secret 생성후 additionalScrapeConfigs.yaml 에 적용

kubectl create secret generic additional-scrape-configs --from-file=prometheus-additional.yaml --dry-run=clien
t -oyaml > additionalScrapeConfigs.yaml -n monitoring

NOTE: Use only one secret for ALL additional scrape configurations.

 

그 다음에 시크릿이 추가된 파일 apply하기

kubectl apply -f additional-scrape-configs.yaml -n monitoring

 

이 additionalScarapeConfigs를 promotheus.yaml 이라는 CRD에서 리퍼런싱 되는지 확인

apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  name: prometheus
  labels:
    prometheus: prometheus
spec:
  replicas: 2
  serviceAccountName: prometheus
  serviceMonitorSelector:
    matchLabels:
      team: frontend
  additionalScrapeConfigs:
    name: additional-scrape-configs
    key: prometheus-additional.yaml