HUB
Sign inSign up

furkanustcayir/metrics-api

By furkanustcayir

•Updated about 1 month ago

Image
2

339

furkanustcayir/metrics-api repository overview

⁠metrics-api

Kubernetes cluster içinde çalışan, cluster'ın CPU, RAM ve pod kullanımını HTTP API olarak sunan read-only bir metrik servisi.

  • Anlık değerler her istekte doğrudan metrics-server (metrics.k8s.io) ve Kubernetes API'den okunur.
  • Son 1 saatlik geçmiş belirli aralıklarla veritabanına yazılır. 1 saatten eski kayıtlar otomatik silinir.
  • Cluster, node, namespace, workload (Deployment / StatefulSet / DaemonSet) ve pod seviyesinde kullanım bilgisi verir.

⁠Gereksinimler

  • Cluster'da metrics-server kurulu olmalı. kubectl top nodes çalışıyorsa hazırdır.
  • Pod, aşağıdaki read-only yetkilere sahip bir ServiceAccount ile çalışmalı.

⁠Environment değişkenleri

DeğişkenZorunluVarsayılanAçıklama
API_KEYSÖnerilir(boş)Virgülle ayrılmış API anahtarları. İstemciler X-API-Key header'ında bunlardan birini gönderir. Boş bırakılırsa kimlik doğrulama kapalıdır. Dışarı açılan kurulumlarda mutlaka doldurun.
DATABASE_URLHayırsqlite:////data/metrics.dbGeçmiş verinin yazılacağı veritabanı. PostgreSQL için: postgresql+psycopg://kullanici:sifre@host:5432/veritabani
METRICS_COLLECTION_INTERVAL_SECONDSHayır60Geçmiş için metrik toplama aralığı (saniye).
COLLECTOR_ENABLEDHayırtruefalse yapılırsa geçmiş toplanmaz, sadece anlık endpointler çalışır.
LOG_LEVELHayırINFODEBUG, INFO, WARNING, ERROR

Hassas değerleri (API_KEYS, DATABASE_URL) Kubernetes Secret üzerinden verin.

⁠Container bilgileri

  • Port: 8080
  • Health check: GET /health
  • Kullanıcı: UID 10001 (root değil), read-only root filesystem ile çalışabilir.
  • SQLite kullanılıyorsa /data yazılabilir olmalıdır (ör. emptyDir). Pod yeniden başlarsa son 1 saatlik geçmiş sıfırlanır; kalıcılık için PostgreSQL kullanın.
  • Collector API ile aynı process'te çalışır. Mükerrer kayıt olmaması için replicas: 1 kullanın.

⁠Gerekli RBAC (read-only)

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: metrics-api-reader
rules:
  - apiGroups: [""]
    resources: ["nodes", "pods", "namespaces"]
    verbs: ["get", "list"]
  - apiGroups: ["apps"]
    resources: ["deployments", "statefulsets", "daemonsets", "replicasets"]
    verbs: ["get", "list"]
  - apiGroups: ["metrics.k8s.io"]
    resources: ["nodes", "pods"]
    verbs: ["get", "list"]

Bu ClusterRole'ü bir ClusterRoleBinding ile pod'un ServiceAccount'una bağlayın.

⁠Örnek Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: metrics-api
  namespace: monitoring
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels: { app: metrics-api }
  template:
    metadata:
      labels: { app: metrics-api }
    spec:
      serviceAccountName: metrics-api
      containers:
        - name: api
          image: furkanustcayir/metrics-api:1.0.0
          ports:
            - containerPort: 8080
          env:
            - name: METRICS_COLLECTION_INTERVAL_SECONDS
              value: "60"
          envFrom:
            - secretRef:
                name: metrics-api-secret   # API_KEYS (+ opsiyonel DATABASE_URL)
          readinessProbe:
            httpGet: { path: /health, port: 8080 }
          volumeMounts:
            - { name: data, mountPath: /data }
      volumes:
        - name: data
          emptyDir: {}
kubectl -n monitoring create secret generic metrics-api-secret \
  --from-literal=API_KEYS="$(openssl rand -hex 32)"

⁠Endpointler

Anlık:

GET /api/v1/summary
GET /api/v1/cluster
GET /api/v1/nodes
GET /api/v1/pods?namespace={namespace}
GET /api/v1/namespaces
GET /api/v1/namespaces/{namespace}
GET /api/v1/namespaces/{namespace}/pods
GET /api/v1/namespaces/{namespace}/workloads
GET /api/v1/namespaces/{namespace}/statefulsets

Geçmiş (?minutes=1..60, varsayılan 60):

GET /api/v1/history/cluster
GET /api/v1/history/nodes?node={node}
GET /api/v1/history/namespaces/{namespace}
GET /api/v1/history/workloads/{namespace}/{workload}?kind=StatefulSet
GET /api/v1/history/pods/{namespace}/{pod}

Swagger UI: /docs

curl -H "X-API-Key: <anahtar>" https://metrics.example.com/api/v1/summary

⁠Hata kodları

KodAnlamı
401API anahtarı eksik veya hatalı
404Namespace bulunamadı
503metrics-server veya Kubernetes API'ye erişilemiyor

Tag summary

Content type

Image

Digest

sha256:439160315…

Size

73.6 MB

Last updated

about 1 month ago

docker pull furkanustcayir/metrics-api