> For the complete documentation index, see [llms.txt](https://bohans.gitbook.io/devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bohans.gitbook.io/devops/kubernetes/ji-chu-zhi-shi/an-zhuang/ji-qun/ji-qun-pei-zhi/metrics-bu-shu.md).

# Metrics 部署

在新版的 Kubernetes 中，系统资源的采集使用 Metrics-server，可以通过 Metrics 采集节点和 Pod 的内存、磁盘、CPU 和网络的使用率。

### 将 Master01 节点的 front-proxy-ca.crt 复制到所有 Node 节点

<pre class="language-bash" data-overflow="wrap"><code class="lang-bash"><strong>for i in k8s-node01 k8s-node02; \
</strong>do scp /etc/kubernetes/pki/front-proxy-ca.crt root@$i:/etc/kubernetes/pki/front-proxy-ca.crt; \
done
</code></pre>

### 在 Master01 节点安装 metrics server

（Optional）如果没有下载 k8s-ha-install，先从 gitee 上克隆它：

{% code overflow="wrap" %}

```bash
git clone https://gitee.com/zhang993632987/k8s-ha-install.git -b manual-installation-v1.27.x
```

{% endcode %}

安装 metrics server：

```bash
cd /root/k8s-ha-install/metrics-server
```

<details>

<summary><mark style="color:purple;">comp.yaml存在一点问题，需要先修复才能正常使用</mark></summary>

在分支中 manual-installation-v1.27.x 中的文件配置存在一点问题，需要修改后才能正常安装。下面展示了修改相关的片段，并用“#” 将修改的部分标示了出来：

{% code overflow="wrap" %}

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    k8s-app: metrics-server
  name: metrics-server
  namespace: kube-system
spec:
  selector:
    matchLabels:
      k8s-app: metrics-server
  strategy:
    rollingUpdate:
      maxUnavailable: 0
  template:
    metadata:
      labels:
        k8s-app: metrics-server
    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
        - --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.crt             # change to front-proxy-ca.crt
        - --requestheader-username-headers=X-Remote-User
        - --requestheader-group-headers=X-Remote-Group
        - --requestheader-extra-headers-prefix=X-Remote-Extra-
        image: registry.cn-beijing.aliyuncs.com/dotbalo/metrics-server:v0.6.3
        imagePullPolicy: IfNotPresent
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /livez
            port: https
            scheme: HTTPS
          periodSeconds: 10
        name: metrics-server
        ports:
        - containerPort: 4443
          name: https
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /readyz
            port: https
            scheme: HTTPS
          initialDelaySeconds: 20
          periodSeconds: 10
        resources:
          requests:
            cpu: 100m
            memory: 200Mi
        securityContext:
          allowPrivilegeEscalation: false
          readOnlyRootFilesystem: true
          runAsNonRoot: true
          runAsUser: 1000
        volumeMounts:
        - mountPath: /tmp
          name: tmp-dir
        - mountPath: /etc/kubernetes/pki             # 删除/front-proxy-ca.pem 
          name: pki
      nodeSelector:
        kubernetes.io/os: linux
      priorityClassName: system-cluster-critical
      serviceAccountName: metrics-server
      volumes:
      - emptyDir: {}
        name: tmp-dir
      - hostPath:
          path: /etc/kubernetes/pki                 # 删除/front-proxy-ca.pem 
        name: pki
```

{% endcode %}

</details>

```properties
kubectl create -f comp.yaml
```

### 检查安装情况

查看 metrics-server Pod 状态：

```properties
kubectl get po -n kube-system -l k8s-app=metrics-server
```

待 Pod 变成 1/1 Running 后，等待几分钟，即可查看节点和 Pod 资源的使用率：

```properties
# kubectl top node
NAME           CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
k8s-master01   263m         13%    1759Mi          22%
k8s-node01     87m          4%     945Mi           12%
k8s-node02     79m          3%     590Mi           7%
```

```properties
# kubectl top po -A
NAMESPACE              NAME                                        CPU(cores)   MEMORY(bytes)   
kube-system            calico-kube-controllers-59fb99f948-5xl52    5m           21Mi
kube-system            calico-node-5g6vw                           33m          122Mi
kube-system            calico-node-dnd5f                           36m          131Mi
kube-system            calico-node-nfvqw                           42m          117Mi
kube-system            calico-typha-7877bbb7fd-8nqp9               3m           34Mi
kube-system            coredns-65dcc469f7-cmpg6                    2m           18Mi
kube-system            coredns-65dcc469f7-cqdlb                    2m           17Mi
kube-system            etcd-k8s-master01                           31m          72Mi
kube-system            kube-apiserver-k8s-master01                 56m          466Mi
kube-system            kube-controller-manager-k8s-master01        25m          63Mi
kube-system            kube-proxy-59xjt                            1m           20Mi
kube-system            kube-proxy-9m89q                            1m           18Mi
kube-system            kube-proxy-z7hwz                            1m           24Mi
kube-system            kube-scheduler-k8s-master01                 4m           21Mi
kube-system            metrics-server-7775d96dc7-cfjjz             3m           22Mi
kubernetes-dashboard   dashboard-metrics-scraper-54b649975-bbt7w   1m           11Mi
kubernetes-dashboard   kubernetes-dashboard-67d8bbc569-z5xxj       1m           14Mi
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bohans.gitbook.io/devops/kubernetes/ji-chu-zhi-shi/an-zhuang/ji-qun/ji-qun-pei-zhi/metrics-bu-shu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
