生成认证密钥
root@localhost:# htpasswd -nBC 10 "" | tr -d ':\n'; echo
New password: #输入密钥
Re-type new password: #在输入一次密钥
$2y$10$gBSRwCogOmIjGNa02TW7hu.XcALfWowuA/lMvIDj0GYLGV/udrR5K
创建basic_auth_users文件
cat > /apps/node_exporter/node_exporter.yml << EOF
basic_auth_users:
admin: $2y$10$gBSRwCogOmIjGNa02TW7hu.XcALfWowuA/lMvIDj0GYLGV/udrR5K
EOF
- 用户:admin
- 密码:$2y$10$gBSRwCogOmIjGNa02TW7hu.XcALfWowuA/lMvIDj0GYLGV/udrR5K
测试
使用二进制文件直接指定basic_auth_users文件启动,测试能否正常启动
root@localhost:# ./node_exporter --web.config.file="node_exporter.yml"
ts=2024-06-14T02:51:55.943Z caller=node_exporter.go:193 level=info msg="Starting node_exporter" version="(version=1.8.1, branch=HEAD, revision=400c3979931613db930ea035f39ce7b377cdbb5b)"
ts=2024-06-14T02:51:55.943Z caller=node_exporter.go:194 level=info msg="Build context" build_context="(go=go1.22.3, platform=linux/amd64, user=root@7afbff271a3f, date=20240521-18:36:22, tags=unknown)"
ts=2024-06-14T02:51:55.943Z caller=node_exporter.go:196 level=warn msg="Node Exporter is running as root user. This exporter is designed to run as unprivileged user, root is not required."
ts=2024-06-14T02:51:55.945Z caller=diskstats_common.go:111 level=info collector=diskstats msg="Parsed flag --collector.diskstats.device-exclude" flag=^(z?ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\d+n\d+p)\d+$
ts=2024-06-14T02:51:55.945Z caller=filesystem_common.go:111 level=info collector=filesystem msg="Parsed flag --collector.filesystem.mount-points-exclude" flag=^/(dev|proc|run/credentials/.+|sys|var/lib/docker/.+|var/lib/containers/storage/.+)($|/)
ts=2024-06-14T02:51:55.945Z caller=filesystem_common.go:113 level=info collector=filesystem msg="Parsed flag --collector.filesystem.fs-types-exclude" flag=^(autofs|binfmt_misc|bpf|cgroup2?|configfs|debugfs|devpts|devtmpfs|fusectl|hugetlbfs|iso9660|mqueue|nsfs|overlay|proc|procfs|pstore|rpc_pipefs|securityfs|selinuxfs|squashfs|sysfs|tracefs)$
ts=2024-06-14T02:51:55.945Z caller=node_exporter.go:111 level=info msg="Enabled collectors"
ts=2024-06-14T02:51:55.945Z caller=node_exporter.go:118 level=info collector=arp
ts=2024-06-14T02:51:55.945Z caller=node_exporter.go:118 level=info collector=bcache
ts=2024-06-14T02:51:55.945Z caller=node_exporter.go:118 level=info collector=bonding
ts=2024-06-14T02:51:55.945Z caller=node_exporter.go:118 level=info collector=btrfs
ts=2024-06-14T02:51:55.945Z caller=node_exporter.go:118 level=info collector=conntrack
ts=2024-06-14T02:51:55.945Z caller=node_exporter.go:118 level=info collector=cpu
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=cpufreq
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=diskstats
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=dmi
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=edac
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=entropy
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=fibrechannel
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=filefd
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=filesystem
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=hwmon
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=infiniband
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=ipvs
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=loadavg
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=mdadm
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=meminfo
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=netclass
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=netdev
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=netstat
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=nfs
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=nfsd
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=nvme
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=os
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=powersupplyclass
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=pressure
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=rapl
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=schedstat
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=selinux
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=sockstat
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=softnet
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=stat
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=tapestats
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=textfile
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=thermal_zone
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=time
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=timex
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=udp_queues
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=uname
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=vmstat
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=watchdog
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=xfs
ts=2024-06-14T02:51:55.946Z caller=node_exporter.go:118 level=info collector=zfs
ts=2024-06-14T02:51:55.946Z caller=tls_config.go:313 level=info msg="Listening on" address=[::]:9100
ts=2024-06-14T02:51:55.946Z caller=tls_config.go:352 level=info msg="TLS is disabled." http2=false address=[::]:9100
测试结果能正常启动服务
创建启停文件
cat > /etc/systemd/system/node-exporter.service << EOF
[Unit]
Description=Prometheus Node Exporter
After=network.target
[Service]
ExecStart=/apps/node_exporter/node_exporter \
--web.config.file="/apps/node_exporter/node_exporter.yml"
[Install]
WantedBy=multi-user.target
EOF
启动服务
systemctl daemon-reload && systemctl restart node-exporter && systemctl enable node-exporter
验证认证配置是否生效
Promtheus端配置
新增job
- job_name: 'test'
basic_auth:
username: admin
password: Seca@2024...
static_configs:
- targets: ["10.84.3.124:9100"]
重新加载配置使job生效
curl -X POST "http://10.84.3.125:9090/-/reload"
仅登录用户可评论,点击 登录