两个助你高效编写 Kubernetes YAML 文件的神技

Posted by Mike on 2020-07-30

我们在编写 Kubernetes 资源清单的时候可能会经常会忘记要创建的资源名称,即使知道了可能也不记得该资源对象有哪些属性可以使用了,特别是对于那些名称很长的资源或者属性,即使死记硬背下来隔一段时间又会忘记了。

比如现在我们要创建一个 validatingadmission webhook,我们就需要定义一个 ValidatingWebhookConfiguration 的资源对象,但是可能我们不记得它的全名了。这个时候我们可以使用 kubectl api-resources 命令来找到我们需要的 API 资源。找到了正确的资源名称之后,就需要了解如何编写正确的 YAML 资源清单文件了,但是 Kubernetes 中资源对象实在是太多了,而且每一个资源对象中配置属性也是非常多的,我们不可能都能完全记住,这个时候我们也可以借助 kubectl explain 命令来找到完整的结构,这对于我们编写 YAML 资源清单文件非常有帮助。

kubectl api-resources 命令

kubectl api-resources 命令可以打印所有已经注册的 API 资源,如下所示:

1
2
3
4
5
6
7
8
9
10
11
$ Kubectl api-resources
NAME SHORTNAMES APIGROUP NAMESPACED KIND
bindings true Binding
componentstatuses cs false ComponentStatus
configmaps cm true ConfigMap
endpoints ep true Endpoints
events ev true Event
limitranges limits true LimitRange
namespaces ns false Namespace
nodes no false Node
......

其中也会包含上面提到的 ValidatingWebhookConfiguration 资源:

1
2
3
4
mutatingwebhookconfigurations                                                  admissionregistration.k8s.io   false        MutatingWebhookConfiguration
validatingwebhookconfigurations admissionregistration.k8s.io false ValidatingWebhookConfiguration
customresourcedefinitions crd,crds apiextensions.k8s.io false CustomResourceDefinition
apiservices apiregistration.k8s.io false APIService

由于 Kubernetes 中已经注册的资源对象非常多,所以如果我们知道我们要查找的资源名称包含一些关键词的话,可以用 grep 来过滤:

1
2
$ kubectl api-resources |grep validating
validatingwebhookconfigurations admissionregistration.k8s.io false ValidatingWebhookConfiguration

这样就可以更精确的搜索到需要使用的资源名称了,比如我们这里就是 ValidatingWebhookConfiguration ,现在知道了资源对象的名称,然后可以使用 kubectl explain 命令来查找资源对象的属性。

kubectl explain 命令

kubectl explain 命令可以将资源对象的详细属性都展示出来,比如我们现在不知道如何去编写 ValidatingWebhookConfiguration ,这个时候我们可以通过命令 kubectl explain ValidatingWebhookConfiguration 来获取详细的信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$ kubectl explain ValidatingWebhookConfiguration
KIND: ValidatingWebhookConfiguration
VERSION: admissionregistration.k8s.io/v1

DESCRIPTION:
ValidatingWebhookConfiguration describes the configuration of and admission
webhook that accept or reject and object without changing it.

FIELDS:
apiVersion <string>
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

kind <string>
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

metadata <Object>
Standard object metadata; More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata.

webhooks <[]Object>
Webhooks is a list of webhooks and the affected resources and operations.

这个命令会输出顶层的属性,我们只需要明白 <string> 表示字符串,<Object> 表示对象, [] 表示数组即可,对象在 YAML 文件中就需要缩进,数组就需要通过添加一个破折号来表示一个 Item,对于对象和对象数组我们不知道里面有什么属性的,我们还可以继续在后面查看,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ kubectl explain ValidatingWebhookConfiguration.metadata
KIND: ValidatingWebhookConfiguration
VERSION: admissionregistration.k8s.io/v1

RESOURCE: metadata <Object>

DESCRIPTION:
Standard object metadata; More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata.

ObjectMeta is metadata that all persisted resources must have, which
includes all objects users must create.

FIELDS:
annotations <map[string]string>
Annotations is an unstructured key value map stored with a resource that
may be set by external tools to store and retrieve arbitrary metadata. They
are not queryable and should be preserved when modifying objects. More
info: http://kubernetes.io/docs/user-guide/annotations

......

上面输出的属性就是属于 metadata 这个 key 下面对应的对象了,有的时候如果觉得这样一层一层的去查看比较麻烦,我们还可以传入一个 --recursive 参数来获取所有的属性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
$ kubectl explain validatingwebhookconfiguration --recursive
KIND: ValidatingWebhookConfiguration
VERSION: admissionregistration.k8s.io/v1

DESCRIPTION:
ValidatingWebhookConfiguration describes the configuration of and admission
webhook that accept or reject and object without changing it.

FIELDS:
apiVersion <string>
kind <string>
metadata <Object>
annotations <map[string]string>
clusterName <string>
creationTimestamp <string>
deletionGracePeriodSeconds <integer>
deletionTimestamp <string>
finalizers <[]string>
generateName <string>
generation <integer>
labels <map[string]string>
managedFields <[]Object>
apiVersion <string>
fieldsType <string>
fieldsV1 <map[string]>
manager <string>
operation <string>
time <string>
name <string>
namespace <string>
ownerReferences <[]Object>
apiVersion <string>
blockOwnerDeletion <boolean>
controller <boolean>
kind <string>
name <string>
uid <string>
resourceVersion <string>
selfLink <string>
uid <string>
webhooks <[]Object>
admissionReviewVersions <[]string>
clientConfig <Object>
caBundle <string>
service <Object>
name <string>
namespace <string>
path <string>
port <integer>
url <string>
failurePolicy <string>
matchPolicy <string>
name <string>
namespaceSelector <Object>
matchExpressions <[]Object>
key <string>
operator <string>
values <[]string>
matchLabels <map[string]string>
objectSelector <Object>
matchExpressions <[]Object>
key <string>
operator <string>
values <[]string>
matchLabels <map[string]string>
rules <[]Object>
apiGroups <[]string>
apiVersions <[]string>
operations <[]string>
resources <[]string>
scope <string>
sideEffects <string>
timeoutSeconds <integer>

这个命令就可以将资源对象的完整属性列出来,而且缩进格式和 YAML 文件基本上是一致的,这样对于我们去编写资源清单文件就更加友好了。

自从使用 kubectl api-resourceskubectl explain 这两个命令后,为我们编写资源清单文件节省了很多时间,工作效率大大提高了!

这么好的神技,你还不赶紧用起来吗? 如果你还有更好的方法,欢迎大家积极留言讨论哟!

本文转载自:「k8s技术圈」,原文:https://tinyurl.com/y4vvln2x,版权归原作者所有。欢迎投稿,投稿邮箱: editor@hi-linux.com