prometheus默认是没有用户密码登录认证的,对于部分环境可能会存在受攻击风险,以下实现安全用户密码登录。
Prometheus于2.24版本(包括2.24)之后提供Basic Auth功能进行加密访问,在浏览器登录UI的时候需要输入用户密码,访问Prometheus api的时候也需要加上用户密码。
yum install -y python pip
pip install bcrypt
cat <<'EOF'>>/prometheus/config/prometheus_passwd.py
import getpass
import bcrypt
password = getpass.getpass("password: ")
hashed_password = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt())
print(hashed_password.decode())
EOF
python /prometheus/config/prometheus_passwd.py
cat <<'EOF'>>/prometheus/config/basic_auth.yaml
basic_auth_users:
admin: $2b$12$S.cAXMn0aP/LoTPgUhhcoee3fifsHUHN9GCuRJbdKQ5lOByUAOGRq
EOF
cat <<'EOF'>>/prometheus/config/docker-compose.yml
version: '3'
services:
prometheus:
image: prom/prometheus:latest
hostname: prometheus
restart: always
ports:
- "9090:9090"
volumes:
- ./data:/data
- ./config/prometheus.yml:/etc/prometheus/prometheus.yml
- ./config/node_down.yml:/etc/prometheus/node_down.yml
- ./config/vmware_exporter.yml:/etc/prometheus/vmware_exporter.yml
- ./config/basic_auth.yaml:/etc/prometheus/basic_auth.yaml
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--web.enable-admin-api'
- '--web.enable-lifecycle'
- '--web.config.file=/etc/prometheus/basic_auth.yaml'
EOF
docker-compose up -d