CRD – Custom Resource Definition

A CRD (Custom Resource Definition) is a way to extend the Kubernetes API by defining your own custom objects, just like built-in ones such as Pod, Deployment, etc.

Once you create a CRD:

  • Kubernetes adds a new API endpoint (e.g., /apis/mydomain.com/v1/widgets)
  • You can use kubectl to create, read, update, and delete those new resource types
  • They behave like native resources, but with custom logic (optional)

🔧 Why Use CRDs?

CRDs are used when:

  • Built-in resources are not enough
  • You want to represent custom domain-specific objects
  • You’re building a Kubernetes Operator (CRD + Controller pattern)

🏗️ Anatomy of a CRD

A CRD YAML has several important sections:

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: widgets.mycompany.com # format: plural.group

spec:
group: mycompany.com
scope: Namespaced # or Cluster
names:
plural: widgets
singular: widget
kind: Widget # Used in YAML's `kind: Widget`
shortNames:
- wd

versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
color:
type: string
size:
type: integer

📌 Key Fields Explained

FieldDescription
groupDefines API group (like apps, batch, etc.)
namesTells Kubernetes how to refer to the resource
scopeNamespaced or Cluster-wide
versionsVersioning of the CRD (v1, v1alpha1, etc.)
schemaValidates the structure of custom resources
kindUsed in kind: field of custom resource YAMLs
shortNamesShorthand for kubectl get commands

📦 Example Custom Resource (CR)

apiVersion: mycompany.com/v1
kind: Widget
metadata:
name: my-widget
spec:
color: red
size: 42

Once the CRD is registered, you can apply this with kubectl apply -f.


🧠 What Happens When You Create a CRD?

  1. Kubernetes API server registers a new resource type
  2. You get a new API endpoint (e.g., /apis/mycompany.com/v1/widgets)
  3. You can kubectl get widgets or create instances (Custom Resources)
  4. You can optionally write a controller/operator to act on those objects

🤖 CRD + Controller = Kubernetes Operator

A Controller:

  • Watches for changes to your CRD (e.g., a new Widget is created)
  • Executes logic (e.g., spins up Pods, validates config, provisions infra)

This is how tools like:

  • ArgoCD
  • Prometheus Operator
  • Cert-Manager
    … all work — via CRDs and controllers.

✅ Benefits of CRDs

BenefitDescription
Extends K8sDefine domain-specific objects
First-class supportIntegrates with kubectl, RBAC, audit logs, CRDs
Declarative APIsManaged just like native objects
EvolvableSupports multiple versions, validation, OpenAPI schemas

⚠️ Limitations / Gotchas

GotchaDetail
No logic on its ownCRDs define structure, not behavior
Requires controllersTo react to changes, you need an Operator
Schema mattersBad OpenAPI schema = hard to debug validation
Upgrades need careChanging versions requires versioning strategy (conversion webhooks optional)

🧪 Tooling Around CRDs

ToolPurpose
kubectlManage CRDs and CRs
kubebuilderScaffold Go-based operators
Operator SDKBuild full-featured Kubernetes operators
KopfPython framework for CRD-based operators
controller-runtimeGo library for writing controllers
K9sTUI to view/interact with CRDs and resources

📌 Real-World Use Cases

CRDPurpose
certificates.cert-manager.ioTLS certificates automation
prometheusrules.monitoring.coreos.comPrometheus alerting rules
applications.argoproj.ioArgoCD apps
installations.operator.tigera.ioCalico network configuration

Leave a comment