Kube-prometheus 监控 K8s 集群外服务的两种方式

时间:2022-10-18 19:00:48

本文档以 Kube-prometheus 监控 K8s 集群外服务器系统相关指标为例,其他服务大同小异

方式一:通过 ServiceMonitor 方式

创建 Service 和 ServiceMonitor 文件名为 external-node.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: external-node-exporter
  namespace: monitoring
  labels:
    app: external-node-exporter
    app.kubernetes.io/name: node-exporter
spec:
  type: ClusterIP
  ports:
  - name: metrics
    port: 9100
    protocol: TCP
    targetPort: 9100
---
apiVersion: v1
kind: Endpoints
metadata:
    name: external-node-exporter
    namespace: monitoring
    labels:
      app: external-node-exporter
      app.kubernetes.io/name: node-exporter
subsets:
- addresses:
  - ip: 192.168.10.10  # 这里是外部的资源列表
  ports:
  - name: metrics
    port: 9100
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: external-node-exporter
  namespace: monitoring
  labels:
    app: external-node-exporter
    release: prometheus
spec:
  selector:
    matchLabels:            # Service选择器
      app: external-node-exporter
  namespaceSelector:        # Namespace选择器
    matchNames:
    - monitoring
  endpoints:
  - port: metrics           # 采集节点端口(svc定义)
    interval: 10s           # 采集频率根据实际需求配置,prometheus默认15s
    path: /metrics          # 默认地址/metrics

运行 external-node.yaml

# kubectl apply -f external-node.yaml

方式二:通过 Kube-prometheus 自定义参数

添加 prometheus-additional.yaml

- job_name: 'node-exporter-others'
  static_configs:
    - targets:
      - 192.168.10.10:9100

  metrics_path: /metrics

通过 prometheus-additional.yaml 文件生成 Secret

# kubectl create secret generic additional-scrape-configs --from-file=prometheus-additional.yaml -n monitoring

编辑 prometheus-prometheus.yaml 添加额外的抓取参数

additionalScrapeConfigs:
    name: additional-scrape-configs
    key: prometheus-additional.yaml

prometheus-prometheus.yaml 在 manifests 目录下

应用 prometheus-prometheus.yaml

# kubectl apply -f prometheus-prometheus.yaml

外部需要被监控服务器安装 node-exporter

docker 方式运行

# docker run -d --name=node_exporter \
        --restart=always  \
        -v /:/host:ro,rslave \
        -p 9100:9100 \
        prom/node-exporter:v1.4.0 \
        --path.rootfs=/host

systemd 方式运行

# cat /usr/lib/systemd/system/node-exporter.service
[Unit]
Description=Node Exporter For Prometheus
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/node_exporter "--web.listen-address=:9100"

[Install]
WantedBy=mutil-user.target


# systemctl daemon-reload
# systemctl status node-exporter

node-exporter 会暴露系统监控相关的 /metrics 接口来供 Prometheus 来抓取