zl程序教程

您现在的位置是:首页 >  后端

当前栏目

分析 k8s docker 查看 内存 CPU使用率 k8s docker 性能 分析

k8sDocker内存性能CPU 分析 查看 使用率
2023-09-14 09:08:37 时间

分析 k8s 容器 内存 CPU使用率

安装metrics-server

参考github https://github.com/kubernetes-sigs/metrics-server
如下命令安装

mkdir metrics-server
wget https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
kubectl apply -f components.yaml

使用如下的命令分析所有容器

kubectl get po -o wide -A |awk '{if ($3 !~ "0|READY") {print $1,$2}}'|while read  ns pod;do kubectl top pod $pod -n $ns|grep -v NAME|awk '{printf  ("%-60s%20s%20s\n",$1,$2,$3)}';done

或者只是看CPU和内存使用率高的pod

kubectl get po -o wide -A |awk '{if ($3 !~ "0|READY") {print $1,$2}}'|while read ns pod;do kubectl top pod $pod -n $ns|grep -v NAME|awk --posix '{if ($2 ~ "[0-9]{3}m"|| $3 ~ "[0-9]{4}Mi") printf  ("%-60s%20s%20s\n",$1,$2,$3)}';done

分析docker的内存和CPU使用率

 docker stats --no-stream --format "table {{.Container}}\t{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"

下面是自定义的格式中可以使用的所有占位符:

.Container 根据用户指定的名称显示容器的名称或 ID。
.Name 容器名称。
.ID 容器 ID。
.CPUPerc CPU 使用率。
.MemUsage 内存使用量。
.NetIO 网络 I/O。
.BlockIO 磁盘 I/O。
.MemPerc 内存使用率。
.PIDs PID 号。

有了这些信息我们就可以完全按照自己的需求或者是偏好来控制 docker stats 命令输出的内容了。
JSON格式输出

docker stats --no-stream --format \
"{\"container\":\"{{ .Container }}\",\"memory\":{\"raw\":\"{{ .MemUsage }}\",\"percent\":\"{{ .MemPerc }}\"},\"cpu\":\"{{ .CPUPerc }}\"}"