目录
一:手动添加监控
1.创建SVC
apiVersion: v1
kind: Service
metadata:
labels:
app: apigateway-svc-exporter
project: booyah
name: apigateway-svc-exporter
namespace: booyah-live
spec:
clusterIP: None # 没有特殊要求
ports:
- name: prom
port: 9500
protocol: TCP
targetPort: 9500
selector:
app: apigateway
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}
2.基于DNS自动发现后端主机
- job_name: apigateway
scrape_interval: 15s
dns_sd_configs:
- refresh_interval: 60s
type: A
port: 9500
names:
- apigateway-svc-exporter.booyah-live.svc.cluster.local
二:基于annotations自动添加监控
1.创建svc时,添加annotation注视信息
apiVersion: v1
kind: Service
metadata:
annotations:
# Enable automatic monitoring of all instances when Prometheus is running in the cluster.
prometheus.io/scrape: "true" # 只有为true时,才会添加监控
prometheus.io/path: "/metrics" # 监控的url
prometheus.io/port: "9500" # 端口
labels:
app: apigateway-svc-exporter
project: booyah
name: apigateway-svc-exporter
namespace: booyah-live
spec:
clusterIP: None # 没有特殊要求
ports:
- name: prom
port: 9500
protocol: TCP
targetPort: 9500
selector:
app: apigateway
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}
2.配置prom发现规则(helm安装的默认就有)
- job_name: kubernetes-service-endpoints
kubernetes_sd_configs:
- role: endpoints
relabel_configs:
- action: keep
regex: true
source_labels:
- __meta_kubernetes_service_annotation_prometheus_io_scrape
- action: replace
regex: (https?)
source_labels:
- __meta_kubernetes_service_annotation_prometheus_io_scheme
target_label: __scheme__
- action: replace
regex: (.+)
source_labels:
- __meta_kubernetes_service_annotation_prometheus_io_path
target_label: __metrics_path__
- action: replace
regex: ([^:]+)(?::d+)?;(d+)
replacement: $1:$2
source_labels:
- __address__
- __meta_kubernetes_service_annotation_prometheus_io_port
target_label: __address__
- action: labelmap
regex: __meta_kubernetes_service_label_(.+)
- action: replace
source_labels:
- __meta_kubernetes_namespace
target_label: kubernetes_namespace
- action: replace
source_labels:
- __meta_kubernetes_service_name
target_label: kubernetes_name
- action: replace
source_labels:
- __meta_kubernetes_pod_node_name
target_label: kubernetes_node