CRDs

Difficulty: Advanced

Question

What are Custom Resource Definitions (CRDs) in Kubernetes, how do they work, and why are they used?

Answer

A CustomResourceDefinition (CRD) extends the Kubernetes API with a new resource type, such as Certificate, KafkaTopic or PostgresCluster. Once the CRD is applied, the API server serves the new kind like any built-in object: you can kubectl get, apply and watch it, protect it with RBAC, and store it in etcd. A CRD on its own only stores data; a controller (often an operator) watches those objects and makes something happen.

What a CRD defines

A CRD (apiextensions.k8s.io/v1) describes:

  • Group and names: for example group billing.example.com, kind Invoice, plural invoices, optional shortNames
  • Scope: Namespaced or Cluster
  • Versions: each with served and exactly one with storage: true, so you can evolve from v1alpha1 to v1
  • Schema: a structural OpenAPI v3 schema that validates fields, types, enums and required properties. CEL rules (x-kubernetes-validations) add cross-field checks without a webhook.
  • Subresources: status (so controllers update status separately from spec) and scale (so HPA and kubectl scale work)
  • Printer columns: what kubectl get shows

After you apply it, the API server creates REST endpoints such as /apis/billing.example.com/v1/namespaces/default/invoices.

Why teams use CRDs

CRDs let you describe higher-level things declaratively, in the same style as Deployments, and reuse all Kubernetes machinery: kubectl, RBAC, audit logs, GitOps, kubectl wait, and watches. Much of the modern ecosystem is built on them:

  • cert-manager: Certificate, Issuer
  • Prometheus Operator: ServiceMonitor, PrometheusRule
  • Argo CD: Application
  • Gateway API: Gateway, HTTPRoute are shipped as CRDs rather than built in
  • Database operators like CloudNativePG: Cluster, Backup

CRD vs other extension options

OptionUse whenCost
ConfigMapSimple configuration blobsNo schema, no dedicated API
CRDNew declarative resource typesEasy, no extra server
Aggregated API serverNeed custom storage or non-CRUD behaviour (like metrics-server)Must run and secure your own API server

Gotchas

  • Deleting a CRD deletes every custom resource of that type in the cluster. Treat CRD removal as a data deletion.
  • Changing schema across versions needs a conversion strategy: None works only when versions have the same shape; otherwise you run a conversion webhook.
  • Too many large custom objects put load on etcd and the API server, so do not use CRs as a general-purpose database.
  • Install order matters in GitOps: the CRD must exist before any resource of that kind is applied.

Code examples

A CRD with schema, status and printer columns

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: invoices.billing.example.com   # <plural>.<group>
spec:
  group: billing.example.com
  scope: Namespaced
  names:
    kind: Invoice
    plural: invoices
    singular: invoice
    shortNames: [inv]
  versions:
    - name: v1
      served: true
      storage: true
      subresources:
        status: {}
      additionalPrinterColumns:
        - name: Amount
          type: integer
          jsonPath: .spec.amountPaise
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              required: [customerId, amountPaise]
              properties:
                customerId: { type: string }
                amountPaise: { type: integer, minimum: 1 }

The name must be plural.group; the schema rejects invoices without a customer or with a zero amount.

Using the new resource

kubectl apply -f invoice-crd.yaml
kubectl api-resources --api-group=billing.example.com

cat <<EOF | kubectl apply -f -
apiVersion: billing.example.com/v1
kind: Invoice
metadata:
  name: inv-1001
spec:
  customerId: cust-42
  amountPaise: 49900
EOF

kubectl get inv

The custom kind behaves like a built-in one; a controller would watch Invoices and act on them.

Key points

  • A CRD adds a new API type; custom resources are the instances of it
  • CRDs store and validate data; a controller provides behaviour
  • apiextensions.k8s.io/v1 requires a structural OpenAPI v3 schema
  • Use the status subresource and CEL validation rules for well-behaved APIs
  • Deleting a CRD deletes all of its custom resources

Common follow-up questions

  • What is the difference between a CRD and an operator?
  • How do you version a CRD from v1alpha1 to v1 without breaking users?
  • When would you build an aggregated API server instead of a CRD?
  • How do CEL validation rules in CRDs work?

Interview tips

  • Say early that a CRD alone has no behaviour; the controller does the work
  • Use real examples like cert-manager or Argo CD that interviewers recognise
  • Mention the CRD-deletion danger; it shows production awareness

Concepts covered

CustomResourceDefinition, Custom Resources, OpenAPI v3 Schema, API Versioning, Controllers

More Kubernetes & Orchestration questions