如何逐步安装 Kubernetes（k8s）指标服务器
Kubernetes
![](/data/attachment/album/202304/12/100919k6p6yyweu6nv6nkh.jpg)



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


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


指标服务器收集资源利用率数据，例如集群中节点和容器荚的 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

```

![](/data/attachment/album/202304/12/101039va87ihh1ha16646z.jpg)


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



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

```

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


接下来，你需要修改指标服务器的 yaml 文件以设置一些配置选项：



```
# vi components.yaml

```

找到 `container` 下的 `args` 部分，添加以下行：



```
- --kubelet-insecure-tls

```

在 `spec` 下，添加以下参数：



```
hostNetwork: true

```

![](/data/attachment/album/202304/12/101048y0se3ecsbqhemmpm.jpg)


保存并关闭文件。


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


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



```
# kubectl apply -f components.yaml

```

![](/data/attachment/album/202304/12/101055vtt8td3sz8tyutkd.jpg)


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


部署指标服务器后，通过检查在 `kube-system` 命名空间中运行的容器荚状态来验证它的状态：



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

```

![](/data/attachment/album/202304/12/101100nq1kksekqswdssvg.jpg)


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


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


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



```
# kubectl top nodes

```

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


![](/data/attachment/album/202304/12/101108phib7dodd9uedoeb.jpg)


要查看当前命名空间或特定命名空间的容器荚资源利用率，请运行：



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

```

![](/data/attachment/album/202304/12/101114e7wr8n7kn1mnobr7.jpg)


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


*（题图：MJ: Kubernetes container paper art light blue background ultra-detailed topview）*




---


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


作者：[Pradeep Kumar](https://www.linuxtechi.com/author/pradeep/) 选题：[lkxed](https://github.com/lkxed/) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


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


OK
In this post, we will cover to how to install Kubernetes metrics server step by step.

The [Kubernetes(k8s) Metrics Server](https://github.com/kubernetes-sigs/metrics-server) is a component that collects and aggregates metrics data from various sources in the Kubernetes cluster, including nodes and pods. This data can be used to monitor and optimize resource utilization, identify potential issues, and improve the overall performance of your Kubernetes cluster.

Metrics Server collects resource utilization data such as CPU and memory usage for nodes and pods in the cluster. It provides an API endpoint that can be used to query this data and retrieve metrics for specific resources in the cluster.

#### Prerequisites

- A Kubernetes cluster (v1.21 or high) up and running.
- kubectl command line tool installed and configured to interact to your Kubernetes cluster.
- The ability to create and modify Kubernetes objects.

Without any further delay, let’s deep dive into the installation steps.

## Step 1) Download Metrics Server Manifest

The first step is to download the latest Metrics Server manifest file from the Kubernetes GitHub repository. Use below curl command to download yaml file,

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

If you are planning to install metrics server in high availability mode then download following manifest file.

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

## Step 2) Modify Metrics Server Yaml File

Next, you need to modify the Metrics Server yaml file to set some configuration options,

# vi components.yaml

Find the args section under the container section, add the following line:

- --kubelet-insecure-tls

Under the spec section, add following parameter,

hostNetwork: true

Save and close the file.

## Step 3) Deploy Metrics Server

Now, we are all set to deploy metrics server, run following kubectl command,

# kubectl apply -f components.yaml

## Step 4) Verify Metrics Server Deployment

After deploying the Metrics Server, verify it’s status by checking the pods status which is running in the kube-system namespace,

# kubectl get pods -n kube-system

Output above confirms that metrics-server pod is up and running.

## Step 5) Test Metrics Server Installation

Finally, you can test the metrics server by running following kubectl command,

# kubectl top nodes

This command should display the resource utilization for each node in your cluster, including CPU and memory usage.

To view pods resource utilization of your current namespace or specific namespace, run

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

That’s all from this post, I have hope you found it informative. Please do post your feedback and queries in below comments section.

Also Read: [How to Install and Use Helm in Kubernetes](https://www.linuxtechi.com/install-use-helm-in-kubernetes/)

AlexReally helpful.

Precise and steightforward.

Thank you.

moran kamielPERFECT user guide.

thanks!

RameshPerfect. Works fine on my self hosted kubernetes running on Ubuntu 22.04.

Rovshan KalanovEven this command “kubectl get pods -n kube-system” shows metrics-server as Ready, but it is still showing “error: Metrics API not available”