[Kubernetes CRD 实战第一步] 基于CustomResourceDefinition创建一个自定义CRD

导言:
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,加油
 

相关推荐

  1. IntelliJ IDEA中创建定义项目向导

    2024-04-05 03:22:01       43 阅读
  2. C#面:如何创建定义异常?

    2024-04-05 03:22:01       35 阅读
  3. Odoo创建定义UI视图

    2024-04-05 03:22:01       28 阅读
  4. Android如何创建定义回调接口(例3)

    2024-04-05 03:22:01       25 阅读
  5. [RP2040]搭建第一定义项目

    2024-04-05 03:22:01       38 阅读
  6. k8s的API资源对象CustomResourceDefinitionCRD

    2024-04-05 03:22:01       73 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-05 03:22:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-05 03:22:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-05 03:22:01       82 阅读
  4. Python语言-面向对象

    2024-04-05 03:22:01       91 阅读

热门阅读

  1. Tauri 进阶使用与实践指南

    2024-04-05 03:22:01       35 阅读
  2. 第十二题:灌溉

    2024-04-05 03:22:01       40 阅读
  3. gitconfig区分工作和个人账号

    2024-04-05 03:22:01       45 阅读
  4. 生成器、迭代器、可迭代对象

    2024-04-05 03:22:01       33 阅读
  5. leetcode268-Missing Number

    2024-04-05 03:22:01       37 阅读
  6. 用筛选法(埃拉托色尼筛法)求100之内的素数

    2024-04-05 03:22:01       35 阅读
  7. 计算机网络概述

    2024-04-05 03:22:01       38 阅读
  8. 域名被污染了怎么恢复

    2024-04-05 03:22:01       34 阅读