DEV Community

Vivesh
Vivesh

Posted on

Custom Resource Definitions (CRD)

Kubernetes Concepts: Operators and CRDs

1. Custom Resource Definitions (CRDs)

  • What are CRDs? Custom Resource Definitions (CRDs) extend Kubernetes by allowing users to define their own custom resources.
  • Why use CRDs? They enable the management of application-specific resources within Kubernetes, just like native resources (Pods, Services, Deployments).
  • Example Use Case:
    • Defining a Database CRD to manage database instances via Kubernetes API.

2. Kubernetes Operators

  • What is a Kubernetes Operator?
    • An Operator is a controller that automates application management using CRDs.
    • It continuously watches for changes and ensures the desired state of the application.
  • Why use Operators?
    • Automate complex deployments (e.g., databases, monitoring stacks).
    • Handle upgrades, backups, and failure recovery.
  • Example Use Case:
    • A PostgreSQL Operator to manage database instances in Kubernetes.

3. Key Differences Between CRDs and Operators

Feature CRD Operator
Purpose Extends Kubernetes API Automates application lifecycle
Complexity Defines custom resources Uses CRDs + controllers to manage resources
Use Case Define resource structures Automate complex operations (e.g., upgrades, backups)

4. Popular Kubernetes Operators

  • Prometheus Operator – Deploy and manage Prometheus monitoring stack.
  • Elasticsearch Operator – Manage Elasticsearch clusters.
  • Kafka Operator – Handle Kafka brokers in Kubernetes.

To implement a Custom Resource Definition (CRD) in your Kubernetes cluster, follow these steps:


1. Create the CRD Definition

Save the following YAML as mycrd.yaml:

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: widgets.example.com
spec:
  group: example.com
  names:
    kind: Widget
    plural: widgets
  scope: Namespaced
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                size:
                  type: string
                color:
                  type: string
Enter fullscreen mode Exit fullscreen mode

Apply it using:

kubectl apply -f mycrd.yaml
Enter fullscreen mode Exit fullscreen mode

2. Define a Custom Resource (CR)

After creating the CRD, define an instance of the custom resource. Save this as widget.yaml:

apiVersion: example.com/v1
kind: Widget
metadata:
  name: my-widget
spec:
  size: "large"
  color: "blue"
Enter fullscreen mode Exit fullscreen mode

Apply it:

kubectl apply -f widget.yaml
Enter fullscreen mode Exit fullscreen mode

3. Verify the Custom Resource

Check if the CRD is created:

kubectl get crds
Enter fullscreen mode Exit fullscreen mode

Check the created custom resource:

kubectl get widgets
Enter fullscreen mode Exit fullscreen mode

Describe the resource:

kubectl describe widget my-widget
Enter fullscreen mode Exit fullscreen mode

4. Clean Up

To remove the CRD:

kubectl delete crd widgets.example.com
Enter fullscreen mode Exit fullscreen mode

Next Steps

  • Implement a custom controller to manage Widget resources.
  • Extend the CRD with validation rules.
  • Integrate with Kubernetes Operators for automation.

Happy Learning !!!

Top comments (0)