前面说过,可以通过kubectl api-resources来查看资源名称,如果想要查看某个资源的字段,可以通过kubectl explain <资源名对象名>来查点它都有哪些字段
[centos@k8s-master ~]$ kubectl explain pod KIND: Pod VERSION: v1 DESCRIPTION: Pod is a collection of containers that can run on a host. This resource is created by clients and scheduled onto hosts. 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/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/api-conventions.md#types-kinds metadata <Object> Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata spec <Object> Specification of the desired behavior of the pod. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status status <Object> Most recently observed status of the pod. This data may not be up to date. Populated by the system. Read-only. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status [centos@k8s-master ~]$以上Description是对资源对象的简要描述,Fields则是对所有字段的描述
2) 列出所有api字段
通过以上我们能感觉到,以上好像并没有罗列出所有的api字段,实际上以上列出的仅是一级字段,一级字段可能还包含二级的,三级的字段,想要罗列出所有的字段,可以加上--recursive来列出所有可能的字段
[centos@k8s-master ~]$ kubectl explain svc --recursive KIND: Service VERSION: v1 DESCRIPTION: Service is a named abstraction of software service (for example, mysql) consisting of local port (for example 3306) that the proxy listens on, and the selector that determines which pods will answer requests sent through the proxy. 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> initializers <Object> pending <[]Object> name <string> result <Object> apiVersion <string> code <integer> details <Object> causes <[]Object> field <string> message <string> reason <string> group <string> kind <string> name <string> retryAfterSeconds <integer> uid <string> kind <string> message <string> metadata <Object> continue <string> resourceVersion <string> selfLink <string> reason <string> status <string> labels <map[string]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> spec <Object> clusterIP <string> externalIPs <[]string> externalName <string> externalTrafficPolicy <string> healthCheckNodePort <integer> loadBalancerIP <string> loadBalancerSourceRanges <[]string> ports <[]Object> name <string> nodePort <integer> port <integer> protocol <string> targetPort <string> publishNotReadyAddresses <boolean> selector <map[string]string> sessionAffinity <string> sessionAffinityConfig <Object> clientIP <Object> timeoutSeconds <integer> type <string> status <Object> loadBalancer <Object> ingress <[]Object> hostname <string> ip <string> [centos@k8s-master ~]$以上输出的内容是经过格式化了的,我们可以根本缩进很容易看到某一个字段从属于关系
查看具体字段通过上面kubectl explain service --recursive可以看到所有的api名称,但是以上仅仅是罗列了所有的api名称,如果想要知道某一个api名称的详细信息,则可以通过kubectl explain <资源对象名称.api名称>的方式来查看,比如以下示例可以查看到service下的spec下的ports字段的信息
[centos@k8s-master ~]$ kubectl explain svc.spec.ports KIND: Service VERSION: v1 RESOURCE: ports <[]Object> DESCRIPTION: The list of ports that are exposed by this service. More info: https://kubernetes.io/docs/concepts/services-networking/service/#virtual-ips-and-service-proxies ServicePort contains information on service's port. FIELDS: name <string> The name of this port within the service. This must be a DNS_LABEL. All ports within a ServiceSpec must have unique names. This maps to the 'Name' field in EndpointPort objects. Optional if only one ServicePort is defined on this service. nodePort <integer> The port on each node on which this service is exposed when type=NodePort or LoadBalancer. Usually assigned by the system. If specified, it will be allocated to the service if unused or else creation of the service will fail. Default is to auto-allocate a port if the ServiceType of this Service requires one. More info: https://kubernetes.io/docs/concepts/services-networking/service/#type-nodeport port <integer> -required- The port that will be exposed by this service. protocol <string> The IP protocol for this port. Supports "TCP", "UDP", and "SCTP". Default is TCP. targetPort <string> Number or name of the port to access on the pods targeted by the service. Number must be in the range 1 to 65535. Name must be an IANA_SVC_NAME. If this is a string, it will be looked up as a named port in the target Pod's container ports. If this is not specified, the value of the 'port' field is used (an identity map). This field is ignored for services with clusterIP=None, and should be omitted or set equal to the 'port' field. More info: https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service