21. Custom Resource Definitions (CRDs) in Kubernetes

21. Custom Resource Definitions (CRDs) in Kubernetes

Custom Resource Definitions (CRDs) extend Kubernetes' functionality by allowing you to define your own custom resources. With CRDs, you can manage application-specific configurations as first-class Kubernetes objects, seamlessly integrating them into the Kubernetes ecosystem.


What Are CRDs?

CRDs are part of Kubernetes' Custom Resource and Custom Controller architecture. A Custom Resource (CR) is a user-defined Kubernetes API object, while a Custom Controller handles the lifecycle of these resources. Together, they enable you to automate application-specific logic.


Why Use CRDs?

  • Flexibility: Define custom APIs for your applications without modifying Kubernetes itself.

  • Automation: Pair CRDs with custom controllers to manage resources programmatically.

  • Scalability: Use Kubernetes' native mechanisms (e.g., kubectl, RBAC) with your custom resources.


Creating and Using CRDs

1. Define a CRD

A CRD is defined as a Kubernetes resource in a YAML manifest. Here’s an example of a CustomResourceDefinition for managing databases:

database-crd.yml

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: databases.example.com
spec:
  group: example.com
  names:
    kind: Database
    listKind: DatabaseList
    plural: databases
    singular: database
  scope: Namespaced
  versions:
  - name: v1
    served: true
    storage: true
    schema:
      openAPIV3Schema:
        type: object
        properties:
          spec:
            type: object
            properties:
              databaseName:
                type: string
              size:
                type: string

Apply the CRD:

kubectl apply -f database-crd.yml


2. Create Custom Resources (CRs)

Once the CRD is registered, you can create instances of the Database resource.

my-database.yml

apiVersion: example.com/v1
kind: Database
metadata:
  name: my-database
spec:
  databaseName: "test-db"
  size: "20Gi"

Apply the custom resource:

kubectl apply -f my-database.yml


3. View and Manage CRs

To view the custom resource:

kubectl get databases

To describe a specific custom resource:

kubectl describe database my-database

To delete a custom resource:

kubectl delete database my-database


Using CRDs with Controllers

To fully automate custom resources, pair the CRD with a controller. Controllers can be implemented using:

  • Kubernetes Operators: Advanced controllers for complex applications.

  • Frameworks: Tools like Kubebuilder or Operator SDK simplify controller development.


Best Practices for CRDs

  1. Validation: Use OpenAPI schema validation to enforce rules on CRs.

  2. Versioning: Define multiple versions of your CRD for backward compatibility.

  3. Namespaces: Use Namespaced scope for most CRDs to avoid conflicts.


Conclusion

Custom Resource Definitions empower you to extend Kubernetes to suit your application’s unique requirements. By combining CRDs with controllers, you can automate complex workflows and integrate application-specific logic into your Kubernetes environment.