二、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)]

### 回答1: Prometheus 自带的 Basic Authentication 可以通过在 Prometheus 配置文件中设置用户名密码来实现登录认证。对于密码,通常采用密文来存储。以下是一个示例配置: ``` # Prometheus 配置文件 global: scrape_interval: 15s evaluation_interval: 15s scrape_configs: - job_name: prometheus static_configs: - targets: ['localhost:9090'] basic_auth: username: "prometheus" password: "$2y$05$Hq3gW0xvK2B0uV7Kl8gRkuO7EfA5KcwBkpGtE5V5.5.VRh9x7bK8y" ``` 在上面的示例中,我们设置了用户名为 "prometheus",密码为一个密文。请注意,密文是加密后的密码,不是明文。在生产环境中,建议采用安全的密码加密方式(例如 bcrypt)来存储密码。 ### 回答2: 在使用Prometheus自带的Basic Authentication进行登录认证时,用户名密码是以密文的形式进行设置的。下面是一个登录认证示例。 首先,我们需要在Prometheus配置文件中启用Basic Authentication功能,并设置用户名密码。可以通过编辑prometheus.yml文件,在其中添加以下内容: ``` ... basic_auth_users: - username: "密文用户名" password_hash: "密文密码" ... ``` 请注意,密文用户名和密文密码都需要进行相应的加密处理。 要设置密文用户名密码,可以使用htpasswd命令行工具进行加密。首先,确保已安装该工具。然后,运行以下命令: ``` $ htpasswd -nbB "用户名" "密码" ``` 这将生成一个密文字符串。将该密文字符串分别填入prometheus.yml文件的basic_auth_users部分的username和password_hash字段中。 保存并关闭prometheus.yml文件后,重启Prometheus服务器以使配置生效。 现在,当访问Prometheus时,会要求进行身份验证。可以在浏览器或使用HTTP客户端发送请求时,将用户名密码作为HTTP标头的Authorization字段进行传递,格式为"Basic base64(username:password)"。 以下是使用curl命令进行登录认证的示例: ``` $ curl -H "Authorization: Basic base64(用户名:密码)" http://localhost:9090/ ``` 在示例中,将"用户名"和"密码"替换为您自己的密文用户名密码。 如此配置后,只有提供正确的用户名密码才能成功访问Prometheus。这提供了一种加强安全性和保护数据的方法。 ### 回答3: 在使用Prometheus自带的Basic Authentication进行登录认证时,可以通过以下方式设置用户名密码为密文。 首先,我们需要在Prometheus的配置文件中进行修改。打开Prometheus的配置文件(通常命名为prometheus.yml),找到并添加以下配置: ``` basic_auth_users: - username: "密文用户名" password: "密文密码" ``` 在上述配置中,"密文用户名"是你想要设定的用户名的密文形式,"密文密码"是你想要设定的密码的密文形式。 在实际应用中,我们可以使用各种加密算法将用户名密码进行加密,例如使用SHA256算法,可以通过以下方法生成密文: 1. 打开终端或命令提示符。 2. 输入以下命令: ``` echo -n "用户名" | sha256sum ``` 3. 将生成的密文复制并替换配置文件中的"密文用户名"。 同样地,对于密码也可以使用相同的方法生成密文并替换配置文件中的"密文密码"。 完成以上步骤后,保存并重新启动Prometheus服务。此时,Prometheus将会使用我们设定的密文进行Basic Authentication验证。 需要注意的是,密文形式的用户名密码只在配置文件中存储,而在用户登录时需要输入明文的用户名密码Prometheus会在登录验证时将明文的用户名密码进行加密,并与配置文件中的密文进行比对来实现登录认证。这样可以更有效地保护用户的登录信息。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值