在kubernetes里,pod,service,rs,rc,deploy,resource等对象都需要使用yaml文件来创建,很多时候我们都是参照照官方示例或者一些第三方示例来编写yaml文件以创建对象.虽然这些示例很有典型性和代表性,能够满足我们大部分时候的需求,然而这往往还是不够的,根据项目不同,实际配置可能远比官方提供的demo配置复杂的多,这就要求我们除了掌握常用的配置外,还需要对其它配置有所了解.如果有一个文档能够速查某一对象的所有配置,不但方便我们学习不同的配置,也可以做为一个小手册以便我们记不起来某些配置时可以速查.
下面我们介绍一些小技巧来快速查看kubernetes api

查看所有api资源

可以通过命令kubectl api-resources来查看所有api资源

  1. [centos@k8s-master ~]$ kubectl api-resources
  2. NAME SHORTNAMES APIGROUP NAMESPACED KIND
  3. bindings true Binding
  4. componentstatuses cs false ComponentStatus
  5. configmaps cm true ConfigMap
  6. endpoints ep true Endpoints
  7. events ev true Event
  8. limitranges limits true LimitRange
  9. namespaces ns false Namespace
  10. nodes no false Node
  11. persistentvolumeclaims pvc true PersistentVolumeClaim
  12. persistentvolumes pv false PersistentVolume
  13. pods po true Pod
  14. podtemplates true PodTemplate
  15. replicationcontrollers rc true ReplicationController
  16. resourcequotas quota true ResourceQuota
  17. secrets true Secret
  18. serviceaccounts sa true ServiceAccount
  19. services svc true Service
  20. mutatingwebhookconfigurations admissionregistration.k8s.io false MutatingWebhookConfiguration
  21. validatingwebhookconfigurations admissionregistration.k8s.io false ValidatingWebhookConfiguration
  22. customresourcedefinitions crd,crds apiextensions.k8s.io false CustomResourceDefinition
  23. apiservices apiregistration.k8s.io false APIService
  24. controllerrevisions apps true ControllerRevision
  25. daemonsets ds apps true DaemonSet
  26. deployments deploy apps true Deployment
  27. replicasets rs apps true ReplicaSet
  28. statefulsets sts apps true StatefulSet
  29. tokenreviews authentication.k8s.io false TokenReview
  30. localsubjectaccessreviews authorization.k8s.io true LocalSubjectAccessReview
  31. selfsubjectaccessreviews authorization.k8s.io false SelfSubjectAccessReview
  32. selfsubjectrulesreviews authorization.k8s.io false SelfSubjectRulesReview
  33. subjectaccessreviews authorization.k8s.io false SubjectAccessReview
  34. horizontalpodautoscalers hpa autoscaling true HorizontalPodAutoscaler
  35. cronjobs cj batch true CronJob
  36. jobs batch true Job
  37. certificatesigningrequests csr certificates.k8s.io false CertificateSigningRequest
  38. leases coordination.k8s.io true Lease
  39. events ev events.k8s.io true Event
  40. daemonsets ds extensions true DaemonSet
  41. deployments deploy extensions true Deployment
  42. ingresses ing extensions true Ingress
  43. networkpolicies netpol extensions true NetworkPolicy
  44. podsecuritypolicies psp extensions false PodSecurityPolicy
  45. replicasets rs extensions true ReplicaSet
  46. networkpolicies netpol networking.k8s.io true NetworkPolicy
  47. poddisruptionbudgets pdb policy true PodDisruptionBudget
  48. podsecuritypolicies psp policy false PodSecurityPolicy
  49. clusterrolebindings rbac.authorization.k8s.io false ClusterRoleBinding
  50. clusterroles rbac.authorization.k8s.io false ClusterRole
  51. rolebindings rbac.authorization.k8s.io true RoleBinding
  52. roles rbac.authorization.k8s.io true Role
  53. priorityclasses pc scheduling.k8s.io false PriorityClass
  54. storageclasses sc storage.k8s.io false StorageClass
  55. volumeattachments storage.k8s.io false VolumeAttachment

除了可以看到资源的对象名称外,还可以看到对象的别名,这时候我们再看到别人的命令如kubectl get no这样费解的命令时就可以知道它实际上代表的是kubectl get nodes命令

查看api的版本,很多yaml配置里都需要指定配置的资源版本,我们经常看到v1,beta1,beta2这样的配置,到底某个资源的最新版本是什么呢?

其实,可以通过kubectl api-versions来查看api的版本

  1. [centos@k8s-master ~]$ kubectl api-versions
  2. admissionregistration.k8s.io/v1beta1
  3. apiextensions.k8s.io/v1beta1
  4. apiregistration.k8s.io/v1
  5. apiregistration.k8s.io/v1beta1
  6. apps/v1
  7. apps/v1beta1
  8. apps/v1beta2
  9. authentication.k8s.io/v1
  10. authentication.k8s.io/v1beta1
  11. authorization.k8s.io/v1
  12. authorization.k8s.io/v1beta1
  13. autoscaling/v1
  14. autoscaling/v2beta1
  15. autoscaling/v2beta2
  16. batch/v1
  17. batch/v1beta1
  18. certificates.k8s.io/v1beta1
  19. coordination.k8s.io/v1beta1
  20. events.k8s.io/v1beta1
  21. extensions/v1beta1
  22. networking.k8s.io/v1
  23. policy/v1beta1
  24. rbac.authorization.k8s.io/v1
  25. rbac.authorization.k8s.io/v1beta1
  26. scheduling.k8s.io/v1beta1
  27. storage.k8s.io/v1
  28. storage.k8s.io/v1beta1
  29. v1

以上只是整体概况,很多时候我们还想要看到某个api下面都有哪些配置,某一荐配置的含义等,下面罗列一些常用的api范例和一些查看api的技巧

常见范例

  1. 通过kubectl explain <资源名对象名>查看资源对象拥有的字段

前面说过,可以通过kubectl api-resources来查看资源名称,如果想要查看某个资源的字段,可以通过kubectl explain <资源名对象名>来查点它都有哪些字段

  1. [centos@k8s-master ~]$ kubectl explain pod
  2. KIND: Pod
  3. VERSION: v1
  4. DESCRIPTION:
  5. Pod is a collection of containers that can run on a host. This resource is
  6. created by clients and scheduled onto hosts.
  7. FIELDS:
  8. apiVersion <string>
  9. APIVersion defines the versioned schema of this representation of an
  10. object. Servers should convert recognized schemas to the latest internal
  11. value, and may reject unrecognized values. More info:
  12. https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
  13. kind <string>
  14. Kind is a string value representing the REST resource this object
  15. represents. Servers may infer this from the endpoint the client submits
  16. requests to. Cannot be updated. In CamelCase. More info:
  17. https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds
  18. metadata <Object>
  19. Standard object's metadata. More info:
  20. https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
  21. spec <Object>
  22. Specification of the desired behavior of the pod. More info:
  23. https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
  24. status <Object>
  25. Most recently observed status of the pod. This data may not be up to date.
  26. Populated by the system. Read-only. More info:
  27. https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
  28. [centos@k8s-master ~]$

以上Description是对资源对象的简要描述,Fields则是对所有字段的描述

  1. 列出所有api字段

    通过以上我们能感觉到,以上好像并没有罗列出所有的api字段,实际上以上列出的仅是一级字段,一级字段可能还包含二级的,三级的字段,想要罗列出所有的字段,可以加上--recursive来列出所有可能的字段

  1. [centos@k8s-master ~]$ kubectl explain svc --recursive
  2. KIND: Service
  3. VERSION: v1
  4. DESCRIPTION:
  5. Service is a named abstraction of software service (for example, mysql)
  6. consisting of local port (for example 3306) that the proxy listens on, and
  7. the selector that determines which pods will answer requests sent through
  8. the proxy.
  9. FIELDS:
  10. apiVersion <string>
  11. kind <string>
  12. metadata <Object>
  13. annotations <map[string]string>
  14. clusterName <string>
  15. creationTimestamp <string>
  16. deletionGracePeriodSeconds <integer>
  17. deletionTimestamp <string>
  18. finalizers <[]string>
  19. generateName <string>
  20. generation <integer>
  21. initializers <Object>
  22. pending <[]Object>
  23. name <string>
  24. result <Object>
  25. apiVersion <string>
  26. code <integer>
  27. details <Object>
  28. causes <[]Object>
  29. field <string>
  30. message <string>
  31. reason <string>
  32. group <string>
  33. kind <string>
  34. name <string>
  35. retryAfterSeconds <integer>
  36. uid <string>
  37. kind <string>
  38. message <string>
  39. metadata <Object>
  40. continue <string>
  41. resourceVersion <string>
  42. selfLink <string>
  43. reason <string>
  44. status <string>
  45. labels <map[string]string>
  46. name <string>
  47. namespace <string>
  48. ownerReferences <[]Object>
  49. apiVersion <string>
  50. blockOwnerDeletion <boolean>
  51. controller <boolean>
  52. kind <string>
  53. name <string>
  54. uid <string>
  55. resourceVersion <string>
  56. selfLink <string>
  57. uid <string>
  58. spec <Object>
  59. clusterIP <string>
  60. externalIPs <[]string>
  61. externalName <string>
  62. externalTrafficPolicy <string>
  63. healthCheckNodePort <integer>
  64. loadBalancerIP <string>
  65. loadBalancerSourceRanges <[]string>
  66. ports <[]Object>
  67. name <string>
  68. nodePort <integer>
  69. port <integer>
  70. protocol <string>
  71. targetPort <string>
  72. publishNotReadyAddresses <boolean>
  73. selector <map[string]string>
  74. sessionAffinity <string>
  75. sessionAffinityConfig <Object>
  76. clientIP <Object>
  77. timeoutSeconds <integer>
  78. type <string>
  79. status <Object>
  80. loadBalancer <Object>
  81. ingress <[]Object>
  82. hostname <string>
  83. ip <string>
  84. [centos@k8s-master ~]$

以上输出的内容是经过格式化了的,我们可以根据缩进很容易看到某一个字段从属于关系

查看具体字段

通过上面kubectl explain service --recursive可以看到所有的api名称,但是以上仅仅是罗列了所有的api名称,如果想要知道某一个api名称的详细信息,则可以通过kubectl explain <资源对象名称.api名称>的方式来查看,比如以下示例可以查看到service下的spec下的ports字段的信息

  1. [centos@k8s-master ~]$ kubectl explain svc.spec.ports
  2. KIND: Service
  3. VERSION: v1
  4. RESOURCE: ports <[]Object>
  5. DESCRIPTION:
  6. The list of ports that are exposed by this service. More info:
  7. https://kubernetes.io/docs/concepts/services-networking/service/#virtual-ips-and-service-proxies
  8. ServicePort contains information on service's port.
  9. FIELDS:
  10. name <string>
  11. The name of this port within the service. This must be a DNS_LABEL. All
  12. ports within a ServiceSpec must have unique names. This maps to the 'Name'
  13. field in EndpointPort objects. Optional if only one ServicePort is defined
  14. on this service.
  15. nodePort <integer>
  16. The port on each node on which this service is exposed when type=NodePort
  17. or LoadBalancer. Usually assigned by the system. If specified, it will be
  18. allocated to the service if unused or else creation of the service will
  19. fail. Default is to auto-allocate a port if the ServiceType of this Service
  20. requires one. More info:
  21. https://kubernetes.io/docs/concepts/services-networking/service/#type-nodeport
  22. port <integer> -required-
  23. The port that will be exposed by this service.
  24. protocol <string>
  25. The IP protocol for this port. Supports "TCP", "UDP", and "SCTP". Default
  26. is TCP.
  27. targetPort <string>
  28. Number or name of the port to access on the pods targeted by the service.
  29. Number must be in the range 1 to 65535. Name must be an IANA_SVC_NAME. If
  30. this is a string, it will be looked up as a named port in the target Pod's
  31. container ports. If this is not specified, the value of the 'port' field is
  32. used (an identity map). This field is ignored for services with
  33. clusterIP=None, and should be omitted or set equal to the 'port' field.
  34. More info:
  35. https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-servic