1.top简介
k8stop命令我们通常可以基于top命令来查看节点上的资源使用情况,
可以带两个参数nodes和pods,通过这个命令分别用于查看节点和pods的资源使用情况,
这对于我们快速查看k8s集群以及pod的字样利用率,从而提醒业务或者系统管理人员及时的集群扩容,调整Pod的资源请求。
2.metrics-server组件安装
top命令依赖于metrics-server组件,需要提前安装才行
部署文件:https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
安装: kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
安装过程中出现一下错误:
1)Back-off pulling image "k8s.gcr.io/metrics-server/metrics-server:v0.6.2"
如果镜像下载不下来,就需要去国外下载中转过来,我已经中转了该镜像:duruo850/metrics-server:v0.6.2
2)x509: cannot validate certificate for 10.0.22.121 because it doesn't contain any IP SANs
如果节点没有安装证书,那么先忽略就好,base/deployment.yaml文件的deployment 添加- --kubelet-insecure-tls跳过证书验证
节选部分,增加- --kubelet-insecure-tls
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
image: k8s.gcr.io/metrics-server/metrics-server:v0.6.2
imagePullPolicy: IfNotPresent
3.top命令使用
查看所有节点资源占用:sudo kubectl top nodes
qiteck@logic:~/program/k8s_1.24.2$ sudo kubectl top nodes
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
master.cluster.k8s 163m 8% 6429Mi 81%
server 230m 11% 6722Mi 85%
查看所有pods资源占用:sudo kubectl top pods --all-namespaces
qiteck@logic:~/program/k8s_1.24.2$ sudo kubectl top pods --all-namespaces
NAMESPACE NAME CPU(cores) MEMORY(bytes)
default account-6468847985-2fqpf 1m 50Mi
default account-6468847985-cxcmh 1m 48Mi
default advertise-849b4cb777-8xw4s 1m 5Mi
default advertise-849b4cb777-knfrs 1m 6Mi
default consul-client-6ln95 13m 197Mi
default consul-client-r7bkd 7m 49Mi
default consul-server-0 9m 282Mi
default consul-server-1 43m 298Mi
default consul-server-2 37m 282Mi
default count-f85858d45-knlzw 1m 5Mi
default count-f85858d45-vk6tg 1m 6Mi
default course-78fbc8957d-5vkhv 2m 43Mi
default course-78fbc8957d-pvr5w 1m 43Mi
default dev-6f444b78cc-7xsf5 1m 5Mi
default dev-6f444b78cc-dj996 1m 6Mi
default device-79574fbcf6-2zrzg 1m 64Mi
default device-79574fbcf6-5ck5f 1m 59Mi
default erp-6ff85c8bc5-m547d 1m 9Mi
default erp-6ff85c8bc5-xpq9n 1m 10Mi
default file-bb5b5c45b-htgnb 1m 46Mi
default file-bb5b5c45b-m2dlp 2m 43Mi
default game-fff48cb9c-5pb4h 1m 6Mi
default game-fff48cb9c-wb8ln 1m 6Mi
default gateway-58698f8b57-w2pq5 3m 90Mi
default gateway-58698f8b57-z77bk 3m 85Mi
default msg-7c67dc656b-mj4gl 1m 48Mi
default msg-7c67dc656b-x7kcl 1m 47Mi
default omp-55bf7f8696-6gwl9 1m 147Mi
default omp-55bf7f8696-szbz5 1m 146Mi
default omp2-96c7c4664-9hqfl 1m 103Mi
default omp2-96c7c4664-d64qj 1m 100Mi
default ota-7446f588d4-7tqb4 2m 38Mi
default ota-7446f588d4-p5cv5 2m 37Mi
default pns-85d88cd969-b5b6j 1m 5Mi
default pns-85d88cd969-lh6vl 1m 6Mi
default rcache-c8699444d-n9zh6 1m 6Mi
default rcache-c8699444d-tl9ds 1m 6Mi
default sport-69fbf7c74b-lbdv2 1m 45Mi
default sport-69fbf7c74b-lssj7 1m 44Mi
default websocket-55958d78fc-24xw7 1m 6Mi
default websocket-55958d78fc-dhqhh 1m 186Mi
kafka kafka-0 4m 590Mi
kafka kafka-1 4m 582Mi
kafka kafka-2 5m 582Mi
kube-system coredns-74586cf9b6-8mbqz 2m 18Mi
kube-system coredns-74586cf9b6-hz48p 2m 14Mi
kube-system etcd-master.cluster.k8s 14m 57Mi
kube-system kube-apiserver-master.cluster.k8s 41m 367Mi
kube-system kube-controller-manager-master.cluster.k8s 11m 53Mi
kube-system kube-flannel-ds-rt8z5 3m 14Mi
kube-system kube-flannel-ds-t6nrc 2m 14Mi
kube-system kube-proxy-ngnrv 1m 20Mi
kube-system kube-proxy-tjh8h 1m 20Mi
kube-system kube-scheduler-master.cluster.k8s 3m 24Mi
kube-system metrics-server-5ccdd99954-kcmmz 4m 15Mi
zookeeper zk-0 5m 221Mi
zookeeper zk-1 5m 249Mi
zookeeper zk-2 3m 213Mi
查看某个pod资源占用:sudo kubectl top pod count-f85858d45-knlzw
qiteck@logic:~/program/k8s_1.24.2$ sudo kubectl top pod count-f85858d45-knlzw
NAME CPU(cores) MEMORY(bytes)
count-f85858d45-knlzw 1m 5Mi