Please enable Javascript to view the contents

给 Node Exporter 添加抓取凭证

 ·  ☕ 1 分钟

1. 背景

Node Exporter 是 Prometheus 生态系统中用于收集主机指标的常用组件,但默认情况下不提供访问认证。本文介绍如何为 Kubernetes 环境中的 Node Exporter 添加基本认证,提高安全性。

2. Node Exporter 配置凭证

2.1 生成加密密码

使用 htpasswd 工具生成加密密码:

1
htpasswd -nBC 12 "" | tr -d ':\n'

这里需要输入密码,生成的输出将是一个加密的密码字符串。

2.2 创建 web-config.yml 配置文件

首先,创建一个包含基本认证用户信息的配置文件:

1
vim web-config.yml

新增以下内容:

1
2
basic_auth_users:
  default: $2y$12$yEap0I9iBtuuLBdvL51LU.xxx.xxx

这里的 default 是用户名,后面的字符串是通过 htpasswd 生成的加密密码。

2.3 创建 ConfigMap

利用 web-config.yml 创建一个 ConfigMap,以便在 Node Exporter DeamonSet 中使用:

1
2
3
kubectl create configmap node-exporter-web-config \
  --from-file=web-config.yml=web-config.yml \
  -n monitoring

2.4 修改 Node Exporter DaemonSet

编辑 Node Exporter DaemonSet 配置,加载认证配置:

1
kubectl -n monitoring edit ds node-exporter

添加启动参数和挂载 ConfigMap:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
containers:
  - name: node-exporter
    image: prom/node-exporter:master
    imagePullPolicy: IfNotPresent
    args:
      - --web.config.file=/etc/node-exporter/web-config.yml
    ports:
      - containerPort: 9100
        hostPort: 9100
        name: scrape
        protocol: TCP
    volumeMounts:
      - name: web-config
        mountPath: /etc/node-exporter/web-config.yml
        subPath: web-config.yml
volumes:
  - name: web-config
    configMap:
      name: node-exporter-web-config

3. 采集端添加凭证

3.1 创建认证凭证 Secret

为 Prometheus 创建包含访问凭证的 Secret:

1
2
3
4
kubectl create secret generic node-exporter-basic-auth \
  --from-literal=username='default' \
  --from-literal=password='xxx' \
  -n monitoring

3.2 配置 ServiceMonitor

更新 ServiceMonitor 配置以使用认证凭证:

1
kubectl -n monitoring edit servicemonitor ops-node-exporter

添加 basicAuth 配置:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  labels:
    tier: ops
  name: ops-node-exporter
  namespace: monitoring
spec:
  endpoints:
    - interval: 15s
      basicAuth:
        username:
          name: node-exporter-basic-auth
          key: username
        password:
          name: node-exporter-basic-auth
          key: password
      port: metric

微信公众号
作者
微信公众号