Kubernetes API 资源使用

Kubernetes使用声明式的 API 让系统更加健壮。但是这样也就意味着我们想要系统执行某些操作就需要通过使用CLI或者REST API来创建一个资源对象,为此,我们需要定义 API 资源的名称、组和版本等信息。但是很多用户就会为此感到困惑了,因为有太多的资源、太多的版本、太多的组了,这些都非常容易产生混淆。如果我们通过 YAML 文件定义过 Deployment 这样的资源清单文件的话,那么你应该会看到apiVersion: apps/v1beta2apiVersion: apps/v1等等这样的信息,那么我们到底应该使用哪一个呢?哪一个才是正确的呢?如何检查Kubernetes集群支持哪些?其实我们使用kubectl工具就可以来解决我们的这些疑惑。

一、API Resources

我们可以通过下面的命令来获取Kubernetes集群支持的所有 API 资源:

[root@brazil-11627 ~]# kubectl api-resources
NAME                              SHORTNAMES       APIVERSION                             NAMESPACED   KIND
batchbindings                                      v1                                     false        BatchBinding
bindings                                           v1                                     true         Binding
componentstatuses                 cs               v1                                     false        ComponentStatus
configmaps                        cm               v1                                     true         ConfigMap
endpoints                         ep               v1                                     true         Endpoints
events                            ev               v1                                     true         Event
limitranges                       limits           v1                                     true         LimitRange
namespaces                        ns               v1                                     false        Namespace
nodes                             no               v1                                     false        Node
persistentvolumeclaims            pvc              v1                                     true         PersistentVolumeClaim
persistentvolumes                 pv               v1                                     false        PersistentVolume
pods                              po               v1                                     true         Pod
podtemplates                                       v1                                     true         PodTemplate
replicationcontrollers            rc               v1                                     true         ReplicationController
resourcequotas                    quota            v1                                     true         ResourceQuota
secrets                                            v1                                     true         Secret
serviceaccounts                   sa               v1                                     true         ServiceAccount
services                          svc              v1                                     true         Service
……

上面的命令输出了很多有用的信息:

  • SHORTNAMES – 资源名称的简写,比如 deployments 简写就是 deploy,我们可以将这些快捷方式与kubectl一起使用
  • APIGROUP – 我们可以查看官方文档以了解更多信息,但简而言之,您将在yaml文件中使用它像apiVersion:<APIGROUP>/v1
  • KIND – 资源名称
  • VERBS – 可用的方法(加 -o wide输出该列),在您想要定义ClusterRole RBAC规则时也很有用,您还可以选择获取特定 API 组的 API 资源,例如:
[root@brazil-11627 ~]# kubectl api-resources --api-group apps -o wide
NAME                  SHORTNAMES   APIVERSION   NAMESPACED   KIND                 VERBS
controllerrevisions                apps/v1      true         ControllerRevision   [create delete deletecollection get list patch update watch]
daemonsets            ds           apps/v1      true         DaemonSet            [create delete deletecollection get list patch update watch]
deployments           deploy       apps/v1      true         Deployment           [create delete deletecollection get list patch update watch]
replicasets           rs           apps/v1      true         ReplicaSet           [create delete deletecollection get list patch update watch]
statefulsets          sts          apps/v1      true         StatefulSet          [create delete deletecollection get list patch update watch]

对于上面的每种资源类型,我们都可以使用kubectl explain命令来获取有关的资源详细信息:

kubectl explain pod

#If you to want to check more about spec section (sub-resource) of POD, use
kubectl explain pod.spec

# For toleration
kubectl explain pod.spec.tolerations

# and if you want to get check values and its input type use
kubectl explain pod.spec.tolerations.value

kubectl explain命令非常有用,特别是在我们不知道该如何编写YAML文件的时候,就可以使用改命令来帮助我们获得更多提示信息。

需要注意的是explain命令可能会显示旧的group/version,我们可以通过--api-version参数显示设置它,比如: 请注意,explain可能会显示旧组/版本,但您可以使用–api-version显式设置它,例如:

kubectl explain replicaset --api-version apps/v1

二、API Versions

我们也可以使用下面的命令来获取集群支持的所有 API 版本:

[root@brazil-11627 ~]# kubectl api-versions
admissionregistration.k8s.io/v1
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
……

输出结果是以group/version的方式呈现的,可以通过查看此页面了解更多有关Kubernetes中 API 版本控制的信息。

有的时候,我们只想检查特定的group/version是否可以用于某些资源即可,大多数的资源都有可用的GET方法,所以我们只需要尝试获取下资源,同时提供 API 的 version 和 group 即可验证,kubectl get <API_RESOURCE_NAME>.<API_VERSION>.<API_GROUP>,例如:

[root@brazil-11627 ~]# kubectl get deployments.v1.apps -n kube-system
NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
coredns                  1/2     2            1           6d1h
everest-csi-controller   1/2     2            1           6d1h

如果资源不存在指定的group/version组合或者资源根本不存在,我们将会收到错误信息:

[root@brazil-11627 ~]# kubectl get deployments.v1beta.apps -n kube-system
error: the server doesn't have a resource type "deployments"

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注