crd_demo.yaml
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
#名称复数形式 加组名 必须与spec匹配 格式 : <名称复数>.<组名>
name: demos.example.com
spec:
#组名
group: example.com
names:
#名称复数形式
plural: demos
#单数形式
singular: demo
#资源名
kind: Demo
#短名字
shortNames:
- dm
#版本配置
versions:
- name: v1
#是否启用
served: true
#只有一个版本可以设置成存储版本 存储在etcd中
storage: true
#定义自定义资源的结构和字段
schema:
#描述资源的属性,包括字段名称、数据类型、是否必填等
openAPIV3Schema:
type: object
properties:
spec:
type: object
#定义字段 apiService 在创建资源时会进行校验
properties:
name:
type: string
replicas:
type: integer
#创建自定义资源时提供状态信息,并在 Kubernetes API 中查询和管理这些状态信息
status:
type: object
properties:
#表示自定义资源的阶段 Running、Pending 等
phase:
type: string
#表示与状态相关的消息
message:
type: string
#kubectl get命令输出中显示的其他列。这些列的值可以使用jsonPath从CRD的状态中提取。
additionalPrinterColumns:
- name: name
type: string
jsonPath: .spec.name
#资源是否限定命名空间 可以是 Namespaced 也可以 Cluster
scope: Namespaced
创建资源对象
apiVersion: "example.com/v1"
kind: Demo
metadata:
name: demo-cr
spec:
# 在此处定义您的自定义资源的特定字段和值
name: name1
replicas: 1