items: [{describedObject: {kind: Pod
部署好了 kube-prometheus 与 k8s-prometheus-adapter (详见之前的博文 k8s 安置 prometheus 过程记录),使用下面的配置文件部署 HPA(Horizontal Pod Autoscaling) 却掉败。
apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata: name: blog-web spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: blog-web minReplicas: 2 maxReplicas: 12 metrics: - type: Pods pods: metric: name: http_requests target: type: AverageValue averageValue: 100错误信息如下:
unable to get metric http_requests: unable to fetch metrics from custom metrics API: the server could not find the metric http_requests for pods通过下面的命令检察 custom.metrics.k8s.io api 撑持的 http_requests(每秒请求数QPS)监控指标:
$kubectl get --raw /apis/custom.metrics.k8s.io/v1beta1/ | jq . | egrep pods/.*http_requests "name": "pods/alertmanager_http_requests_in_flight", "name": "pods/prometheus_http_requests"发明只有 prometheus_http_requests 指标 ,没有所需的 http_requests 开头的指标。
打开 prometheus 控制台,发明 /service-discovery 中没有呈现我们想监控的应用 blog-web ,网上查找资料后知道了需要部署 ServiceMonitor 让 prometheus 发明所监控的 service 。
添加下面的 ServiceMonitor 配置文件:
kind: ServiceMonitor apiVersion: monitoring.coreos.com/v1 metadata: name: blog-web-monitor labels: app: blog-web-monitor spec: selector: matchLabels: app: blog-web endpoints: - port: http部署后还是没有被 prometheus 发明,,检察 prometheus 的日志发明下面的错误:
Failed to list *v1.Pod: pods is forbidden: User \"system:serviceaccount:monitoring:prometheus-k8s\" cannot list resource \"pods\" in API group \"\" at the cluster scope在园子里的博文 PrometheusOperator处事自动发明-监控redis样例 中找到了解决要领,将 prometheus-clusterRole.yaml 改为下面的配置:
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: prometheus-k8s rules: - apiGroups: - "" resources: - nodes - services - endpoints - pods - nodes/proxy verbs: - get - list - watch - apiGroups: - "" resources: - configmaps - nodes/metrics verbs: - get - nonResourceURLs: - /metrics verbs: - get从头部署即可
kubectl apply -f prometheus-clusterRole.yaml注1:如果给与上面的要领还是没被发明,需要强制刷新 prometheus 的配置,参考 部署 ServiceMonitor 之后如何让 Prometheus 当即发明 。
注2:也可以将 prometheus 配置为自动发明 service 与 pod ,参考园子里的博文 prometheus配置pod和svc的自动发明和监控 与 PrometheusOperator处事自动发明-监控redis样例 。
但是这时还有问题,虽然 service 被 prometheus 发明了,但 service 所对应的 pod 一个都没被发明。
production/blog-web-monitor/0 (0/19 active targets)排查后发明是因为 ServiceMonitor 与 Service 配置不同错误应,Service 配置文件中缺少 ServiceMonitor 配置中 matchLabels 所对应的 label ,ServiceMonitor 中的 port 没有对应 Service 中的 ports 配置,修正后的配置如下:
service-blog-web.yaml
servicemonitor-blog-web.yaml
kind: ServiceMonitor apiVersion: monitoring.coreos.com/v1 metadata: name: blog-web-monitor labels: app: blog-web spec: selector: matchLabels: app: blog-web endpoints: - port: http-blog-web用修正后的配置部署后,pod 终于被发明了:
production/blog-web-monitor/0 (0/5 up)但是这些 pod 全部处于 down 状态。
Endpoint State Scrape Duration Error :80/metrics DOWN server returned HTTP status 400 Bad Request通过园子里的博文 使用Kubernetes演示金丝雀颁布 知道了本来需要应用本身供给 metrics 监控指标数据让 prometheus 抓取。
标准Tomcat自带的应用没有/metrics这个路径,prometheus获取不到它能识另外格局数据,而指标数据就是从/metrics这里获取的。所以我们使用标准Tomcat不行或者你就算有这个/metrics这个路径,但是返回的格局不切合prometheus的规范也是不行的。
我们的应用是用 ASP.NET Core 开发的,所以选用了 prometheus-net ,由它供给 metrics 数据给 prometheus 抓取。
安置 nuget 包
dotnet add package prometheus-net.AspNetCore添加 HttpMetrics 中间件
app.UseRouting(); app.UseHttpMetrics();添加 MapMetric 路由
app.UseEndpoints(endpoints => { endpoints.MapMetrics(); };当通过下面的命令确认通过 /metrics 路径可以获取监控数据时,
$ docker exec -t $(docker ps -f name=blog-web_blog-web -q | head -1) curl 127.0.0.1/metrics | grep http_request_duration_seconds_sum http_request_duration_seconds_sum{code="200",method="GET",controller="AggSite",action="SiteHome"} 0.44973779999999997 http_request_duration_seconds_sum{code="200",method="GET",controller="",action=""} 0.0631272Prometheus 控制台 /targets 页面就能看到 blog-web 对应的 pod 都处于 up 状态。
production/blog-web-monitor/0 (5/5 up)温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31393.html
- 上一篇:不需要web服务器进行重启生效
- 下一篇:上传文件.py