环境准备
系统Centos 7.9
k8s集群:
版本:1.21.5
节点:
192.168.10.201 master
192.168.10.202 work
在k8s集群每个节点上,部署node-export
此案例,只在192.168.10.201 master 节点上,安装了node-export,进行改节点的监控
脚本配置:
deploy-node-exporter.sh
#!/bin/bashwgethttps://github.com/prometheus/node_exporter/releases/download/v1.4.0/node_exporter-1.4.0.linux-amd64.tar.gztarzxfnode_exporter-1.4.0.linux-amd64.tar.gzmvnode_exporter-1.4.0.linux-amd64/usr/local/node_exportercat<<EOF>/usr/lib/systemd/system/node_exporter.service[Unit]Descrption=https://prometheus.io[Service]Restart=on-failureExecStart=/usr/local/node_exporter/node_exporter--collector.systemd.unit-whitelist=(docker|kubelet|kube-proxy|flanneld).service[Install]WantedBy=multi-user.targetEOFsystemctldaemon-reloadsystemctlenablenode_exportersystemctlrestartnode_exporter
执行脚本部署node-export
shdeploy-node-exporter.sh
在prometheus 的configmap中,设置对node-exporter 的监控配置
有关prometheus 的部署,请查看 prometheus部署 这篇文章
在这篇文章的部署配置文件中,添加对部署的了node-export的节点
资源监控配置:
prometheus-configmap.yaml 完整配置:
apiVersion v1 kind ConfigMap metadata name prometheus-config namespace kube-system labels kubernetes.io/cluster-service"true" addonmanager.kubernetes.io/mode EnsureExists data prometheus.yml scrape_configs: - job_name: prometheus static_configs: - targets: - localhost:9090 - job_name: kubernetes-nodes static_configs: - targets: [192.168.10.201:9100] labels: node_name: k201 - job_name: kubernetes-apiservers kubernetes_sd_configs: - role: endpoints relabel_configs: - action: keep regex: default;kubernetes;https source_labels: - __meta_kubernetes_namespace - __meta_kubernetes_service_name - __meta_kubernetes_endpoint_port_name scheme: https tls_config: ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt insecure_skip_verify: true bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/tokenjob_name kubernetes-nodes-kubelet kubernetes_sd_configsrole node relabel_configsaction labelmap regex __meta_kubernetes_node_label_(.+) scheme https tls_config ca_file /var/run/secrets/kubernetes.io/serviceaccount/ca.crt insecure_skip_verifytrue bearer_token_file /var/run/secrets/kubernetes.io/serviceaccount/token job_name kubernetes-nodes-cadvisor kubernetes_sd_configsrole node relabel_configsaction labelmap regex __meta_kubernetes_node_label_(.+) target_label __metrics_path__ replacement /metrics/cadvisor scheme https tls_config ca_file /var/run/secrets/kubernetes.io/serviceaccount/ca.crt insecure_skip_verifytrue bearer_token_file /var/run/secrets/kubernetes.io/serviceaccount/token job_name kubernetes-service-endpoints kubernetes_sd_configsrole endpoints relabel_configsaction keep regextrue 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 job_name kubernetes-services kubernetes_sd_configsrole service metrics_path /probe params module http_2xx relabel_configsaction keep regextrue source_labels __meta_kubernetes_service_annotation_prometheus_io_probe source_labels __address__ target_label __param_target replacement blackbox target_label __address__ source_labels __param_target target_label instance action labelmap regex __meta_kubernetes_service_label_(.+) source_labels __meta_kubernetes_namespace target_label kubernetes_namespace source_labels __meta_kubernetes_service_name target_label kubernetes_name job_name kubernetes-pods kubernetes_sd_configsrole pod relabel_configsaction keep regextrue source_labels __meta_kubernetes_pod_annotation_prometheus_io_scrape action replace regex (.+) source_labels __meta_kubernetes_pod_annotation_prometheus_io_path target_label __metrics_path__ action replace regex ( ^ +)(? \d+)?;(\d+) replacement $1 $2 source_labels __address__ __meta_kubernetes_pod_annotation_prometheus_io_port target_label __address__ action labelmap regex __meta_kubernetes_pod_label_(.+) action replace source_labels __meta_kubernetes_namespace target_label kubernetes_namespace action replace source_labels __meta_kubernetes_pod_name target_label kubernetes_pod_name
重启prometheus 服务
执行一下命令进行prometheus服务重启:
kubectldelete-fprometheus-configmap.yamlkubectlapply-fprometheus-configmap.yaml
打开prometheus的控制台