[#]: subject: "How to Install Kubernetes (K8s) Metrics Server Step by Step"
[#]: via: "https://www.linuxtechi.com/how-to-install-kubernetes-metrics-server/"
[#]: author: "Pradeep Kumar https://www.linuxtechi.com/author/pradeep/"
[#]: collector: "lkxed"
[#]: translator: "geekpi"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "

如何逐步安装 Kubernetes (K8s) 指标服务器
======

在这篇文章中,我们将逐步介绍如何安装 Kubernetes 指标服务器。

Kubernetes(k8s) 指标服务器是一个组件,用于收集和聚合来自 Kubernetes 集群中各种来源(包括节点和 Pod)的指标数据。此数据可用于监控和优化资源利用率、识别潜在问题并提高 Kubernetes 集群的整体性能。

指标服务器收集资源利用率数据,例如集群中节点和 Pod 的 CPU 和内存使用情况。它提供了一个 API 端点,可用于查询此数据并检索集群中特定资源的指标。

##### 先决条件

- 启动并运行 Kubernetes 集群(v1.21 或更高版本)。
- kubectl 命令行工具已安装并配置为与你的 Kubernetes 集群交互。
- 创建和修改 Kubernetes 对象的能力。

事不宜迟,让我们深入了解安装步骤。

### 步骤 1) 下载指标服务器清单

第一步是从 Kubernetes GitHub 仓库下载最新的指标服务器清单文件。使用下面的 curl 命令下载 yaml 文件:

```
# curl -LO https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
```

![][1]

如果你计划在高可用性模式下安装指标服务器,请下载以下清单文件:

```
# curl https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/high-availability-1.21+.yaml
```

### 步骤 2)修改指标服务器 Yaml 文件

接下来,你需要修改 Metrics Server yaml 文件以设置一些配置选项:

```
# vi components.yaml
```

找到 container 下的 args 部分,添加以下行:

```
- --kubelet-insecure-tls
```

在 spec 下,添加以下参数:

```
hostNetwork: true
```

![][2]

保存并关闭文件。

### 步骤 3)部署指标服务器

现在,我们准备好部署指标服务器,运行以下 kubectl 命令:

```
# kubectl apply -f components.yaml
```

![][3]

### 步骤 4)验证指标服务器部署

部署指标服务器后,通过检查在 kube-system 命名空间中运行的 pod 状态来验证它的状态:

```
# kubectl get pods -n kube-system
```

![][4]

上面的输出确认指标服务器 pod 已启动并正在运行。

### 步骤 5)测试指标服务器安装

最后,你可以通过运行以下 kubectl 命令来测试指标服务器:

```
# kubectl top nodes
```

此命令应显示集群中每个节点的资源利用率,包括 CPU 和内存使用率。

![][5]

要查看当前命名空间或特定命名空间的 pod 资源利用率,请运行:

```
# kubectl top pod
# kubectl top pod -n kube-system
```

![][6]

这就是这篇文章的全部内容,我希望你能从中找到有用的信息。请在下面的评论部分发表你的反馈和疑问。

--------------------------------------------------------------------------------

via: https://www.linuxtechi.com/how-to-install-kubernetes-metrics-server/

作者:[Pradeep Kumar][a]
选题:[lkxed][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)

本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出

[a]: https://www.linuxtechi.com/author/pradeep/
[b]: https://github.com/lkxed/
[1]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Download-Metrics-Server-Yaml-file-Curl-Command-1024x127.png?ezimgfmt=ng:webp/ngcb22
[2]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Edit-Metrics-Server-Yaml-file.png
[3]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Deploy-Metrics-Server-Kubectl-Command.png
[4]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Metrics-Server-Pods-Status-Kube-System.png
[5]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Kubectl-top-node-command-Output.png?ezimgfmt=ng:webp/ngcb22
[6]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Kubectl-top-pod-command-output.png