二、Prometheus TLS加密认证和基于 basic_auth 用户名密码访问

Prometheus 基于用户名密码访问

参考博客:https://blog.csdn.net/qq_34556414/article/details/113106095

Export采集指标的地址谁都可以访问,这里可以使用基础认证使用用户名密码方式去采集被监控端,也就是访问接口使用用户名密码提高安全性

1. Node Export端配置密码

Basic Auth 支持配置多个用户名密码的。

2. 在被监控端这里生成密码

# 没有https-tools则 yum install -y httpd-tools
[root@VM_2-44 ~]# rpm -qa|grep httpd-tools   
httpd-tools-2.4.6-97.el7.centos.x86_64
[root@VM_2-44 ~]# htpasswd -nBC 12 '' | tr -d ':\n'
New password:               # 这里设置密码为123456
Re-type new password: 
$2y$12$6yR84yKSqoYv3B2D70QAOuqggT0QvdpMp1wUNfLwBo63oLYWc1AYy
[root@VM_2-44 ~]# 

3. 在node_exporter中新增配置文件

[root@VM_2-44 /usr/local/node_exporter]# ls
LICENSE  node_exporter  NOTICE
[root@VM_2-44 /usr/local/node_exporter]# vim node_exporter 

# 设置用户名为admin(可修改),密码为123456
[root@VM_2-44 /usr/local/node_exporter]# vim config.yml
basic_auth_users:
  # 当前设置的用户名为admin, 可以设置多个
  admin: $2y$12$6yR84yKSqoYv3B2D70QAOuqggT0QvdpMp1wUNfLwBo63oLYWc1AYy  

4. node_exporter启动时引用该配置

/usr/local/node_exporter/node_exporter --web.config=/usr/local/node_exporter/config.yml

# 上文一、中我们把node_exporter添加为服务了,修改node_exporter.service
[root@VM_2-44 /usr/local/node_exporter]# vim /usr/lib/systemd/system/node_exporter.service 
[Unit]
Description=node_exporter
After=network.target 

[Service]
ExecStart=/usr/local/node_exporter/node_exporter --web.config=/usr/local/node_exporter/config.yml
Restart=on-failure

[Install]
WantedBy=multi-user.target
[root@VM_2-44 /usr/local/node_exporter]# 

# 重启加载,并重启
systemctl daemon-reload 
systemctl restart node_exporter.service

5. 访问http://192.168.2.44:9100/metrics需要账户密码验证了

  • 现在普罗米修斯没有配置,可以看到采集不了数据了
    在这里插入图片描述

  • 需要密码才能访问

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ICCVHdBL-1616050058398)(D372EAC74D554CCAAE18932E97685556)]

6. 配置Prometheus启用用户名密码访问

  - job_name: 'linux'
    basic_auth:
      username: admin
      password: 123456
    static_configs:
    - targets: ['192.168.2.44:9100']

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-X1fP3W2L-1616050058400)(4D5D6673A9004724A72D2A0D08D2BBAE)]

7. 重启Prometheus后可以看到数据可以正常采集了

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hwb4NSC0-1616050058401)(FE45A35B9B5F443A81ED2FDEAAB6610C)]

PrometheusNode_Exporter TLS加密认证

在 Promethues 的监控体系中,社区中一直存在的一个观点是,Metrics 不包含过于私密的信息。所以你可以看到,大多数的 /metrics 接口都是直接暴露出来的,没什么特别的安全措施。

但随着 Prometheus 在生产中的大量应用,安全问题变得更加重要。

大家最先想到的解决办法就是,为 Prometheus 与监控目标之间的连接启用 TLS。 但由于各类 exporter 并不原生支持 TLS 连接,所以通常情况下我们会选择配合反向代理来完成。

这种方式能满足需求,但未免复杂了些。近期 Prometheus 对其安全模型做了修改 , 从 Node Exporter 开始到后续其他的组件,都将支持 TLS 和 basic auth, 同时也列出了最新的安全基准(默认情况下都支持 TLS v1.2 及以上)

注意:Node Exporter v1.0.0 版本以上才支持。

1. 启用 TLS ,准备证书

# openssl生成证书
[root@VM_2-45 /usr/local/prometheus]# openssl req -new -newkey rsa:2048 -days 3650 -nodes -x509 -keyout \
node_exporter.key -out node_exporter.crt \
-subj "/C=CN/ST=Beijing/L=Beijing/O=Moelove.info/CN=localhost"

[root@VM_2-45 /usr/local/prometheus]# ll node_exporter.*
-rw-r--r-- 1 root root 1289 3月  12 09:53 node_exporter.crt
-rw-r--r-- 1 root root 1704 3月  12 09:53 node_exporter.key
[root@VM_2-45 /usr/local/prometheus]# 

2. node_exporter新增config.yml配置引用TLS

  • 复制前面/usr/local/prometheus目录下生成的 node_exporter.crtnode_exporter.key这两个文件到node_exporter目录下
[root@VM_2-45 /usr/local/prometheus]# cp node_exporter.* ../node_exporter/
[root@VM_2-45 /usr/local/prometheus]# cd ../node_exporter
[root@VM_2-45 /usr/local/node_exporter]# ls
LICENSE  node_exporter  node_exporter.crt  node_exporter.key  NOTICE
[root@VM_2-45 /usr/local/node_exporter]# 


# 创建config.yml
[root@VM_2-45 /usr/local/node_exporter]# vim config.yml
tls_server_config:    # TLS加密
  cert_file: node_exporter.crt
  key_file: node_exporter.key

  • 使用 --web.config 将配置文件传递给 Node Exporter
[root@VM_2-45 /usr/local/node_exporter]# cat /usr/lib/systemd/system/node_exporter.service 
[Unit]
Description=node_exporter
After=network.target 

[Service]
# 使用 --web.config 将配置文件传递给 Node Exporter
ExecStart=/usr/local/node_exporter/node_exporter --web.config=/usr/local/node_exporter/config.yml  
Restart=on-failure

[Install]
WantedBy=multi-user.target
[root@VM_2-45 /usr/local/node_exporter]# 

3. 启动node_exporter,验证TLS链接

[root@VM_2-45 /usr/local/node_exporter]# systemctl daemon-reload 
[root@VM_2-45 /usr/local/node_exporter]# systemctl start node_exporter.service

# 验证TLS链接
# 发现无法使用http连接了,可以使用https连接
[root@VM_2-45 /usr/local/node_exporter]# curl http://localhost:9100/metrics
[root@VM_2-45 /usr/local/node_exporter]# curl https://localhost:9100/metrics
curl: (60) Peer's certificate issuer has been marked as not trusted by the user.
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.
[root@VM_2-45 /usr/local/node_exporter]#

# 使用--cacert参数,将证书传递给curl 再次验证https(或者通过 -k 参数来忽略证书检查)
[root@VM_2-45 ~]# curl --cacert node_exporter.crt https://localhost:9100/metrics |head -n 10
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0.000116614
go_gc_duration_seconds{quantile="0.25"} 0.000124826
go_gc_duration_seconds{quantile="0.5"} 0.000197253
go_gc_duration_seconds{quantile="0.75"} 0.000307498
go_gc_duration_seconds{quantile="1"} 0.000307498
go_gc_duration_seconds_sum 0.000746191
go_gc_duration_seconds_count 4
# HELP go_goroutines Number of goroutines that currently exist.

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ms9URVoh-1616050058403)(3A48BCDDF8EC4CCF9375A01F2CF544C7)]

4. 配置 Prometheus 使用 TLS

  • 192.168.2.45的node_exporter使用TLS验证后,发现Prometheus无法通过http获取到数据了

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NVAuAEa9-1616050058404)(1E98275B8F0D4B2D9B5CA50257EF56CF)]

  • 修改下prometheus.yml配置文件,让 Prometheus 可以抓取 Node Exporter 暴露的 metrics
    scheme: https
    tls_config:
      ca_file: node_exporter.crt
      insecure_skip_verify: true  # 跳过不安全认证

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NjuRdJn5-1616050058404)(70970537E88D4F5D8A518128F5A35F49)]

  • https认证成功了

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gvJacT82-1616050058405)(7FB77CF921124156AFD03DDAB444045C)]

将剩下的节点也使用htpps方式

  • 将Prometheus的证书都复制到别的节点
[root@VM_2-45 /usr/local/prometheus]# scp node_exporter.* 192.168.2.43:/usr/local/node_exporter
node_exporter.crt                                                                                                                        100% 1289     1.7MB/s   00:00    
node_exporter.key                                                                                                                        100% 1704     1.9MB/s   00:00    
[root@VM_2-45 /usr/local/prometheus]# scp node_exporter.* 192.168.2.44:/usr/local/node_exporter
node_exporter.crt                                                                                                                        100% 1289     1.8MB/s   00:00    
node_exporter.key                                                                                                                        100% 1704     2.2MB/s   00:00    
[root@VM_2-45 /usr/local/prometheus]# scp node_exporter.* 121.196.147.41:/usr/local/node_exporter
  • node_exporter节点修改config.yml
    现在的效果是基于用户名密码、https双重保险啦
[root@aliyun node_exporter]# cat config.yml 
basic_auth_users:
  admin: $2y$12$6yR84yKSqoYv3B2D70QAOuqggT0QvdpMp1wUNfLwBo63oLYWc1AYy
tls_server_config: 
  cert_file: node_exporter.crt
  key_file: node_exporter.key

在这里插入图片描述

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0Vdwcsx5-1616050058406)(D033D87155E2406CB83ED60E19FBDAF4)]

Prometheus自己 TLS加密认证

完成以上操作仅仅是完成了对node_exproter的TLS加密等措施,Prometheus本身仍然是http连接直接就可以访问

  • 目标:Prometheus 通过https加密,及使用账号密码才能登录

1. 在Prometheus目录下新增config.yml文件

# 新增 config.yml 文件,使用TLS及basic_auth
[root@VM_2-45 /usr/local/prometheus]# vim config.yml
basic_auth_users:
  admin: $2y$12$6yR84yKSqoYv3B2D70QAOuqggT0QvdpMp1wUNfLwBo63oLYWc1AYy
tls_server_config:
  cert_file: node_exporter.crt
  key_file: node_exporter.key
[root@VM_2-45 /usr/local/prometheus]# ll
总用量 168008
-rw-r--r-- 1 root root      168 3月  12 13:31 config.yml
drwxr-xr-x 2 3434 3434       38 2月  18 00:11 console_libraries
drwxr-xr-x 2 3434 3434      173 2月  18 00:11 consoles
drwxr-xr-x 7 root root      172 3月  12 13:33 data
-rw-r--r-- 1 3434 3434    11357 2月  18 00:11 LICENSE
-rw-r--r-- 1 root root     1289 3月  12 10:48 node_exporter.crt
-rw-r--r-- 1 root root     1704 3月  12 10:48 node_exporter.key
-rw-r--r-- 1 3434 3434     3420 2月  18 00:11 NOTICE
-rwxr-xr-x 1 3434 3434 91044140 2月  17 22:19 prometheus
-rw-r--r-- 1 root root     1093 3月  12 13:11 prometheus.yml
-rwxr-xr-x 1 3434 3434 80948693 2月  17 22:21 promtool
drwxr-xr-x 2 root root       38 3月  11 14:27 rules
drwxr-xr-x 2 root root       22 3月  12 09:56 sd_config
-rwxr-xr-x 1 root root      240 3月  10 15:24 start.sh
-rw-r--r-- 1 root root      187 3月  12 10:58 targets.json
-rw-r--r-- 1 root root      183 3月  11 16:30 targets.yml
[root@VM_2-45 /usr/local/prometheus]# 

2. 修改prometheus.yml文件

# 将prometheus标签下加入认证,不然prometheus标签下的仍然是HTTP方式
 - job_name: 'prometheus'
    basic_auth:
      username: admin
      password: 123456
    scheme: https
    tls_config:
      ca_file: node_exporter.crt
      insecure_skip_verify: true
    static_configs:
    - targets: ['192.168.2.45:9090']

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-y8YjCoCD-1616050058407)(F5371768F36A405BBD10C555CB92C7FF)]

3. 修改启动服务脚本prometheus.service(引用刚才创建的config.yml)

# --web.config.file=config.yml
[root@VM_2-45 /usr/local/prometheus]# ./prometheus --config.file=prometheus.yml --web.config.file=config.yml  --web.listen-address=:9090 --web.enable-lifecycle

# 顺便把服务启动项修改下
[root@VM_2-45 /usr/local/prometheus]# vim /usr/lib/systemd/system/prometheus.service 

[Unit]
  Description=https://prometheus.io

  [Service]
  Restart=on-failure
  ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --web.config.file=/usr/local/prometheus/config.yml --web.listen-address=:9090 --web.enable-lifecycle

  [Install]
  WantedBy=multi-user.target

4. 重启Prometheus,验证加密保护

[root@VM_2-45 /usr/local/prometheus]# systemctl restart prometheus && systemctl status prometheus.service 
  • 重新打开已加密
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Xo5RhGji-1616050058407)(8B9F560BDCFA4CD6BE72AA19CC58EDB7)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-b1Lf7uOw-1616050058408)(49748DD02F7740DB97CA230A0DB86763)]

5. 使用TLS加密后Grafana获取不到数据

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-c8XpZ49I-1616050058409)(A97844E9A2CE4DD28AAC426EE695D109)]

  • 测试后必须要http
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-X5VwXxSQ-1616050058409)(B60FF62F16A8458593F60A95160B999B)]

注:尝试用nginx做反向代理,将http请求转发到https但是grafana验证通过,但是就是获取不到数据,待研究,付nginx配置

    server{
      listen 9091;
      server_name 192.168.2.45;
      location / {
            proxy_pass              https://192.168.2.45:9090;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        Host $http_host;
            proxy_connect_timeout 300s;
            proxy_send_timeout 300s;
            proxy_read_timeout 300s;
        }
    }

Prometheus 仅使用 Basic Auth(避免踩Grafana坑)

1. 修改config.yml,不使用TLS

[root@VM_2-45 /usr/local/prometheus]# cat config.yml 
basic_auth_users:
  admin: $2y$12$6yR84yKSqoYv3B2D70QAOuqggT0QvdpMp1wUNfLwBo63oLYWc1AYy

# 不使用TLS加密,仅使用basic_auth验证
#tls_server_config:
#  cert_file: node_exporter.crt
#  key_file: node_exporter.key
[root@VM_2-45 /usr/local/prometheus]# 

2. 修改prometheus.yml,不使用TLS

scrape_configs:
  - job_name: 'prometheus'
    basic_auth:
      username: admin
      password: 123456
# 将TLS内容注释掉
#    scheme: https
#    tls_config:
#      ca_file: node_exporter.crt
#      insecure_skip_verify: true
    static_configs:
    - targets: ['192.168.2.45:9090']

  • 重启prometheus,后grafana 勾选Basic auth添加数据源,给出账户密码

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mTOfxzVg-1616050058410)(A9E42F74098045B7B119784FC857C64E)]

### 解决 Prometheus 未授权访问的安全配置方案 为了防止 Prometheus 的未授权访问,可以采取多种安全措施来保护其 API Web UI 接口。以下是详细的解决方案: #### 配置基本认证 (Basic Authentication) 通过启用 HTTP 基本认证功能,可以有效阻止未经授权的用户访问 Prometheus 实例。 1. **创建用户名密码文件** 使用 `htpasswd` 工具生成包含用户名加密密码的文件。 ```bash htpasswd -c /etc/prometheus/htpasswd prometheus_user ``` 2. **更新 Prometheus 配置文件** 修改 Prometheus 的启动参数以加载上述身份验证文件。 ```yaml command: [ "/bin/prometheus", "--config.file=/etc/prometheus/prometheus.yml", "--web.enable-lifecycle", "--web.basic-auth-file=/etc/prometheus/htpasswd" ] ``` 此方法基于参考实现[^1]中的基础概念进行了扩展应用。 --- #### 启用 TLS 加密通信 TLS 可以为客户端与服务器之间的数据传输提供额外的安全保障。 1. **准备证书私钥** 创建自签名证书或者从受信任的 CA 获取正式证书。 ```bash openssl req -x509 -nodes -days 365 -newkey rsa:2048 \ -keyout tls.key -out tls.crt ``` 2. **修改 Prometheus 配置** 将生成的证书私钥路径传递给 Prometheus 进程。 ```yaml command: [ "/bin/prometheus", "--config.file=/etc/prometheus/prometheus.yml", "--web.tls-certificate=/path/to/tls.crt", "--web.tls-private-key=/path/to/tls.key" ] ``` 这一步骤同样遵循了 Kubernetes 中类似的实践思路。 --- #### 设置网络策略 (Network Policies) 如果运行环境为 Kubernetes,则可以通过定义 NetworkPolicy 来限制哪些 Pod 或 IP 地址能够访问 Prometheus。 ```yaml apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-prometheus-access spec: podSelector: matchLabels: app: prometheus policyTypes: - Ingress ingress: - from: - namespaceSelector: matchLabels: environment: monitoring ports: - protocol: TCP port: 9090 ``` 以上 YAML 文件确保只有特定命名空间下的服务才能连接到 Prometheus。 --- #### Grafana 默认管理员密码管理 对于提到的 Grafana,默认管理员账户 (`admin`) 密码可通过如下命令提取并更改初始值: ```bash kubectl get secret --namespace prometheus prometheus-grafana -o jsonpath="{.data.admin-password}" | base64 --decode; echo ``` 随后登录至 Grafana 并立即重设密码以防泄露风险。 这一操作流程直接来源于所提供的参考资料。 --- #### 总结 综合运用 Basic AuthTLS 加密以及 K8s 网络隔离技术,可显著提升 Prometheus 的安全性水平。同时定期审查日志记录以便及时发现潜在威胁活动。 ```python import requests response = requests.get('https://your-prometheus-instance/api', auth=('username', 'password'), verify=True) print(response.status_code) ``` 上述 Python 脚本展示了如何利用请求库配合 HTTPS 访问已加固的服务端点实例。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值