使用metrics-server监控k8s的资源指标

使用,metrics,server,监控,k8s,资源,指标 · 浏览次数 : 308

小编点评

## Generate Content **1. Start with Header** ```yaml spec: containers: - args: ``` **2. Add Arguments for Container** - `--cert-dir=/tmp` - `--secure-port=4443` - `--kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname` - `--kubelet-use-node-status-port` - `--metric-resolution=15s` - `--kubelet-insecure-tls` - `--kubelet-insecure-tls` - `--kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname` **3. Add Arguments for Kubernetes Deployment** - `--kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname` - `--kubelet-use-node-status-port` - `--kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname` **4. Add Arguments for Metrics Resolution** - `--kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname` - `--kubelet-use-node-status-port` - `--kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname` **5. End with YAML Header** ```yaml spec: containers: - args: ``` **6. Generate Content** Now generate content based on the arguments provided for each container, Kubernetes deployment, and metrics resolution. **Note:** This content includes placeholder values for specific information. Please replace them with actual values for your deployment.

正文

首先,欢迎使用DHorse部署k8s应用。

k8s可以通过top命令来查询pod和node的资源使用情况,如果直接运行该命令,如下所示。

[root@centos05 deployment]# kubectl top pod
W0306 15:23:24.990550    8247 top_pod.go:140] Using json format to get metrics. Next release will switch to protocol-buffers, switch early by passing --use-protocol-buffers flag
error: Metrics API not available

top命令依赖于metrics server,而k8s默认未安装该组件,下面详细介绍使用过程。

安装过程

  1. 下载部署文件

下载components.yaml文件

  1. 修改镜像地址

将部署文件中镜像地址修改为国内的地址,大概在部署文件的第140行。
原配置是:

image: k8s.gcr.io/metrics-server/metrics-server:v0.6.2

修改后的配置是:

image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.6.2
  1. 部署metrics server
[root@centos05 deployment]# kubectl create -f components.yaml
serviceaccount/metrics-server created
clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created
clusterrole.rbac.authorization.k8s.io/system:metrics-server created
rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created
clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created
clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created
service/metrics-server created
deployment.apps/metrics-server created
apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created

查看metric server的运行情况,发现探针问题:Readiness probe failed: HTTP probe failed with statuscode: 500

[root@centos05 deployment]# kubectl get pods -n kube-system | grep metrics
kube-system   metrics-server-6ffc8966f5-84hbb      0/1     Running   0              2m23s
[root@centos05 deployment]# kubectl describe pod metrics-server-6ffc8966f5-84hbb -n kube-system

进而查看pod的日志:

[root@centos05 deployment]# kubectl logs metrics-server-6ffc8966f5-84hbb -n kube-system 
I1010 16:27:46.228594       1 serving.go:342] Generated self-signed cert (/tmp/apiserver.crt, /tmp/apiserver.key)
I1010 16:27:46.633494       1 secure_serving.go:266] Serving securely on [::]:4443
I1010 16:27:46.633585       1 requestheader_controller.go:169] Starting RequestHeaderAuthRequestController
I1010 16:27:46.633616       1 shared_informer.go:240] Waiting for caches to sync for RequestHeaderAuthRequestController
I1010 16:27:46.633653       1 dynamic_serving_content.go:131] "Starting controller" name="serving-cert::/tmp/apiserver.crt::/tmp/apiserver.key"
I1010 16:27:46.634221       1 tlsconfig.go:240] "Starting DynamicServingCertificateController"
W1010 16:27:46.634296       1 shared_informer.go:372] The sharedIndexInformer has started, run more than once is not allowed
I1010 16:27:46.634365       1 configmap_cafile_content.go:201] "Starting controller" name="client-ca::kube-system::extension-apiserver-authentication::requestheader-client-ca-file"
I1010 16:27:46.634370       1 shared_informer.go:240] Waiting for caches to sync for client-ca::kube-system::extension-apiserver-authentication::requestheader-client-ca-file
I1010 16:27:46.634409       1 configmap_cafile_content.go:201] "Starting controller" name="client-ca::kube-system::extension-apiserver-authentication::client-ca-file"
I1010 16:27:46.634415       1 shared_informer.go:240] Waiting for caches to sync for client-ca::kube-system::extension-apiserver-authentication::client-ca-file
E1010 16:27:46.641663       1 scraper.go:140] "Failed to scrape node" err="Get \"https://192.168.100.22:10250/metrics/resource\": x509: cannot validate certificate for 192.168.100.22 because it doesn't contain any IP SANs" node="k8s-slave2"
E1010 16:27:46.645389       1 scraper.go:140] "Failed to scrape node" err="Get \"https://192.168.100.20:10250/metrics/resource\": x509: cannot validate certificate for 192.168.100.20 because it doesn't contain any IP SANs" node="k8s-master"
E1010 16:27:46.652261       1 scraper.go:140] "Failed to scrape node" err="Get \"https://192.168.100.21:10250/metrics/resource\": x509: cannot validate certificate for 192.168.100.21 because it doesn't contain any IP SANs" node="k8s-slave1"
I1010 16:27:46.733747       1 shared_informer.go:247] Caches are synced for RequestHeaderAuthRequestController 
I1010 16:27:46.735167       1 shared_informer.go:247] Caches are synced for client-ca::kube-system::extension-apiserver-authentication::client-ca-file 
I1010 16:27:46.735194       1 shared_informer.go:247] Caches are synced for client-ca::kube-system::extension-apiserver-authentication::requestheader-client-ca-file 
E1010 16:28:01.643646       1 scraper.go:140] "Failed to scrape node" err="Get \"https://192.168.100.22:10250/metrics/resource\": x509: cannot validate certificate for 192.168.100.22 because it doesn't contain any IP SANs" node="k8s-slave2"
E1010 16:28:01.643805       1 scraper.go:140] "Failed to scrape node" err="Get \"https://192.168.100.21:10250/metrics/resource\": x509: cannot validate certificate for 192.168.100.21 because it doesn't contain any IP SANs" node="k8s-slave1"
E1010 16:28:01.646721       1 scraper.go:140] "Failed to scrape node" err="Get \"https://192.168.100.20:10250/metrics/resource\": x509: cannot validate certificate for 192.168.100.20 because it doesn't contain any IP SANs" node="k8s-master"
I1010 16:28:13.397373       1 server.go:187] "Failed probe" probe="metric-storage-ready" err="no metrics to serve"

可以确定pod异常是因为:Readiness Probe 探针检测到 Metris 容器启动后对 http Get 探针存活没反应,具体原因是:cannot validate certificate for 192.168.100.22 because it doesn't contain any IP SANs" node="k8s-slave2"

查看 metrics-server 的文档(https://github.com/kubernetes...),有如下一段说明:

Kubelet certificate needs to be signed by cluster Certificate Authority (or disable certificate validation by passing
--kubelet-insecure-tls to Metrics Server)
意思是:kubelet 证书需要由集群证书颁发机构签名(或者通过向 Metrics Server 传递参数 --kubelet-insecure-tls 来禁用证书验证)。
由于是测试环境,我们选择使用参数禁用证书验证,生产环境不推荐这样做!!!

在大概 139 行的位置追加参数:--kubelet-insecure-tls,修改后内容如下:

spec:
  containers:
  - args:
	- --cert-dir=/tmp
	- --secure-port=4443
	- --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
	- --kubelet-use-node-status-port
	- --metric-resolution=15s
	- --kubelet-insecure-tls

再次部署文件:

[root@centos05 deployment]# kubectl apply -f components.yaml

查看pod已经正常运行:

[root@centos05 deployment]# kubectl get pod -A | grep metrics
kube-system   metrics-server-fd9598766-8zphn       1/1     Running   0              89s

执行kubectl top命令成功:

[root@centos05 deployment]# kubectl top pod
NAME                                CPU(cores)   MEMORY(bytes)   
hello-1-qa-dhorse-6fc54647c-5zkjc   501m         133Mi 
[root@centos05 deployment]# kubectl top node
NAME       CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%     
centos05   192m         4%     1610Mi          59%         
centos06   107m         2%     854Mi           50%  

也可以通过代码方式获取资源指标,如:

public PodMetricsList replicaMetrics(ClusterPO clusterPO, String namespace) {
	ApiClient apiClient = this.apiClient(clusterPO.getClusterUrl(), clusterPO.getAuthToken());
	Metrics metrics = new Metrics(apiClient);
	try {
		return metrics.getPodMetrics(namespace);
	} catch (ApiException e) {
		logger.error("Failed to list pod metrics", e);
	}
	return null;
}

同时,欢迎使用DHorse进行部署、监控应用。

与使用metrics-server监控k8s的资源指标相似的内容:

使用metrics-server监控k8s的资源指标

首先,欢迎使用DHorse部署k8s应用。 k8s可以通过top命令来查询pod和node的资源使用情况,如果直接运行该命令,如下所示。 [root@centos05 deployment]# kubectl top pod W0306 15:23:24.990550 8247 top_pod.go

使用jmx exporter采集kafka指标

预置条件 安装kafka、prometheus 使用JMX exporter暴露指标 下载jmx exporter以及配置文件。Jmx exporter中包含了kafka各个组件的指标,如server metrics、producer metrics、consumer metrics等,但这些指标并

Grafana 系列-统一展示-3-Prometheus 仪表板

系列文章 Grafana 系列文章 知识储备 Prometheus Template Variables 你可以使用变量来代替硬编码的细节,如 server、app 和 pod_name 在 metric 查询中。Grafana 在仪表盘顶部的下拉选择框中列出这些变量,帮助你改变仪表盘中显示的数据。

.NET 使用 OpenTelemetry metrics 监控应用程序指标

上一次我们讲了 OpenTelemetry Logs 与 OpenTelemetry Traces。今天继续来说说 OpenTelemetry Metrics。 随着现代应用程序的复杂性不断增加,对于性能监控和故障排除的需求也日益迫切。在 .NET 生态系统中,OpenTelemetry Metri

使用 Grafana 统一监控展示-对接 Zabbix

概述 在某些情况下,Metrics 监控的 2 大顶流: Zabbix: 用于非容器的虚拟机环境 Prometheus: 用于容器的云原生环境 是共存的。但是在这种情况下,统一监控展示就不太方便,本文介绍利用 Grafana 对接 Zabbix, 来作为统一监控展示端。Let's go! 在这里,主

使用评价指标工具

评估一个训练好的模型需要评估指标,比如正确率、查准率、查全率、F1值等。当然不同的任务类型有着不同的评估指标,而HuggingFace提供了统一的评价指标工具。 1.列出可用的评价指标 通过list_metrics()函数列出可用的评价指标: def list_metric_test(): # 第4

机器学习策略:详解什么时候该改变开发/测试集和指标?(When to change dev/test sets and metrics)

什么时候该改变开发/测试集和指标? 有时候在项目进行途中,可能意识到,目标的位置放错了。这种情况下,应该移动的目标。 来看一个例子,假设在构建一个猫分类器,试图找到很多猫的照片,向的爱猫人士用户展示,决定使用的指标是分类错误率。所以算法\(A\)和\(B\)分别有3%错误率和5%错误率,所以算法\(

[转帖]TiDB Lightning 监控告警

https://docs.pingcap.com/zh/tidb/v6.5/monitor-tidb-lightning tidb-lightning 支持使用 Prometheus 采集监控指标 (metrics)。本文主要介绍 TiDB Lightning 的监控配置与监控指标。 监控配置 如果

[转帖]TiDB 数据库统计表的大小方法

简介:TiDB统计表的大小,列出了一些方法: 1、第一种的统计方式: 基于统计表 METRICS_SCHEMA.store_size_amplification 要预估 TiDB 中一张表的大小,你可以参考使用以下查询语句: SELECT db_name, table_name, ROUND(SUM

使用 OpenTelemetry 构建 .NET 应用可观测性(4):ASP.NET Core 应用中集成 OTel

目录前言使用 elastic 构建可观测性平台在 ASP.NET Core 应用中集成 OTel SDK安装依赖基础配置Instrumentation 配置创建自定义 Span 和 Metric完整的代码演示kibana 中查看数据TracingMetricsTracing 和 Logs 的关联 前