Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Custom Resource Definitions (CRD) in Kubernetes

What is CRD?

Custom Resource Definitions (CRD) allow you to extend Kubernetes capabilities by defining your own resources. CRDs enable the creation of new types of objects in Kubernetes, allowing developers to build applications tailored to their specific needs.

Why Use CRD?

  • Extends Kubernetes API with custom resources.
  • Facilitates application-specific configurations.
  • Supports the development of operators that manage complex applications.

Creating a CRD

Step-by-Step Process

  1. Define the CRD manifest in YAML format.
  2. Apply the CRD manifest using kubectl.
  3. Create instances of the custom resource.

Example CRD Manifest

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: myresources.example.com
spec:
  group: example.com
  versions:
  - name: v1
    served: true
    storage: true
    schema:
      openAPIV3Schema:
        type: object
        properties:
          spec:
            type: object
            properties:
              field1:
                type: string
              field2:
                type: integer
  scope: Namespaced
  names:
    plural: myresources
    singular: myresource
    kind: MyResource
                    

Applying the CRD

kubectl apply -f myresource-crd.yaml

Creating a Custom Resource Instance

apiVersion: example.com/v1
kind: MyResource
metadata:
  name: myresource-instance
spec:
  field1: "value1"
  field2: 42
                    

Applying the Custom Resource

kubectl apply -f myresource-instance.yaml

Best Practices

  • Use a clear naming convention for your CRDs.
  • Version your CRDs to manage changes over time.
  • Define validation schemas to enforce correct usage.
  • Document your CRDs to help users understand their purpose.

FAQ

What is the difference between a CRD and an Operator?

A CRD is a way to define a new resource type in Kubernetes, while an Operator uses CRDs to manage complex applications by encapsulating the logic for deploying and managing those applications.

Can CRDs be versioned?

Yes, CRDs can be versioned by defining multiple versions in the CRD manifest. This allows you to support changes and upgrades to your custom resources.

How do I update a CRD?

To update a CRD, modify the YAML manifest and re-apply it using kubectl apply -f crd.yaml. Ensure that the updated schema is compatible with existing resources.