Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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 aDatabase 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:
    • APostgreSQL Operator to manage database instances in Kubernetes.

3. Key Differences Between CRDs and Operators

FeatureCRDOperator
PurposeExtends Kubernetes APIAutomates application lifecycle
ComplexityDefines custom resourcesUses CRDs + controllers to manage resources
Use CaseDefine resource structuresAutomate 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 aCustom Resource Definition (CRD) in your Kubernetes cluster, follow these steps:


1. Create the CRD Definition

Save the following YAML asmycrd.yaml:

apiVersion:apiextensions.k8s.io/v1kind:CustomResourceDefinitionmetadata:name:widgets.example.comspec:group:example.comnames:kind:Widgetplural:widgetsscope:Namespacedversions:-name:v1served:truestorage:trueschema:openAPIV3Schema:type:objectproperties:spec:type:objectproperties:size:type:stringcolor:type:string
Enter fullscreen modeExit fullscreen mode

Apply it using:

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

2. Define a Custom Resource (CR)

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

apiVersion:example.com/v1kind:Widgetmetadata:name:my-widgetspec:size:"large"color:"blue"
Enter fullscreen modeExit fullscreen mode

Apply it:

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

3. Verify the Custom Resource

Check if the CRD is created:

kubectl get crds
Enter fullscreen modeExit fullscreen mode

Check the created custom resource:

kubectl get widgets
Enter fullscreen modeExit fullscreen mode

Describe the resource:

kubectl describe widget my-widget
Enter fullscreen modeExit fullscreen mode

4. Clean Up

To remove the CRD:

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

Next Steps

  • Implement acustom controller to manageWidget resources.
  • Extend the CRD with validation rules.
  • Integrate with Kubernetes Operators for automation.

Happy Learning !!!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

####
  • Education
    ☘️
  • Work
    👨‍💻
  • Joined

More fromVivesh

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp