Sobes.tech
Junior — Middle

How can Custom Resource Definitions be implemented and modified in Kubernetes?

sobes.tech AI

Answer from AI

Custom Resource Definitions (CRD) in Kubernetes allow extending the cluster's API by adding your own resource types. To create and modify a CRD, you need to:

  1. Define a YAML manifest describing the new resource, including spec with fields, validation schema, and versioning.
  2. Apply the manifest using kubectl apply -f crd.yaml.
  3. After creating the CRD, you can create objects of the new type.
  4. To modify a CRD, update the YAML manifest and apply it again. It is important to consider version compatibility and data migration.

Example of a simple CRD:

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: widgets.example.com
spec:
  group: example.com
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                size:
                  type: string
  scope: Namespaced
  names:
    plural: widgets
    singular: widget
    kind: Widget
    shortNames:
    - wd

After creating such a CRD, you can create Widget objects and manage them.