prometheus监控k8s的配置文件该怎么写
时间: 2023-07-28 14:14:05 浏览: 124
k8s-prometheus-配置文件.zip
以下是一个简单的示例配置文件,用于使用 Prometheus 监控 Kubernetes 集群:
```yaml
global:
scrape_interval: 1m
evaluation_interval: 1m
external_labels:
monitor: 'prometheus'
rule_files:
- /etc/prometheus/rules/*.rules.yml
scrape_configs:
- job_name: 'kubernetes-apiservers'
scheme: https
tls_config:
ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
cert_file: /var/run/secrets/kubernetes.io/serviceaccount/client.crt
key_file: /var/run/secrets/kubernetes.io/serviceaccount/client.key
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
kubernetes_sd_configs:
- role: endpoints
namespaces:
names:
- default
relabel_configs:
- source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
action: keep
regex: default;kubernetes;https
- source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
action: replace
target_label: __address__
regex: ([^:]+)(?::\d+)?;[^;]+;https
replacement: kubernetes.default.svc:443
- source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
action: replace
target_label: __param_target
regex: ([^:]+)(?::\d+)?;[^;]+;https
replacement: /healthz
- action: labelmap
regex: __meta_kubernetes_pod_label_(.+)
- source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_pod_name]
action: replace
target_label: kubernetes_pod_name
regex: (.+);(.+)
- source_labels: [__meta_kubernetes_namespace]
action: replace
target_label: kubernetes_namespace
- source_labels: [__meta_kubernetes_service_name]
action: replace
target_label: kubernetes_service_name
- job_name: 'kubernetes-nodes'
scheme: https
tls_config:
ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
cert_file: /var/run/secrets/kubernetes.io/serviceaccount/client.crt
key_file: /var/run/secrets/kubernetes.io/serviceaccount/client.key
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
kubernetes_sd_configs:
- role: node
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
replacement: /metrics
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: kubernetes.default.svc:443
- source_labels: [__meta_kubernetes_node_label_kubernetes_io_hostname]
target_label: instance
- action: labelmap
regex: __meta_kubernetes_node_label_(.+)
- source_labels: [__meta_kubernetes_namespace]
action: replace
target_label: kubernetes_namespace
```
此配置文件使用 Kubernetes SD 配置来动态发现和监控 Kubernetes API 服务器和节点。要使用此配置文件,请将其保存为 `prometheus.yml` 并将其部署到 Prometheus 服务器。
阅读全文