在Prometheus中添加Basic Authentication(基本认证)用户,可以实现对Prometheus Web UI的访问权限控制。下面是在Kubernetes环境中添加basic_auth_users的完整步骤:
- 创建一个htpasswd文件,该文件包含要添加的用户名和密码信息。
htpasswd -c auth <username>
- 将生成的auth文件存储为Secret对象。
kubectl create secret generic prometheus-basic-auth --from-file=auth
- 编辑Prometheus配置文件prometheus.yaml,在spec.containers.args中增加以下内容:
args:
- '--web.enable-lifecycle'
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/data/'
- '--web.console.libraries=/usr/share/prometheus/console_libraries'
- '--web.console.templates=/usr/share/prometheus/consoles'
- '--web.external-url=http://localhost:9090/'
+ - '--web.auth.basic.user-file=/etc/prometheus/auth'
+ - '--web.auth.basic.enabled=true'
- 更新Prometheus Deployment对象,将Secret挂载到容器内部,并重启Deployment。
kubectl set volume deployment prometheus-server --add \
--name=prometheus-basic-auth \
--type=secret \
--secret-name=prometheus-basic-auth \
--mount-path /etc/prometheus/
kubectl rollout restart deployment prometheus-server
- 确认配置是否生效。在浏览器中访问Prometheus Web UI时会提示输入用户名和密码。
通过以上步骤,我们成功地向Prometheus添加了Basic Authentication用户。这样可以保护我们的监控数据不被未经授权的用户访问和查看。