题目主干
Task
通过 pod label name=cpu-loader,找到运行时占用大量 CPU 的 pod,
并将占用 CPU 最高的 pod 名称写入文件 /opt/KUTR000401/KUTR00401.txt(已存在)。
参考说明
通过帮助命令查看相关参数
student@master01:~$ kubectl top pod -h
Display resource (CPU/memory) usage of pods.
The 'top pod' command allows you to see the resource consumption of pods.
Due to the metrics pipeline delay, they may be unavailable for a few minutes since pod creation.
Aliases:
pod, pods, po
Examples:
# Show metrics for all pods in the default namespace
kubectl top pod
# Show metrics for all pods in the given namespace
kubectl top pod --namespace=NAMESPACE
# Show metrics for a given pod and its containers
kubectl top pod POD_NAME --containers
# Show metrics for the pods defined by label name=myLabel
kubectl top pod -l name=myLabel
Options:
-A, --all-namespaces=false: If present, list the requested object(s) across all namespaces. Namespace in current
context is ignored even if specified with --namespace.
--containers=false: If present, print usage of containers within a pod.
--field-selector='': Selector (field query) to filter on, supports '=', '==', and '!='.(e.g. --field-selector
key1=value1,key2=value2). The server only supports a limited number of field queries per type.
--no-headers=false: If present, print output without headers.
-l, --selector='': Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)
--sort-by='': If non-empty, sort pods list using specified field. The field can be either 'cpu' or 'memory'.
--use-protocol-buffers=true: Enables using protocol-buffers to access Metrics API.
Usage:
kubectl top pod [NAME | -l label] [options]
Use "kubectl options" for a list of global command-line options (applies to all commands).
student@master01:~$
题目解答
student@master01:~$ kubectl top pod -l name=cpu-loader --sort-by=cpu -A
NAMESPACE NAME CPU(cores) MEMORY(bytes)
cpu-top redis-test 2m 3Mi
cpu-top nginx-host 0m 4Mi
cpu-top test0 0m 8Mi
student@master01:~$ sudo mkdir -p /opt/KUTR000401/
student@master01:~$ sudo touch /opt/KUTR000401/KUTR00401.txt
student@master01:~$ sudo echo "redis-test" > /opt/KUTR000401/KUTR00401.txt
student@master01:~$ cat /opt/KUTR000401/KUTR00401.txt