为 Promethus 配置https访问

一、序言

本篇将介绍如何使用数字证书为Promethus 访问提供加密功能,由于是实验环境证书由openssl生成,操作指南来自官网手册:https://prometheus.io/docs/guides/tls-encryption/

在生产环境中prometheus可能会放在后端,证书一般配置在前端。

二、生成ssl证书

复制代码
openssl req \
  -x509 \
  -newkey rsa:4096 \
  -nodes \
  -keyout prometheus.key \
  -out prometheus.crt \
  -subj "/CN=192.168.25.225"
  • -subj "/CN=192.168.25.225" : 指定服务器地址或者域名

查看证书文件:

复制代码
ls /root/certificate/
clike 复制代码
prometheus.crt  prometheus.key

三、配置Promethus

认证也是这个文件,认证操作指导:https://prometheus.io/docs/guides/basic-auth

1. 创建web-config.yml 文件配置证书

clike 复制代码
tls_server_config:
  cert_file: /root/certificate/prometheus.crt
  key_file: /root/certificate/prometheus.key

2. 修改prometheus.yml文件

复制代码
scrape_configs:
  - job_name: "node"
    metrics_path: "/metrics"
    scheme: "https"		# 协议这里需要选择https
    tls_config:
      ca_file: /root/certificate/prometheus.crt
      insecure_skip_verify: true
    static_configs:
    - targets: ['localhost:9090']

添加tls_config配置:

  • ca_file:指定公钥位置
  • insecure_skip_verify: 禁用服务器对证书验证(因为是自建证书所以必须开启)

3. Prometheus 启动时指定web-config.yml配置文件

复制代码
./prometheus \
  --config.file=./prometheus.yml \
  --web.config.file=./web-config.yml 

4. 使用https访问Prometheus

复制代码
curl --cacert /root/certificate/prometheus.crt  https://192.168.25.225:9090/api/v1/label/job/values | jq
clike 复制代码
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    68  100    68    0     0   4008      0 --:--:-- --:--:-- --:--:--  4533
{
  "status": "success",
  "data": [
    "node",
    "prometheus",
    "promethus",
    "test"
  ]
}

或者跳过证书:

复制代码
curl -k  https://192.168.25.225:9090/api/v1/label/job/values | jq
clike 复制代码
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    68  100    68    0     0   3944      0 --:--:-- --:--:-- --:--:--  4250
{
  "status": "success",
  "data": [
    "node",
    "prometheus",
    "promethus",
    "test"
  ]
}
相关推荐
郝亚军13 小时前
根据Sec-WebSocket-Key计算Sec-WebSocket-Accept
网络·websocket·网络协议
Kaede614 小时前
深入探索 WebSocket:构建实时应用的核心技术
网络·websocket·网络协议
web前端神器18 小时前
记录一下node后端写下载https的文件报错,而浏览器却可以下载。
网络协议·http·https
少陽君18 小时前
什么是CA根证书
服务器·https·ssl
三项超标18 小时前
AI入坑: Trae 通过http调用.net 开发的 mcp server
网络·网络协议·http
2501_9159090618 小时前
uni-app iOS 上架常见问题与解决方案,实战经验全解析
android·ios·小程序·https·uni-app·iphone·webview
云霄IT18 小时前
CentOS7安装Nginx服务——为你的网站配置https协议和自定义服务端口
服务器·nginx·https
绵绵细雨中的乡音19 小时前
HTTP 协议详解
网络·网络协议·http
耐达讯通信技术19 小时前
惊爆!耐达讯自动化RS485转Profinet,电机连接的“逆天神器”?
运维·网络·人工智能·科技·网络协议·自动化
JJ1M821 小时前
http问题汇总
网络·网络协议·http