prometheus 密码认证之base认证
一、生成加密哈希
安装加密工具
yum install -y httpd-tools
自定义密码
htpasswd -nBC 12 '' | tr -d ':\n'
然后输入密码,会出现一串加密字符串
我这里直接用工具生成
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
二、创建web-config.yml配置文件
basic_auth_users:
admin: $2a$10$z58JxPndLBwFHVwTUPieGe.4ccpeaAjNeh5pth0cmylX03ECUsOoG
三、重新启动容器
version: '3'
services:
container_name: node-exporter
restart: unless-stopped
hostname: node-exporter
privileged: true
ports:
- "9100:9100"
networks:
- "prometheus"
volumes:
- "/opt/prometheus/node-exporter/proc:/host/proc:ro"
- "/opt/prometheus/node-exporter/sys:/host/sys:ro"
- "/opt/prometheus/node-exporter:/rootfs:ro"
- "/etc/localtime:/etc/localtime"
prom:
image: prom/prometheus:v2.42.0
container_name: prometheus
restart: unless-stopped
hostname: prometheus
privileged: true
ports:
- "9090:9090"
networks:
- "prometheus"
volumes:
- "/opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml"
- "/etc/localtime:/etc/localtime"
- "/opt/prometheus/web-config.yml:/etc/prometheus/web-config.yml"
command:
--web.enable-lifecycle #热加载
--config.file=/etc/prometheus/prometheus.yml # 指定配置文件
--web.config.file=/etc/prometheus/web-config.yml
networks:
prometheus:
driver: bridge
/opt/prometheus/prometheus.yml 本地prometheus的配置文件
/opt/prometheus/web-config.yml 本地prometheus的密码配置文件
注意:检查web-config.yml的语法
promtool check web-config web-config.yml
四、访问测试
没有用户名密码
curl --head --request GET http://localhost:9090/graph
有用户名密码
curl -u admin:123456 --request GET http://localhost:9090/metrics