獲取 Rancher 中 Prometheus 的數據


1、需求

在 rancher 應用商店添加集群監控,會安裝 prometheus、grafana;需要從 prometheus 的 api 中收集 pod 的一些信息。

查看grafana 配置的數據源為:http://prometheus-operated:9090

遂用 curl 請求如下:

curl http://prometheus-operated:9090/api/v1/query?query=container_cpu_usage_seconds_total
unauthorized

提示沒有授權。

 

2、查找原因

查看官網介紹了 basic auth 方式,采用 nginx 作為代理,配置了驗證信息。遂想着 rancher 是不是也配置了代理;查看rancher 信息:

應用商店添加集群監控 prometheus,會創建如下信息:

命名空間: cattle-prometheus,
工作負載:prometheus-cluster-monitoring,
工作負載中包含如下5個容器:
    prometheus                           #監聽9090端口
    prometheus-config-reloader    
    rules-configmap-reloader    
    prometheus-proxy                     #監聽8080端口
    prometheus-agent

查看到 prometheus-proxy 代理了9090 端口,並監聽8080端口,其中還配置了 Authorization

proxy_set_header Authorization "Bearer eyJhbGciOiJSL3j-89LHMtCQCzHrmk12uUP4SI425bxKJEg........";
proxy_pass_header Authorization;

這樣我們只需要請求 prometheus-cluster-monitoring 的 8080 端口即可,不用配置 Authorization 就可以訪問數據了。

 

3、解決

配置服務發現,暴露出 prometheus-cluster-monitoring 的 8080 端口

名稱:http-api
命名空間: cattle-prometheus 
解析到:Pod
標簽:
    app=prometheus
    chart=prometheus-0.0.1
    release=cluster-monitoring
類型:Headless Service
端口映射:
    端口名稱:8080
    服務端口:8080
    協議:tcp
    目標端口:8080

然后請求 http-api 

curl http://http-api:8080/api/v1/query?query=container_cpu_usage_seconds_total

可以看到返回了數據。

 

4、外部訪問

要想外部也想要訪問該服務,還需要配置負載均衡
1)域名 api-prometheus-operated.wmq.com 指向 http-api 服務的 8080 端口;

2)配置域名解析到 ingress;

3)獲取數據

curl http://api-prometheus-operated.wmq.com/api/v1/query?query=container_cpu_usage_seconds_total
{
    "status": "success",
    "data": {
        "resultType": "vector",
        "result": [
            {
                "metric": {
                    "__name__": "container_cpu_usage_seconds_total",
                    "container": "POD",
                    "endpoint": "https-metrics",
                    "instance": "172.16.5.74:10250",
                    "job": "expose-kubelets-metrics",
                    "namespace": "cattle-prometheus",
                    "node": "dev-k8s-master-server-01",
                    "pod": "exporter-node-cluster-monitoring-h8ph2",
                    "service": "expose-kubelets-metrics"
                },
                "value": [
                    1573700778.551,
                    "0.026998712"
                ]
            }
    }
}

 

 

參考:

官網介紹http api:https://prometheus.io/docs/prometheus/latest/querying/api/

官網介紹functions:https://prometheus.io/docs/prometheus/latest/querying/functions/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM