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

公众号关注 「奇妙的 Linux 世界

设为「星标」,每天带你玩转 Linux !

两个助你高效编写 Kubernetes YAML 文件的神技_第1张图片

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

比如现在我们要创建一个 validatingadmission 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  
     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  
     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 


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

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

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

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

本文转载自:「k8s技术圈」,原文:https://tinyurl.com/y4vvln2x,版权归原作者所有。欢迎投稿,投稿邮箱: [email protected]

你可能还喜欢

点击下方图片即可阅读

推荐 3 款超好用的 Docker 图形化管理工具

点击上方图片,打开小程序,加入「玩转 Linux」圈子

更多有趣的互联网新鲜事,关注「奇妙的互联网」视频号全了解!

你可能感兴趣的:(两个助你高效编写 Kubernetes YAML 文件的神技)