导言:
Kubernetes(简称K8s)是一个流行的容器编排平台,它提供了丰富的功能来管理和部署容器化应用程序。除了支持核心的资源对象(如Pod、Deployment、Service等),Kubernetes还允许用户定义自己的自定义资源,以满足特定应用的需求。

自定义资源是Kubernetes中的一种扩展机制,它允许我们定义和使用与核心资源类似的自定义对象。而CustomResourceDefinition(CRD)则是定义自定义资源的关键组件。在本篇博客中,我们将介绍如何使用CRD来创建自定义资源。

环境信息:

操作系统:22.04.1-Ubuntu  Server

k8s版本:v1.23.0

1. 创建crd

创建一个名为person.yaml的文件,具体代码如下:

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  # name must match the spec fields below, and be in the form: <plural>.<group>
  # stable.example.com 为分组
  name: persons.stable.example.com
spec:
  # group name to use for REST API: /apis/<group>/<version>
  group: stable.example.com
  # list of versions supported by this CustomResourceDefinition
  versions:
    - name: v1
      # Each version can be enabled/disabled by Served flag.
      served: true
      # One and only one version must be marked as the storage version.
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              # 下面的属性可以自定义 类似与Java中的变量
              properties:
                name:
                  type: string
                school:
                  type: string
                replicas:
                  type: integer
  # either Namespaced or Cluster
  scope: Namespaced
  names:
    # plural name to be used in the URL: /apis/<group>/<version>/<plural>
    plural: persons
    # singular name to be used as an alias on the CLI and for display
    singular: person
    # kind is normally the CamelCased singular type. Your resource manifests use this.
    # 这里要大写,类似于Java中的类
    kind: Person
    # shortNames allow shorter string to match your resource on the CLI
    # 简称
    shortNames:
    - per

然后执行命令:

kubectl apply -f person.yaml

显示:

customresourcedefinition.apiextensions.k8s.io/persons.stable.example.com created

ok,继续

2. Create custom objects

创建一个名为object-person.yaml的文件,具体代码如下:

apiVersion: stable.example.com/v1
kind: Person
metadata:
  name: my-person
spec:
  name: "李四"
  school: "山东大学"

执行命令:

kubectl apply -f object-person.yaml

显示 person.stable.example.com/my-person created

接下来可以查看

$ kubectl get person
NAME        AGE
my-person   39s
$ kubectl get per
NAME        AGE
my-person   42s

 执行:

$ kubectl get per -o yaml

显示:

apiVersion: v1
items:
- apiVersion: stable.example.com/v1
  kind: Person
  metadata:
    annotations:
      kubectl.kubernetes.io/last-applied-configuration: |
        {"apiVersion":"stable.example.com/v1","kind":"Person","metadata":{"annotations":{},"name":"my-person","namespace":"default"},"spec":{"name":"李四","school":"山东大学"}}
    creationTimestamp: "2024-04-03T15:33:39Z"
    generation: 1
    name: my-person
    namespace: default
    resourceVersion: "1200748"
    uid: 2e32605a-ecfa-495e-b464-ecde7fdf21b5
  spec:
    name: 李四
    school: 山东大学
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""

到这里一个类型为Person的CRD就创建成功了,希望可以帮到你

接下来我们下一篇更新创建Person 的controller,加油
 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐