kubectl:描述与 get -o <format>

kubectl: describe vs get -o <format>

在kubectl中,describeget -o <format>都可以用来获取资源的详细信息,我想知道这两者有什么区别?如果 get 可以做同样的事情甚至更多,为什么 describe 还存在?

根据 kubernetes 文档:

kubectl -n <NAMESPACE> get <NAME_OF_RESOURCE>

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. Source: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#get

kubectl -n <NAMESPACE> describe <NAME_OF_RESOURCE>

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. Source: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#describe

kubectl describe 应该会为您提供更多信息。即使我同意您的看法,有些资源与 kubectl get 或 kubectl describe 提供的信息相同。

  • kubectl get 默认显示表格。 (您可以 view/visualize 轻松地处理大量对象)

  • kubectl describe显示详细说明。 (最好是单个对象)

  • kubectl describekubectl 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> + 相关事件


出于调试目的,同时查看 describeget -o <format> 会很有用,因为每个都有相关信息,而另一个未显示。

请注意,事件也可以通过 运行 kubectl get events(对于默认命名空间)或 kubectl get event --all-namespaces(对于所有命名空间)显示。


一些挖掘:

运行 kubectl describe with extended logging --v=8 显示它用 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

您可以使用以下方法获取特定资源的摘要:

  • kubectl get <资源> <名称>

虽然您可以通过以下方式获取详细信息:

  • kubectl describe <资源> <名称>

需要提及一个重要区别:describe 接受前缀,但 get 需要准确的名称。

$ kubectl describe TYPE NAME_PREFIX
$ kubectl get TYPE NAME