【go云原生】Kubernetes二开:自定义资源crd
最近更新:2025-01-08
|
字数总计:1.6k
|
阅读估时:7分钟
|
阅读量:次
- Kubernetes二开:自定义资源crd
- k8s golang客户端client-go基本结构
- kustomize声明式资源管理
- 手动创建一个crd资源对象
- 通过kubebuilder框架开发自定义资源
Kubernetes二开:自定义资源crd
k8s golang客户端client-go基本结构
- 依赖安装
1
| go get k8s.io/client-go@v0.27.4
|
- 源码结构
- applyconfigurations: 构建一个yaml资源配置对象,也可以序列化成yaml文本
- discovery:服务发现
- dynamic:动态客户端,用来操作自定义资源
- examples:官方提供的案例
- informers:本地缓存与索引机制,我们的k8s一般只有一个apiserver服务,存在在master节点,大量请求会扛不住,所以需要本地缓存与索引机制。
- kubernetes:所有api的client side,不同的资源会有对应的不同的客户端,举例是Deployment有Deployment客户端,DaemonSet有DaemonSet客户端。
- listers:资源列表
- metadata:元数据信息
- openapi和openapi3:api规范
- plugin:api授权
- rest:rest客户端
- restmapper:rest资源的映射
- scale:伸缩操作
- tools: 工具
- 如果想做k8s二开,那么client-go的源码要了解的比较透彻,应用的比较灵活。
kustomize声明式资源管理
- 需求:使用配置差异化部署不同环境下的代码
- kustomize参考布局,只是参考布局,和目录没有实际关系。kustomize的作用:管理和组织各种资源的yaml。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| mkdir kustomize cd kustomize mkdir base cd base (base):vim Deployment.yaml
apiVerison: v1 kind: Namespace metadata: name: kustomize --- apiVersion: apps/v1 kind: Deployment metadata: namespace: kustomize name: kustomize-deploy labels: name: kustomize-deploy spec: replicas: 2 selector: matchLabels: name: kustomzie-pod template: matedata: namespace: kustomize labels: name: kustomize-pod spec: containers: - name: kustomize-c image: alpine: 3.18 imagePullPolicy: Always env: - name: env1 value: v1 workdingDir: /app tty: true stdin: true - name: kustomize-c1 image: ubuntu:latest imagePullPolicy: Always workdingDir: /app
cd .. mkdir test cd test (test)kustomize create cd ../base (base)kustomize create (base)vim kustomization.yaml
apiVersion: kustomize.config.k8s.io/vlbetal kind: Kustomization resources: - Deployment.yaml
(base)kustomize build . cd ../test (test)vim kustomization.yaml
apiVersion: kustomize.config.k8s.io/vlbetal kind: Kustomization resources: - ../base namespace: test namePrefix: pre- nameSuffix: -suffix commonLabels: someName: someValue owner: alice app: bingo images: - name: alpine newName: my-registry/my-postgres newTag: v1 - name: ubuntu newName: ubuntu newTag: 1.8.0
(test)kustomize build .
|
手动创建一个crd资源对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| mkdir work cd work vim resourcedefinition.yaml
apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: name: crontabs.stable.example.com spec: group: stable.example.com versions: - name: v1 served: true storage: true schema: openAPIV3Schema: type: object properties: spec: type: object properties: cronSpec: type: string image: type: string replicas: type: interget scope: Namespaced names: plural: crontabs singular: crontabs kind: CronTab shortNames: - ct
kubectl apply -f resourcedefinition.yaml
kubectl explain crontabs kubectl explain crontabs.spec
vim mycrontab.yaml
apiVersion: stable.example.com/v1 kind: CronTab metadata: name: my-new-cron-object spec: cronSpec: "*****/5" image: my-awesome-cron-image
kubectl apply -f mycrontab.yaml kubectl get crontabs.stable.example.com my-new-cron-object -o yaml
kubectl delete -f mycrontab.yaml kubectl delete -f resourcedefinition.yaml
|
通过kubebuilder框架开发自定义资源
- kubebuilder作用
- 开发控制器
- api定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| curl -L -o kubebuilder "https://go.kubebuilder.io/dl/latest/$(go env GOOS)/$(go env GOARCH)" chmod +x kubebuilder && mv kubebuilder /usr/local/bin/
mkdir crontab-crd cd crontab-crd kubebuilder init --domain my.domain --repo my.domain/crontab-crd go mod tidy
kubebuilder create api --group crd --version v1 --kind Crontab
vim ./api/v1/crontab_types.go
|
- ./api/v1/crontab_types.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| type CrontabSpec struct { Schedule string `json:"schedule"` StartingDeadlineSeconds *int64 `json:"startingDeadlineSeconds,omitempty"` ConcurrencyPolicy ConcurrencyPolicy `json:"concurrencyPolicy,omitempty"` Suspend *bool `json:"suspend,omitempty"` JobTemplate batchv1.JobTemplateSpec `json:"jobTemplate"` SuccessfulJobsHistoryLimit *int32 `json:"successfulJobHistoryLimit,omitempty"` FailedJobsHistoryLimit *int32 `json:"failedJobsHistoryLimit,omitempty"` TimeZone *string `json:"timeZone"` } type ConcurrencyPolicy string const( AllowConcurrent ConcurrencyPolicy = "Allow" ForbidConcurrent ConcurrencyPolicy = "Forbid" ReplaceConcurrent ConcurrencyPolicy = "Replace" ) type CrontabStatus struct{ Active []corev1.ObjectReference `json:"active,omitempty"` LastScheduleTime *metav1.Time `json:"lastScheduleTime,omitempty"` LastSuccessfulTime *metav1.Time `json:"lastSuccessfulTime,omitempty"` } type Crontab struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"`
Spec CrontabSpec `json:"spec,omitempty"` Status CrontabStatus `json:"status,omitempty"` } type CrontabList struct { metav1.TypeMeta `json:",inline"` metav1.ListMeta `json:"metadata,omitempty"` Items []Crontab `json:"items"` } func init(){ SchemeBuilder.Register(&Crontab{}, &CrontabList{}) }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| make manifests kubectl apply -f ./config/crd/bases/crd.my.domain_crontabs.yaml kubectl create -f ./config/crd/bases/crd.my.domain_crontabs.yaml
vim ./config/samples/crd_v1_crontab.yaml
apiVersion: crd.my.domain/v1 kind: Crontab metadata: labels: app.kubernetes.io/name: crontab app.kubernetes.io/instance: crontab-sample app.kubernetes.io/part-of: crontab-crd app.kubernetes.io/managed-by: kustomize app.kubernetes.io/created-by: crontab-crd name: crontab-sample spec:
schedule:"*/10 *****" startingDeadlineSeconds: 5 concurrencyPolicy: Allow jobTemplate: spec: template: spec: containers: - name: hello image: busybox args: -/bin/sh - -c - date; echo Hello from the Kubernetes cluster restartPolicy: OnFailure
kubectl apply -f ./config/samples/crd_v1_crontab.yaml kubectl delete -f ./config/samples/crd_v1_crontab.yaml
vim ./internal/controller/crontab_controller.go
1. 添加资源权限 2. 完成协调方法,包括更新job状态,返回调度时间等
kubebuilder create webhook --group crd --version v1 --kind Crontab --defaulting --programmatic-validation vim ./api/v1/crontab_webhook.go
make manifests
|
2025-01-08
该篇文章被 Cleofwine
归为分类:
Go云原生