题目主干

Task
通过 pod label name=cpu-loader,找到运行时占用大量 CPU 的 pod,
并将占用 CPU 最高的 pod 名称写入文件 /opt/KUTR000401/KUTR00401.txt(已存在)。

参考说明

通过帮助命令查看相关参数

  1. student@master01:~$ kubectl top pod -h
  2. Display resource (CPU/memory) usage of pods.
  3. The 'top pod' command allows you to see the resource consumption of pods.
  4. Due to the metrics pipeline delay, they may be unavailable for a few minutes since pod creation.
  5. Aliases:
  6. pod, pods, po
  7. Examples:
  8. # Show metrics for all pods in the default namespace
  9. kubectl top pod
  10. # Show metrics for all pods in the given namespace
  11. kubectl top pod --namespace=NAMESPACE
  12. # Show metrics for a given pod and its containers
  13. kubectl top pod POD_NAME --containers
  14. # Show metrics for the pods defined by label name=myLabel
  15. kubectl top pod -l name=myLabel
  16. Options:
  17. -A, --all-namespaces=false: If present, list the requested object(s) across all namespaces. Namespace in current
  18. context is ignored even if specified with --namespace.
  19. --containers=false: If present, print usage of containers within a pod.
  20. --field-selector='': Selector (field query) to filter on, supports '=', '==', and '!='.(e.g. --field-selector
  21. key1=value1,key2=value2). The server only supports a limited number of field queries per type.
  22. --no-headers=false: If present, print output without headers.
  23. -l, --selector='': Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)
  24. --sort-by='': If non-empty, sort pods list using specified field. The field can be either 'cpu' or 'memory'.
  25. --use-protocol-buffers=true: Enables using protocol-buffers to access Metrics API.
  26. Usage:
  27. kubectl top pod [NAME | -l label] [options]
  28. Use "kubectl options" for a list of global command-line options (applies to all commands).
  29. student@master01:~$

题目解答

  1. student@master01:~$ kubectl top pod -l name=cpu-loader --sort-by=cpu -A
  2. NAMESPACE NAME CPU(cores) MEMORY(bytes)
  3. cpu-top redis-test 2m 3Mi
  4. cpu-top nginx-host 0m 4Mi
  5. cpu-top test0 0m 8Mi
  6. student@master01:~$ sudo mkdir -p /opt/KUTR000401/
  7. student@master01:~$ sudo touch /opt/KUTR000401/KUTR00401.txt
  8. student@master01:~$ sudo echo "redis-test" > /opt/KUTR000401/KUTR00401.txt
  9. student@master01:~$ cat /opt/KUTR000401/KUTR00401.txt