title: Selectors和拉取配置
概述
监控应用设置prometheus.prometheusSpec.ignoreNamespaceSelectors=false,默认情况下可以跨所有命名空间进行监控。
这确保你可以查看部署在带有istio-injection=enabled标签的命名空间中的资源的流量、指标和 Chart。
如果你想将 Prometheus 限制在特定的命名空间,请设置prometheus.prometheusSpec.ignoreNamespaceSelectors=true。一旦你这样做,你将需要添加额外的配置来继续监控你的资源。
通过将 ignoreNamespaceSelectors 设置为 True,将监测限制在特定的命名空间。
这会将监控限制在特定的命名空间:
- 在集群资源管理器中,如果已经安装了监控,请导航到安装的应用程序,或应用程序和市场中的Chart。
- 如果开始新的安装,单击rancher-monitoring Chart,然后在Chart 选项中单击编辑为 Yaml。
- 如果更新现有的安装,请单击升级,然后在Chart 选项中单击编辑为 Yaml。
- 设置
prometheus.prometheusSpec.ignoreNamespaceSelectors=true。 - 完成安装或升级
结果: Prometheus 将被限制在特定的命名空间,这意味着需要设置以下配置之一,以继续在各种仪表盘中查看数据。
启用 Prometheus 检测其他命名空间中的资源
当prometheus.prometheusSpec.ignoreNamespaceSelectors=true时,有两种不同的方法可以让 Prometheus 检测其他命名空间中的资源。
- 监控特定的命名空间:在带有您要清除的目标的命名空间中添加服务监视器或花苞监视器。
- 跨命名空间监控:在你的 rancher-monitoring 实例中添加一个
additionalScrapeConfig,以便刮取所有名字空间中的所有目标。
监视特定的命名空间:创建服务监视器或 Pod 监视器
该选项允许您定义您希望在特定命名空间中监控哪些特定的服务或 pod。
可用性的权衡是,你必须为每个命名空间创建服务监控器或 pod 监控器,因为你不能跨命名空间监控。
前提条件:为
<your namespace>定义一个 ServiceMonitor 或 PodMonitor。下面提供一个 ServiceMonitor 的例子。
- 在集群资源管理器中,打开 kubectl shell。
如果该文件存储在您的集群中的本地,请运行
kubectl create -f <name of service/pod monitor file>.yaml。 - 或者运行
cat<< EOF | kubectl apply -f -,将文件内容粘贴到终端,然后运行EOF完成命令。 - 如果开始新的安装,单击rancher-monitoring图,向下滚动到Preview Yaml。
- 运行
kubectl label namespace <your namespace> istio-injection=enabled启用 envoy sidecar 注入。
结果: <your namespace>可以被 Prometheus 拉取到。
apiVersion: monitoring.coreos.com/v1kind: ServiceMonitormetadata:name: envoy-stats-monitornamespace: istio-systemlabels:monitoring: istio-proxiesspec:selector:matchExpressions:- { key: istio-prometheus-ignore, operator: DoesNotExist }namespaceSelector:any: truejobLabel: envoy-statsendpoints:- path: /stats/prometheustargetPort: 15090interval: 15srelabelings:- sourceLabels: [__meta_kubernetes_pod_container_port_name]action: keepregex: ".*-envoy-prom"- action: labeldropregex: "__meta_kubernetes_pod_label_(.+)"- sourceLabels: [__meta_kubernetes_namespace]action: replacetargetLabel: namespace- sourceLabels: [__meta_kubernetes_pod_name]action: replacetargetLabel: pod_name
跨命名空间监控:将 ignoreNamespaceSelectors 设置为 False
这可以通过给 Prometheus 提供额外的 scrape 配置,实现跨命名空间的监测。
可用性的权衡是,所有 Prometheus 的 “additionalScrapeConfigs “都维护在一个 Secret 中。如果在安装 Istio 之前已经使用 additionalScrapeConfigs 部署了监控,这可能会给升级带来困难。
- 如果开始新的安装,单击rancher-monitoringChart,然后在Chart 选项中单击编辑为 Yaml。
- 如果更新现有的安装,请单击升级,然后在Chart 选项中单击编辑为 Yaml。
- 如果更新现有的安装,单击升级,然后单击预览 Yaml。
- 设置
prometheus.prometheusSpec.additionalScrapeConfigs数组为下面提供的Additional Scrape Config。 - 完成安装或升级
结果:所有带有istio-injection=enabled标签的命名空间将被 prometheus 清除。
- job_name: "istio/envoy-stats"scrape_interval: 15smetrics_path: /stats/prometheuskubernetes_sd_configs:- role: podrelabel_configs:- source_labels: [__meta_kubernetes_pod_container_port_name]action: keepregex: ".*-envoy-prom"- source_labels:[__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]action: replaceregex: ([^:]+)(?::\d+)?;(\d+)replacement: $1:15090target_label: __address__- action: labelmapregex: __meta_kubernetes_pod_label_(.+)- source_labels: [__meta_kubernetes_namespace]action: replacetarget_label: namespace- source_labels: [__meta_kubernetes_pod_name]action: replacetarget_label: pod_name
