一、序言
本篇將介紹如何使用數字證書為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/
prometheus.crt prometheus.key
三、配置Promethus
認證也是這個文件,認證操作指導:https://prometheus.io/docs/guides/basic-auth
1. 創建web-config.yml 文件配置證書
tls_server_config:cert_file: /root/certificate/prometheus.crtkey_file: /root/certificate/prometheus.key
2. 修改prometheus.yml文件
scrape_configs:- job_name: "node"metrics_path: "/metrics"scheme: "https" # 協議這里需要選擇httpstls_config:ca_file: /root/certificate/prometheus.crtinsecure_skip_verify: truestatic_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
% Total % Received % Xferd Average Speed Time Time Time CurrentDload 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
% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed
100 68 100 68 0 0 3944 0 --:--:-- --:--:-- --:--:-- 4250
{"status": "success","data": ["node","prometheus","promethus","test"]
}