Difficulty: Advanced
What are Custom Resource Definitions (CRDs) in Kubernetes, how do they work, and why are they used?
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.
A CRD (apiextensions.k8s.io/v1) describes:
billing.example.com, kind Invoice, plural invoices, optional shortNamesNamespaced or Clusterserved and exactly one with storage: true, so you can evolve from v1alpha1 to v1x-kubernetes-validations) add cross-field checks without a webhook.status (so controllers update status separately from spec) and scale (so HPA and kubectl scale work)kubectl get showsAfter you apply it, the API server creates REST endpoints such as /apis/billing.example.com/v1/namespaces/default/invoices.
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:
Certificate, IssuerServiceMonitor, PrometheusRuleApplicationGateway, HTTPRoute are shipped as CRDs rather than built inCluster, Backup| Option | Use when | Cost |
|---|---|---|
| ConfigMap | Simple configuration blobs | No schema, no dedicated API |
| CRD | New declarative resource types | Easy, no extra server |
| Aggregated API server | Need custom storage or non-CRUD behaviour (like metrics-server) | Must run and secure your own API server |
None works only when versions have the same shape; otherwise you run a conversion webhook.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.
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.
CustomResourceDefinition, Custom Resources, OpenAPI v3 Schema, API Versioning, Controllers