Kubernetes-如何知道最新支持的API版本

s g*_*s g 6 kubernetes kubectl

给定k8s集群版本,是否有一张表格可以告诉我应该使用哪一组API版本?Kubernetes文档始终假设我始终拥有一个不错的,最新的集群(在撰写本文时为1.12),但是平台提供商并不总是生活在这一前沿,因此它可能很快就令人沮丧。

更好的是,是否存在kubectl我可以运行的命令,该命令可以使我集群告知每个资源类型及其最新支持的API版本?

小智 20

我认为kubectl api-versions是一个更简单的选择:

 kubectl api-versions
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
apps/v1beta1
apps/v1beta2
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1beta1
coordination.k8s.io/v1
coordination.k8s.io/v1beta1
events.k8s.io/v1beta1
extensions/v1beta1
networking.k8s.io/v1
networking.k8s.io/v1beta1
node.k8s.io/v1beta1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
scheduling.k8s.io/v1
scheduling.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1
Run Code Online (Sandbox Code Playgroud)

  • 当要使用的名称很明显时,这很方便,但是您如何从输出中知道部署应该使用扩展/v1beta1,这从接受的答案中非常清楚。 (2认同)

s g*_*s g 9

要获取所有资源类型及其关联版本的列表,请运行以下命令:

for kind in `kubectl api-resources | tail +2 | awk '{ print $1 }'`; do kubectl explain $kind; done | grep -e "KIND:" -e "VERSION:"

它应该产生像

KIND:     Binding
VERSION:  v1
KIND:     ComponentStatus
VERSION:  v1
KIND:     ConfigMap
VERSION:  v1
KIND:     Endpoints
VERSION:  v1
KIND:     Event
VERSION:  v1
...
Run Code Online (Sandbox Code Playgroud)

如@Rico所述,它们的键位于kubectl explain命令中。这可能有点脆弱,因为它取决于打印输出的格式,但是它适用于kubernetes 1.9.6。

此外,信息可以从kubernetes API文档(为每个版本的链接)低效率的方式聚集在这里找到- https://kubernetes.io/docs/reference/#api-reference

  • 不过要小心。有时,同一个对象在两个不同的 API 中可用(例如,当前“Ingress”在“extensions”和“networking.k8s.io”中),并且它可能默认为较旧的(例如 Ingress 显示“extensions/v1beta1”,但“networking.k8s.io”)。 k8s.io/v1beta1` 更合适)。 (2认同)

Ric*_*ico 8

这就是kubectl explain <resource>命令。例如对于豆荚:

$ kubectl explain pod
KIND:     Pod
VERSION:  v1   <==  API version

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

...
Run Code Online (Sandbox Code Playgroud)

部署类似:

$ kubectl explain deploy
KIND:     Deployment
VERSION:  extensions/v1beta1 <== API Version

DESCRIPTION:
     DEPRECATED - This group version of Deployment is deprecated by
     apps/v1beta2/Deployment. See the release notes for more information.
     Deployment enables declarative updates for Pods and ReplicaSets.

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
...
Run Code Online (Sandbox Code Playgroud)

此外,例如这里是v1.12 API 参考