自定義資源(CRD), 是對Kubernetes API的擴展,kubernetes中的每個資源都是一個API對象的集合,例如我們在YAML文件裏定義的那些spec都是對kubernetes中的資源對象的定義,所有的自定義資源可以跟kubernetes中內建的資源一樣使用kubectl操作
CRD
參考下面的CRD,crd.yaml:
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
# 名稱必須符合下面的格式:<plural>.<group>
name: crontabs.stable.example.com
spec:
# REST API使用的組名稱:/apis/<group>/<version>
group: stable.example.com
# REST API使用的版本號:/apis/<group>/<version>
version: v1
# Namespaced或Cluster
scope: Namespaced
names:
# URL中使用的複數名稱: /apis/<group>/<version>/<plural>
plural: crontabs
# CLI中使用的單數名稱
singular: crontab
# CamelCased格式的單數類型。在清單文件中使用
kind: CronTab
# CLI中使用的資源簡稱
shortNames:
- ct
創建該CRD:
kubectl create -f crd.yaml
查看創建結果:
kubectl get crd
the server doesn't have a resource type "crd"
why? 爲什麼, 不可思議,完全按照example 下來,然後各種思緒激情碰撞。。。
記得以前看文檔時:CRD 是後來加進來的,具體哪個版本不記得(只是有這個印象),好, 我們驗證下當前自己環境中的:
kubectl version
Client Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.1", GitCommit:"82450d03cb057bab0950214ef122b67c83fb11df", GitTreeState:"clean", BuildDate:"2016-12-14T00:57:05Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.0", GitCommit:"641856db18352033a0d96dbc99153fa3b27298e5", GitTreeState:"clean", BuildDate:"2019-03-25T15:45:25Z", GoVersion:"go1.12.1", Compiler:"gc", Platform:"linux/amd64"}
再查查資料,走起。。。
Kubernetes1.6版本中包含一個內建的資源叫做TPR(ThirdPartyResource),可以用它來創建自定義資源,但該資源在kubernetes1.7版本中已被CRD(CustomResourceDefinition)取代。
確實是,自己的當前版本太低了,讓我們升級一把試試哈哈。。
brew install kubectl
再次驗證下:
kubectl version
Client Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.3", GitCommit:"5e53fd6bc17c0dec8434817e69b04a25d8ae0ff0", GitTreeState:"clean", BuildDate:"2019-06-07T09:55:27Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.0", GitCommit:"641856db18352033a0d96dbc99153fa3b27298e5", GitTreeState:"clean", BuildDate:"2019-03-25T15:45:25Z", GoVersion:"go1.12.1", Compiler:"gc", Platform:"linux/amd64"}
kubectl get crd
NAME CREATED AT
crontabs.stable.example.com 2019-06-17T06:18:00Z
students.bolingcavalry.k8s.io 2019-06-17T06:01:57Z