- job_name: kubernetes-nodes-cadvisor honor_timestamps: false scrape_interval: 30s scrape_timeout: 10s metrics_path: /metrics scheme: https kubernetes_sd_configs: - role: node bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token tls_config: ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt insecure_skip_verify: true relabel_configs: - separator: ; regex: __meta_kubernetes_node_label_(.+) replacement: $1 action: labelmap - separator: ; regex: (.*) target_label: __metrics_path__ replacement: /metrics/cadvisor action: replace
以k8s 1.15版本爲例
代碼在:E:go_pathsrcgithub.comkuberneteskubernetespkgkubeletserverserver.gonode
func containerPrometheusLabelsFunc(s stats.Provider) metrics.ContainerLabelsFunc { // containerPrometheusLabels maps cAdvisor labels to prometheus labels. return func(c *cadvisorapi.ContainerInfo) map[string]string { // Prometheus requires that all metrics in the same family have the same labels, // so we arrange to supply blank strings for missing labels var name, image, podName, namespace, containerName string if len(c.Aliases) > 0 { name = c.Aliases[0] } image = c.Spec.Image if v, ok := c.Spec.Labels[kubelettypes.KubernetesPodNameLabel]; ok { podName = v } if v, ok := c.Spec.Labels[kubelettypes.KubernetesPodNamespaceLabel]; ok { namespace = v } if v, ok := c.Spec.Labels[kubelettypes.KubernetesContainerNameLabel]; ok { containerName = v } // Associate pod cgroup with pod so we have an accurate accounting of sandbox if podName == "" && namespace == "" { if pod, found := s.GetPodByCgroupfs(c.Name); found { podName = pod.Name namespace = pod.Namespace } } set := map[string]string{ metrics.LabelID: c.Name, metrics.LabelName: name, metrics.LabelImage: image, "pod_name": podName, "pod": podName, "namespace": namespace, "container_name": containerName, "container": containerName, } return set } }
--node-labels=os.name=xxxx,os.version=xxxx,os.architecture=amd64,