K8s 采集 Elasticsearch 监控指标 - 梦想歌の网络日志
in operateCo-De with 0 comment

K8s 采集 Elasticsearch 监控指标 - 梦想歌の网络日志

in operateCo-De with 0 comment

K8s 采集 Elasticsearch 监控指标 - 梦想歌の网络日志

将 ES 的访问凭据保存到 Secret

为 es-exporter 创建 secret。

apiVersion: v1
kind: Secret
metadata:
  name: es-secret
  namespace: tools
type: Opaque
data:
  esURI: http://elastic:123456@127.0.0.1:9200

使用 StatefulSet 创建 es-exporter

apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app.kubernetes.io/name: es-exporter
    app.kubernetes.io/component: prometheus-exporter
  name: es-exporter
  namespace: tools
spec:
  podManagementPolicy: OrderedReady
  replicas: 0
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app.kubernetes.io/name: es-exporter
      app.kubernetes.io/component: prometheus-exporter
  serviceName: ""
  template:
    metadata:
      labels:
        app.kubernetes.io/name: es-exporter
        app.kubernetes.io/component: prometheus-exporter
    spec:
      containers:
      - command:
        - /bin/elasticsearch_exporter
        - --es.uri=http://127.0.0.1:9200
        - --es.all
        env:
        - name: TZ
          value: Asia/Shanghai
        image: quay.io/prometheuscommunity/elasticsearch-exporter:latest
        imagePullPolicy: IfNotPresent
        name: es-exporter
        ports:
        - containerPort: 9114
          name: metric-port
          protocol: TCP
        resources:
          limits:
            cpu: 250m
            memory: 512Mi
          requests:
            cpu: 250m
            memory: 512Mi
        securityContext:
          privileged: false
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      imagePullSecrets:
      - name: qcloudregistrykey
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
  updateStrategy:
    rollingUpdate:
      partition: 0
    type: RollingUpdate

确认 es-exporter 的端点访问正常:http://127.0.0.1:9114/metrics

创建 Service 绑定 es-exporter

apiVersion: v1
kind: Service
metadata:
  annotations:
    prometheus.io/es-exporter-prometheus-enabled: "true"
    prometheus.io/port: "9114"
  name: es-exporter
  namespace: tools
spec:
  clusterIP: none
  ports:
  - name: tcp-9114-9114
    port: 9114
    protocol: TCP
    targetPort: 9114
  selector:
    app.kubernetes.io/name: es-exporter
    app.kubernetes.io/component: prometheus-exporter
  sessionAffinity: None
  type: ClusterIP

加入 Prometheus 监控配置

基于 K8s 服务发现机制解析 es-exporter 的 endpoints,从而获得所有 Pod 的实例信息。

global:
  scrape_interval: 15s
  evaluation_interval: 15s

  - job_name: "es-exporter"
    scrape_interval: 5s
    kubernetes_sd_configs:
    - role: endpoints
    relabel_configs:
    - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_es_exporter_prometheus_enabled]
      action: keep
      regex: true
    - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scheme]
      action: replace
      target_label: __scheme__
      regex: (https?)
    - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]
      action: replace
      target_label: __metrics_path__
      regex: (.+)
    - source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]
      action: replace
      target_label: __address__
      regex: ([^:]+)(?::\d+)?;(\d+)
      replacement: $1:$2
    - action: labelmap
      regex: __meta_kubernetes_service_label_(.+)
    - source_labels: [__meta_kubernetes_namespace]
      action: replace
      target_label: namespace
    - source_labels: [__meta_kubernetes_service_name]
      action: replace
      target_label: service_name
Comments are closed.