我已经能够用kubectl的插件以以下方式生成以下输出:
./kubectl get namespaces | awk '{ print $1 }' | while read x; do ./kubectl resource-capacity --sort cpu.util --namespace $x --util --pod-count --pods; done
Error getting Pod Metrics: pods.metrics.k8s.io is forbidden: User "u-567" cannot list resource "pods" in API group "metrics.k8s.io" in the namespace "NAME"
For this to work, metrics-server needs to be running in your cluster
NODE POD CPU REQUESTS CPU LIMITS CPU UTIL MEMORY REQUESTS MEMORY LIMITS MEMORY UTIL POD COUNT
* * 16550m (16%) 45060m (45%) 0Mi (0%) 38174Mi (9%) 33492Mi (8%) 0Mi (0%) 16/1540
ip-10-11-7-77.eu-west-1.compute.internal * 6150m (76%) 14160m (177%) 139m (1%) 16784Mi (53%) 9632Mi (30%) 3126Mi (9%) 5/110
ip-10-11-7-77.eu-west-1.compute.internal anomaly-detector-water-transformer-default-msxhv-deploymenrnv85 1035m (12%) 3040m (38%) 46m (0%) 2138Mi (6%) 2376Mi (7%) 300Mi (0%)
ip-10-11-7-77.eu-west-1.compute.internal anomaly-detector-blades-transformer-default-gmvb2-deploymezzt5r 1035m (12%) 3040m (38%) 45m (0%) 2138Mi (6%) 2376Mi (7%) 292Mi (0%)
ip-10-11-7-77.eu-west-1.compute.internal leadec-uk-inference-m1-predictor-default-zvxkb-deployment-8mscd 1035m (12%) 3040m (38%) 43m (0%) 2138Mi (6%) 2376Mi (7%) 277Mi (0%)
ip-10-11-7-77.eu-west-1.compute.internal datascience-0 2010m (25%) 2000m (25%) 8m (0%) 8232Mi (26%) 128Mi (0%) 2259Mi (7%)
ip-10-11-7-77.eu-west-1.compute.internal anomaly-detector-leadec-predictor-default-tqjsg-deployment4bqdc 1035m (12%) 3040m (38%) 0Mi (0%) 2138Mi (6%) 2376Mi (7%) 0Mi (0%)
ip-10-11-7-134.eu-west-1.compute.internal * 2070m (25%) 6080m (76%) 88m (1%) 4276Mi (13%) 4752Mi (15%) 597Mi (1%) 2/110
ip-10-11-7-134.eu-west-1.compute.internal leadec-uk-inference-m1-transformer-default-84tk6-deploymeng4qjf 1035m (12%) 3040m (38%) 45m (0%) 2138Mi (6%) 2376Mi (7%) 298Mi (0%)
ip-10-11-7-134.eu-west-1.compute.internal leadec-uk-synthetic-f65535db1d5e1078fca102b9506391d8-deplop74ww 1035m (12%) 3040m (38%) 44m (0%) 2138Mi (6%) 2376Mi (7%) 299Mi (0%)
ip-10-11-7-123.eu-west-1.compute.internal * 1035m (12%) 3040m (38%) 55m (0%) 2138Mi (6%) 2376Mi (7%) 317Mi (1%) 1/110
ip-10-11-7-123.eu-west-1.compute.internal leadec-uk-synthetic-58a5440150937e2198bfdd610a109b95-deplomfn97 1035m (12%) 3040m (38%) 55m (0%) 2138Mi (6%) 2376Mi (7%) 317Mi (1%)
ip-10-11-7-190.eu-west-1.compute.internal * 1085m (13%) 3540m (44%) 50m (0%) 2148Mi (6%) 2476Mi (7%) 327Mi (1%) 2/110
ip-10-11-7-190.eu-west-1.compute.internal leadec-uk-synthetic-year3-m4-predictor-default-ttdkp-deplovvtj6 1035m (12%) 3040m (38%) 47m (0%) 2138Mi (6%) 2376Mi (7%) 267Mi (0%)
ip-10-11-7-190.eu-west-1.compute.internal random-experiment-random-568ff69c58-j7lx4 50m (0%) 500m (6%) 3m (0%) 10Mi (0%) 100Mi (0%) 60Mi (0%)
ip-10-11-7-44.eu-west-1.compute.internal * 1035m (12%) 3040m (38%) 46m (0%) 2138Mi (6%) 2376Mi (7%) 268Mi (0%) 1/110
ip-10-11-7-44.eu-west-1.compute.internal leadec-uk-synthetic-year4-m4-predictor-default-7d9cb-deplofc5kf 1035m (12%) 3040m (38%) 46m (0%) 2138Mi (6%) 2376Mi (7%) 268Mi (0%)
ip-10-11-7-57.eu-west-1.compute.internal * 1035m (12%) 3040m (38%) 46m (0%) 2138Mi (6%) 2376Mi (7%) 290Mi (0%) 1/110
ip-10-11-7-57.eu-west-1.compute.internal anomaly-detector-leadec-transformer-default-7j42s-deploymeqhxtd 1035m (12%) 3040m (38%) 46m (0%) 2138Mi (6%) 2376Mi (7%) 290Mi (0%)
ip-10-11-7-113.eu-west-1.compute.internal * 1035m (25%) 3040m (76%) 43m (1%) 2138Mi (13%) 2376Mi (15%) 298Mi (1%) 1/110
ip-10-11-7-113.eu-west-1.compute.internal leadec-uk-synthetic-m4-transformer-default-9d4pz-deploymenhtfsv 1035m (25%) 3040m (76%) 43m (1%) 2138Mi (13%) 2376Mi (15%) 298Mi (1%)
ip-10-11-7-168.eu-west-1.compute.internal * 0Mi (0%) 0Mi (0%) 0Mi (0%) 0Mi (0%) 0Mi (0%) 0Mi (0%) 0/110
我想在每个循环中附加名称空间作为列,我怎么能这样做呢。
提前感谢
答案根据操作人员的意见进行调整。
按节点打印pod资源的命令:
kubectl describe node | perl -0777 -wnE '@pods = /Name:s+([^ ]+)n.*?Non-terminateds+Pods:s+([0-9]+s+ins+total)n(.*?)nAllocated resources:/gs;say for @pods'
第一步,将每个节点的输出写入各自的文件。
kubectl describe node | perl -0777 -wnE '@pods = /Name:s+([^ ]+)n.*?Non-terminateds+Pods:s+([0-9]+s+ins+total)n(.*?)nAllocated resources:/gs;say for @pods'|awk -v OFS=',' '/^[^ ]+/{node=$0;next} {print $0 > node ".csv"}'
上面的命令将创建如下文件:
ls -lrt *.csv
-rw-rw-r-- 1 p.. p.. 2800 Sep 8 12:43 dev-kube-worker-3.csv
-rw-rw-r-- 1 p.. p.. 2782 Sep 8 12:43 dev-kube-worker-2.csv
-rw-rw-r-- 1 p.. p.. 2800 Sep 8 12:43 dev-kube-worker-1.csv
-rw-rw-r-- 1 p.. p.. 1551 Sep 8 12:43 dev-kube-controller-1.csv
最终修改:但是,这些文件的内容不会是CSV格式,因此要使内容成为CSV:
kubectl describe node | perl -0777 -wnE '@pods = /Name:s+([^ ]+)n.*?Non-terminateds+Pods:s+([0-9]+s+ins+total)n(.*?)nAllocated resources:/gs;say for @pods'|awk -v OFS=',' '/^[^ ]+/{node=$0;next} {$1=$1;print $0 > node ".csv"}'