應(yīng)該使用哪個 Group 和 Version?
日常使用的過程中我們經(jīng)常碰到使用 YAML 定義 deployment等資源文件時躲履, 經(jīng)常弄不清楚資源的版本以及資源使用的組等信息厨剪, 如apiVersion: rbac.authorization.k8s.io/v1beta1
痰洒。
那么我們到底應(yīng)該使用哪一個呢?哪一個才是正確的呢瘟裸?如何檢查Kubernetes集群支持哪些客叉?其實我們使用kubectl工具就可以來解決我們的這些疑惑。
01.獲取kubernetes支持API resource
我們使用如下指令獲取當(dāng)前版本的kubernetes支持哪些KIND话告, 屬于哪個APIGROUP以及每個KIND支持哪些方法兼搏。
$ kubectl api-resources -o wide
NAME SHORTNAMES APIGROUP NAMESPACED KIND VERBS
selfsubjectaccessreviews authorization.k8s.io false SelfSubjectAccessReview [create]
selfsubjectrulesreviews authorization.k8s.io false SelfSubjectRulesReview [create]
subjectaccessreviews authorization.k8s.io false SubjectAccessReview [create]
horizontalpodautoscalers hpa autoscaling true HorizontalPodAutoscaler [create delete deletecollection get list patch update watch]
cronjobs cj batch true CronJob [create delete deletecollection get list patch update watch]
jobs batch true Job [create delete deletecollection get list patch update watch]
certificatesigningrequests csr certificates.k8s.io false CertificateSigningRequest [create delete deletecollection get list patch update watch]
leases coordination.k8s.io true Lease [create delete deletecollection get list patch update watch]
endpointslices discovery.k8s.io true EndpointSlice [create delete deletecollection get list patch update watch]
events ev events.k8s.io true Event [create delete deletecollection get list patch update watch]
ingresses ing extensions true Ingress [create delete deletecollection get list patch update watch]
ingressclasses networking.k8s.io false IngressClass [create delete deletecollection get list patch update watch]
ingresses ing networking.k8s.io true Ingress [create delete deletecollection get list patch update watch]
networkpolicies netpol networking.k8s.io true NetworkPolicy [create delete deletecollection get list patch update watch]
runtimeclasses node.k8s.io false RuntimeClass [create delete deletecollection get list patch update watch]
poddisruptionbudgets pdb policy true PodDisruptionBudget [create delete deletecollection get list patch update watch]
podsecuritypolicies psp policy false PodSecurityPolicy [create delete deletecollection get list patch update watch]
clusterrolebindings rbac.authorization.k8s.io false ClusterRoleBinding [create delete deletecollection get list patch update watch]
clusterroles rbac.authorization.k8s.io false ClusterRole [create delete deletecollection get list patch update watch]
rolebindings rbac.authorization.k8s.io true RoleBinding [create delete deletecollection get list patch update watch]
roles rbac.authorization.k8s.io true Role [create delete deletecollection get list patch update watch]
priorityclasses pc scheduling.k8s.io false PriorityClass [create delete deletecollection get list patch update watch]
csidrivers storage.k8s.io false CSIDriver [create delete deletecollection get list patch update watch]
..........
上面的命令輸出了很多有用的信息:
- SHORTNAMES - 資源名稱的簡寫,比如 deployments 簡寫就是 deploy沙郭,我們可以將這些快捷方式與kubectl一起使用
- APIGROUP - 我們可以查看官方文檔以了解更多信息佛呻,但簡而言之,您將在yaml文件中使用它像apiVersion:<APIGROUP>/v1
- KIND - 資源名稱
- VERBS - 可用的方法病线,在您想要定義ClusterRole RBAC規(guī)則時也很有用吓著,您還可以選擇獲取特定 API 組的 API 資源,例如:
$ kubectl api-resources --api-group apps -o wide
NAME SHORTNAMES APIGROUP NAMESPACED KIND VERBS
controllerrevisions apps true ControllerRevision [create delete deletecollection get list patch update watch]
daemonsets ds apps true DaemonSet [create delete deletecollection get list patch update watch]
deployments deploy apps true Deployment [create delete deletecollection get list patch update watch]
replicasets rs apps true ReplicaSet [create delete deletecollection get list patch update watch]
statefulsets sts apps true StatefulSet
對于上面的每種資源類型送挑,我們都可以使用kubectl explain命令來獲取有關(guān)的資源詳細(xì)息:
$ kubectl explain configmap
DESCRIPTION:
ConfigMap holds configuration data for pods to consume.
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
data <object>
Data contains the configuration data. Each key must consist of alphanumeric
characters, '-', '_' or '.'.
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
kubectl explain命令非常有用绑莺,特別是在我們 不知道該如何編寫YAML文件的時候,就可以使用改命令來幫助我們獲得更多提示信息 惕耕。
需要注意的是explain命令可能會顯示舊的group/version纺裁,我們可以通過--api-version參數(shù)顯示設(shè)置它,比如: 請注意司澎,explain可能會顯示舊組/版本欺缘,但您可以使用–api-version顯式設(shè)置它,例如:
$ kubectl explain replicaset --api-version apps/v1
02.獲取支持的API VERSION
上節(jié)最后說到我們在查看資源的explain時可能并不知道集群支持的版本挤安, 可以使用如下指令獲取當(dāng)前集群支持的 API 版本谚殊。
$ 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
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1beta1
coordination.k8s.io/v1
coordination.k8s.io/v1beta1
discovery.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
03.kubernetes REST path的構(gòu)成
kubernetes 為了提供更好的擴展性所以才有了Apigroup的概念, 它是如何對外提供rest接口的呢蛤铜?
目前使用的apigroup:
- 核心組络凿, 對應(yīng)的
ApiVersion: v1
對應(yīng)的rest path為/api/v1
- 其它的group的rest path為
/apis/$GROUP_NAME/$VERSION
, 對應(yīng)的apiVersion:$GROUP_NAME/$VERSION
(e.g. apiVersion: batch/v1)
04.啟用/關(guān)閉 apigroup
默認(rèn)情況下有一些apigroup是啟用狀態(tài), 可以在apiserver中指定--runtime-config
參數(shù)啟用/關(guān)閉apigroup昂羡。
- 關(guān)閉 batch/v1 , 設(shè)置
--runtime-config=batch/v1=false
- 啟用 batch/v2alpha1 , 設(shè)置
--runtime-config=batch/v2alpha1