本文永久链接: https://www.xtplayer.cn/prometheus/enable-access-authentication-for-node-exporter/

步骤一、创建 node-exporter 配置密文

这一步骤中,根据需要修改用户名, 然后输入密码,其他参数不要修改。

# 1. 定义用户名和生成密码哈希
export USERNAME=node-exporter

# 命令执行后会提示 "New password:",输入你的用户名密码
export HASH=$(htpasswd -nBC 12 "" | tr -d ':\n')

# 2. 创建配置文件
cat > web-config.yml <<EOF
basic_auth_users:
${USERNAME}: ${HASH}
EOF

# 3. 查看生成的配置(确认没有空格)
cat web-config.yml

# 4. 创建 Secret
kubectl create secret generic node-exporter-config \
--from-file=config.yaml=web-config.yml \
-n cattle-monitoring-system

# 5. 验证 Secret 内容
kubectl get secret node-exporter-config -n cattle-monitoring-system -o yaml

步骤二、创建认证 secret

在 node-exporter 启用访问认证之后,prometheus 访问 node-exporter 获取指标数据需要认证。以下的用户名和密码用于prometheus 访问 node-exporter 的访问认证。

注意:此用户名和密码与步骤一保持一致。

kubectl create secret generic node-exporter-auth \
--from-literal=username=<用户名> \
--from-literal=password=<密码> \
-n cattle-monitoring-system

步骤三、创建用户名和密码的 base64 字符串备用

node-exporter pod 启用了 k8s 的健康检查,在 node-exporter 启用访问认证之后,k8s 健康检查会因为无法访问 node-exporter 端口导致健康检查失败。以下用户名和密码用于 k8s 健康检查时,访问 node-exporter 认证的用户名和密码。

注意:此用户名和密码与步骤一保持一致。

# echo -n "<用户名>:<密码>" | base64
# 示例
echo -n "node-exporter:node-exporter" | base64

步骤四、更新监控 App 配置

编辑监控 App > 编辑 YAML,在 prometheus-node-exporter 层级下插入如下配置。

注意: 只需要调整 httpHeaders 中的认证字符串,其他配置不用修改。

prometheus-node-exporter:
livenessProbe:
httpGet:
httpHeaders:
- name: Authorization
value: Basic <步骤三中生成的字符串>
readinessProbe:
httpGet:
httpHeaders:
- name: Authorization
value: Basic <步骤三中生成的字符串>
secrets:
- name: node-exporter-config
mountPath: /tmp
extraArgs:
- --web.config.file=/tmp/config.yaml
prometheus:
monitor:
basicAuth:
username:
name: node-exporter-auth # Secret 名称
key: username # Secret 中的 key
password:
name: node-exporter-auth # Secret 名称
key: password # Secret 中的 key