目录

使用node Exporter监控Kubernetes节点

本文记录一下在k8s环境下安装node exporter的过程,node exporter和节点相关, 每台节点都需要数据,所以daemonset部署是最合适的,并且每台节点的数据是独立的,所以不能使用service来自动发现,而是使用host network来暴露服务,然后使用prometheus的自动发现来自动监控。看了有些教程加了headless service,不过我测试了这个service是没有用的,因为是直接访问的节点ip,并没有走service

部署node exporter

使用yaml

---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-exporter
  namespace: monitor
  labels:
    k8s-app: node-exporter
spec:
  selector:
    matchLabels:
      k8s-app: node-exporter
  template:
    metadata:
      labels:
        k8s-app: node-exporter
    spec:
      containers:
      - name: node-exporter
        image: prom/node-exporter:v1.1.2
        ports:
        - name: metrics
          containerPort: 9102					# 端口被占用了,改成了9102
        args:
        - "--path.procfs=/host/proc"
        - "--path.sysfs=/host/sys"
        - "--path.rootfs=/host"
        - "--web.listen-address=:9102"
        volumeMounts:
        - name: dev
          mountPath: /host/dev
        - name: proc
          mountPath: /host/proc
        - name: sys
          mountPath: /host/sys
        - name: rootfs
          mountPath: /host
      volumes:
        - name: dev
          hostPath:
            path: /dev
        - name: proc
          hostPath:
            path: /proc
        - name: sys
          hostPath:
            path: /sys
        - name: rootfs
          hostPath:
            path: /
      hostPID: true
      hostNetwork: true							# hostnetwork直接在节点上暴露端口
      tolerations:
      - operator: "Exists"

配置prometheus

      - job_name: 'host'
        kubernetes_sd_configs:
        - role: node
        relabel_configs:
        - action: replace
          source_labels: [__address__]
          regex: '(.*):10250'
          replacement: '${1}:9102'
          target_label: __address__