【发布时间】:2022-04-25 17:01:19
【问题描述】:
在 kubectl 中,describe 和 get -o <format> 都可以用来获取资源的详细信息,我想知道两者有什么区别?如果get 可以做同样的事情甚至更多,为什么describe 还会存在?
【问题讨论】:
-
根据文档,-o 标志只应该改变输出格式。
标签: kubernetes kubectl
在 kubectl 中,describe 和 get -o <format> 都可以用来获取资源的详细信息,我想知道两者有什么区别?如果get 可以做同样的事情甚至更多,为什么describe 还会存在?
【问题讨论】:
标签: kubernetes kubectl
kubectl get 默认显示表格。 (您可以轻松查看/可视化大量对象)
kubectl describe 显示详细说明。 (更适合单个对象)
kubectl describe 比kubectl get -o yaml 给出的完整对象数据更扁平化,数据更少且更易于阅读
帮助输出供参考。
kubectl describe -h
Show details of a specific resource or group of resources
Print a detailed description of the selected resources, including related resources such as events or controllers. You
may select a single object by name, all objects of that type, provide a name prefix, or label selector. For example:
$ kubectl describe TYPE NAME_PREFIX
will first check for an exact match on TYPE and NAME_PREFIX. If no such resource exists, it will output details for
every resource that has a name prefixed with NAME_PREFIX.
Use "kubectl api-resources" for a complete list of supported resources.
kubectl get -h
Display one or many resources
Prints a table of the most important information about the specified resources. You can filter the list using a label
selector and the --selector flag. If the desired resource type is namespaced you will only see results in your current
namespace unless you pass --all-namespaces.
Uninitialized objects are not shown unless --include-uninitialized is passed.
By specifying the output as 'template' and providing a Go template as the value of the --template flag, you can filter
the attributes of the fetched resources.
Use "kubectl api-resources" for a complete list of supported resources.
【讨论】:
一个简单的解释可以是:
kubectl describe .. == Filterred kubectl get .. -o <format> + 相关事件
出于调试目的,查看describe 和get -o <format> 会很有用,因为每一个都具有其他未显示的相关信息。
请注意,事件也可以通过运行 kubectl get events(对于默认命名空间)或 kubectl get event --all-namespaces(对于所有命名空间)来显示。
一些挖掘:
使用扩展日志记录 --v=8 运行 kubectl describe 表明它使用 involvedObject.name%3Dsome-pod 调用 events API(第三次调用)。
$ kubectl --v=8 describe pod some-pod 2>&1 | grep GET
I1216 17:09:00.453529 6918 round_trippers.go:416] GET https://100.190.50.200/api/v1/namespaces/default/pods/some-pod
I1216 17:09:01.098053 6918 round_trippers.go:416] GET https://100.190.50.200/api/v1/namespaces/default/pods/some-pod
I1216 17:09:01.265924 6918 round_trippers.go:416] GET https://100.190.50.200/api/v1/namespaces/default/events?fieldSelector=involvedObject.name%3Dsome-pod%2CinvolvedObject.namespace%3Ddefault%2CinvolvedObject.uid%3Dbf664be1-1cde-11ea-bce6-42010af00267
kubectl get pod some-pod -o yaml 只调用pods API。
kubectl --v=8 get pod some-pod -o yaml 2>&1 | grep GET
I1216 17:13:21.084386 28256 round_trippers.go:416] GET https://100.190.50.200/api/v1/namespaces/default/pods/some-pod
【讨论】:
describe 中省略的内容是否有任何规则或文档? (例如,当deploy.spec.containers[].lifecycle.preStop 不可见时,我感到很惊讶。)
根据 kubernetes 文档:
kubectl -n <NAMESPACE> get <NAME_OF_RESOURCE>
打印有关指定的最重要信息的表格 资源。您可以使用标签选择器和 --选择器标志。如果所需的资源类型是命名空间的,除非您通过,否则您只会在当前命名空间中看到结果 --所有命名空间。 来源:https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#get
kubectl -n <NAMESPACE> describe <NAME_OF_RESOURCE>
打印所选资源的详细说明,包括 相关资源,例如事件或控制器。您可以选择一个 按名称的单个对象,该类型的所有对象,提供名称 前缀或标签选择器。 来源:https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#describe
kubectl describe 应该可以为您提供更多信息。即使我同意您的观点,有些资源已经使用 kubectl get 或 kubectl describe 提供了相同的信息。
【讨论】:
您可以使用以下方法获取特定资源的摘要:
虽然您可以通过以下方式获取详细信息:
【讨论】:
需要提到一个重要的区别:describe 接受前缀,但 get 需要确切的名称。
$ kubectl describe TYPE NAME_PREFIX
$ kubectl get TYPE NAME
【讨论】: