获取 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 端口

扫描二维码关注公众号,回复: 7852032 查看本文章
名称: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/

猜你喜欢

转载自www.cnblogs.com/weavepub/p/11856180.html
今日推荐