自学内容网 自学内容网

云原生化 - 监控(简约版)

要在程序中暴露指标,并符合 Prometheus 和 Kubernetes 的规范,可以按照以下步骤进行:

1. 选择合适的库

根据你的编程语言选择适合的 Prometheus 客户端库。例如:

  • Go: github.com/prometheus/client_golang
  • Java: io.prometheus:simpleclient
  • Python: prometheus_client
  • Node.js: prom-client

2. 定义和创建指标

在代码中定义需要监控的指标。常用的指标类型包括:

  • Counter: 计数器,只增不减。
  • Gauge: 测量某个值,可以增减。
  • Histogram: 用于记录分布的样本。
  • Summary: 用于计算和记录一组值的摘要统计信息。

示例(以 Go 为例):

package main

import (
    "net/http"
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
)

var (
    requestCount = prometheus.NewCounterVec(
        prometheus.CounterOpts{
            Name: "http_requests_total",
            Help: "Total number of HTTP requests",
        },
        []string{"method", "endpoint"},
    )
)

func init() {
    prometheus.MustRegister(requestCount)
}

func handler(w http.ResponseWriter, r *http.Request) {
    requestCount.WithLabelValues(r.Method, r.URL.Path).Inc()
    w.Write([]byte("Hello, World!"))
}

func main() {
    http.HandleFunc("/", handler)
    // 指定 /metric 路径专门用于指标的暴露
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe(":8080", nil)
}

3. 暴露指标

在你的应用程序中,通常会通过一个 HTTP 端点(如 /metrics)来暴露指标。确保这个端点可以被 Prometheus 访问。

4. 在 Kubernetes 中部署应用

创建一个 Kubernetes 部署文件,确保你的应用程序能够正确启动并暴露 /metrics 端点。

示例部署 YAML 文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-app-image:latest
        ports:
        - containerPort: 8080

5. 配置 Prometheus

在 Kubernetes 中配置 Prometheus,使其能够发现并抓取你的指标。你可以使用 ConfigMap 来配置 Prometheus。

一般部署 Prometheus 为 Kubernetes 运维人员的工作,开发人员不需要关注。但开发人员需要学习并配置指标,在 Kubernetes 中通常通过 ServiceMonitor 配置来实现。

示例 Prometheus 配置:

scrape_configs:
  - job_name: 'my-app'
    kubernetes_sd_configs:
      - role: pod
    relabel_configs:
      - source_labels: [__meta_kubernetes_pod_label_app]
        action: keep
        regex: my-app
      - action: labelmap
        regex: __meta_kubernetes_pod_label_(.+)

示例 ServiceMonitor 配置:

apiVersion: v1
kind: Service
metadata:
  name: my-app
  labels:
    app: my-app
spec:
  ports:
    - port: 8080
      targetPort: 8080
  selector:
    app: my-app
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: my-app-monitor
  labels:
    app: my-app
spec:
  selector:
    matchLabels:
      app: my-app
  namespaceSelector:
    matchNames:
      - default
  endpoints:
    - port: http  # 假设你的服务配置中端口名称为 "http"
      path: /metrics
      interval: 30s

6. 部署和监控

一旦你完成了上述步骤,部署应用和 Prometheus。你可以通过 Prometheus 的 UI 来查看和查询你的指标。

7. 调整和优化

根据收集到的指标数据,监控应用的性能,并根据需要进行调整和优化。

总结

遵循这些步骤,你就可以在程序中暴露指标并符合 Prometheus 和 Kubernetes 的规范。


原文地址:https://blog.csdn.net/weixin_38975685/article/details/142741479

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!