两条命令助你成为优秀的 YAML 工程师
共 6828字,需浏览 14分钟
·
2020-08-02 16:01
我们在编写 Kubernetes 资源清单的时候可能会经常会忘记要创建的资源名称,即使知道了可能也不记得该资源对象有哪些属性可以使用了,特别是对于那些名称很长的资源或者属性,即使死记硬背下来隔一段时间又会忘记了。
比如现在我们要创建一个 validating 的 admission webhook,我们就需要定义一个 ValidatingWebhookConfiguration
的资源对象,但是可能我们不记得它的全名了。这个时候我们可以使用 kubectl api-resources
命令来找到我们需要的 API 资源。找到了正确的资源名称之后,就需要了解如何编写正确的 YAML 资源清单文件了,但是 Kubernetes 中资源对象实在是太多了,而且每一个资源对象中配置属性也是非常多的,我们不可能都能忘记记住,这个时候我们也可以借助 kubectl explain
命令来找到完整的结构,这对于我们编写 YAML 资源清单文件非常有帮助。
kubectl api-resources 命令
kubectl api-resources 命令可以打印所有已经注册的 API 资源,如下所示:
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
资源:
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 来过滤:
$ kubectl api-resources |grep validating
validatingwebhookconfigurations admissionregistration.k8s.io false ValidatingWebhookConfiguration
这样就可以更精确的搜索到需要使用的资源名称了,比如我们这里就是 ValidatingWebhookConfiguration
,现在知道了资源对象的名称,然后可以使用 kubectl explain
命令来查找资源对象的属性。
kubectl explain 命令
kubectl explain
命令可以将资源对象的详细属性都展示出来,比如我们现在不知道如何去编写 ValidatingWebhookConfiguration
,这个时候我们可以通过命令 kubectl explain ValidatingWebhookConfiguration
来获取详细的信息:
$ 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
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.
这个命令会输出顶层的属性,我们只需要明白
表示字符串,表示对象,
[]
表示数组即可,对象在 YAML 文件中就需要缩进,数组就需要通过添加一个破折号来表示一个 Item,对于对象和对象数组我们不知道里面有什么属性的,我们还可以继续在后面查看,如下所示:
$ 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
参数来获取所有的属性:
$ 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-resources
和 kubectl explain
这两个命令可以为我们节省编写资源清单文件的大量时间。