【从入门到放弃-Kubernetes】Kubernetes进阶-CustomResources

前言

上文【从入门到放弃-Kubernetes】Kubernetes进阶-pod水平自动伸缩(hpa),我们学习了如何对标准资源Deployment、replication controller、replica set等内置资源进行g 5 E r水平自动伸缩D Q 9

实际在生产中(一般较大的# p 5 b f ` I % C公司)不太会用到这些内置标准资源,都会使用CustomResources自定义资源进行灵活的扩展,来满足复杂多变的D K * x Q 5 g 7业务需求。

CustomResources是Kubernetes API的扩展,可以通过动态注册,控制资源在集群中的运_ ; z N Q b _行状态。

安装CustomResources后,就可以和内置资源如pod一样,通过kubectl创建和访问对象。

下面我们将CustomR2 r K p ( 9 !esources简称为CR

定义CRD

添加CR通常有两种方式,通过CRm } } J [ cD(CustomResourceDefinitions)的方式和使用聚1 9 Y $ A * j O !API的方式。

CRD的方式使用简单,聚合API的方式更灵活。

选型建议如下:

【从入门到放弃-Kubernetes】Kubernetes进阶-CustomResources

更详细的对比可以在官方文档中查看详情。

这里我们使用CZ - K v 8 KRD的方式创建,g - v y n $有两种版本的CRD定义方式分别如下

apiextensionD ; N $ n } 1 Zs.k8s.io/v1

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
# naO a Mme must match the spec fields below, and be in the form: <plural>.<group>
name: crontabs.stable.example.com
spec:
# group name to use for REST API: /apis/<group>/<verf | ~sion>
group: stable.example.com
# list of versions supported by this CustomResourceDefinition
versions:
- name: v1
# Each version can be enabled/disabled by S1 : } * 7erved fl? ^ $ 8 o S 4 Iag.
sS x L 0 t C } !erved: true
# One and only one version must be marked as the storage version.
storage: true
schema:z , b : * 4 l
openAPIV3Schema:
type: object
propertie] # ~ ^ C q s U Ws:
spT V { H % n aec:
ty~ f L ( % D bpe: object
properties:
cronSpec:
type: string
image:
type: string
replicas:
type: integer
# either Namespaced or Cluster
scope: N= m n k Q k 2amespaced
names:
# plural name to be used in thes 0 1 % L : m [ URL: /apis/<group>/<ve9 j I ? # i m zrsion>/q ~ * o<plural>
plural: crontabs
# singular name to be used as an aliast r Z ] on the CLI and for disp_ V ( % ( xlay
singular: crontab
# kind is normally the CamelCased singular type. Your resource manifests use this.
kind: CronTab
# shortNames allow sa } bhorter string to match your resource on the CLI
shortNames:
- ct

apiextensiA , Rons.k8s.io/v1betaT 3 n Q1

# Deprecated in v1.16 in fa* 0 + 4vor of apiextensions.k8s.io/v1
apiVeO ] $ p L o i ` krsion: aD d ` X B H G Upiextensions.k8s.io/v1beta1
kind: CustomResourceDefi S O x h c 4ni= J A p @tion
metadata:
# name must match the spec fields below, and be in the form: <V # k;plural>.<group>
name: crontabs.stable.exaL ~ _ N N P i a ^mple.com
specb q w B:
# group name to use for R] 6 9 C x L &EST API: /apis/<group>/<version>
group: stable.example.com
# list of versions suppoV ! u Y H 6rted by this CuC R s 7 X 4 S stomReso/ y ] D u ( K -urceDefinition
versions:
- name: v1
# Each version7 Y [ w G can be enE j ? , 4 wabled/disabled by Served flag.
served: true
# One and only one version muH 0 4 U W P r : :st be mar@ 8 Fked as the storage version.
storage: true
# either Namespaced oV | z Xr ClustG  b M S 1 I Jer
scope] G : x m: Namespaced
names:
# plural name to be used in the URL: /apisN x k/<$ R T / { v 0 `;group>/<version>/<plural>
plural: crontabs
# sinI x Z L u ; j cgular name t[ ] Ao be used as an alias on the CLI and fore N } k display
singula. } U 7r: crontab
# kind is normally the CamelCased singular type. Your| L s S resour v * Qrce manj i d b y BifeT # X F # u -sts use this.
kind: CronTab
# shortNames allow shorterQ L y p ] string to match your resourcei $ V F 9 ; on the CLI
shortNames:
- ct
preserveUnknownFields: false
valida~ N c ; , ? N Gtion:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
cronSpec:
type: string
image:
type: string
replicas:
type: integer

创建并验证

# 使用上面任意一版本的yaml文件
kubectl applyt Q n O / . $ -f crd.yaml
# 开启api seI U } 3 H r t ; Drver http代理
kubect| A +l proxy --port=8080 &
# 访问/apis/sta| g k vble.example.com/v1/namespaces/*/crontabs查看
curl http://127.0.0.1:8080/apQ E ? B  l -is/stable.examp7 s ! 6le.com/v1/namespaJ $ sces/*/crontabs
# 查看crd
kubectl get crd

可以看到类似以下输出

【从入门到放弃-Kubernetes】Kubernetes进阶-CustomResources

创建CR对象

apiVersion: "stable.example.com/v1"
kind: CronTK ) o + m { Vab
metadate V C = n _ |a:
name: my-ne] 2 + C S ww-cron-object
spec( 6 8:
cronSpec: "* * * * */5"
image: my-C F $ Aawesome-cron-image
# 创建CR对象
kubectl app/ | ely -f cr-object.yaml
# 查看crontab资源
kubectl get crontab
# 查看crontab yaml文件
kubectl get crontab -o yaml

【从入门到放弃-Kubernetes】Kubernetes进阶-CustomResources

删除CRD

# 删除CRD
kubectl delete -f crd.yaml
# 再次查看crontab资源,会看到Error from server (NotFound): Unable to list "s[ Q + ` z {table.example.com/v1, Resource=crontabs": the servA g S ~ b R ^er could not find the requested resource (get cH S & d Urontabs.stable.example.com)
kubectl get crontabs

总结

本文学习了如何通过CRD来创建CR,实际上通常还需要通过Custom controllers监听资源的变化,在周期内的各个阶段做相; y r 4 A W应的处理。
接下里会开始学习Cu{ / ] Y V U * G 3stom controllers的开发。

更多文章

见我的博客:https://nc2era.com

written by AloofJr,转载请注明出a $ Q m / r Z D