What Is a Kubernetes CRD? Define and Safely Manage a Custom Resource API
A Kubernetes CustomResourceDefinition (CRD) registers a new resource type in a cluster's API. After a CRD is installed, the API server creates an API path for instances of that type (Custom Resources), stores and serves them, and can validate their fields against a schema or remove fields it does not recognize. A CRD does not deploy an application or run work by itself. For behavior beyond storing and validating objects, a separate controller must watch the Custom Resources.
What a CRD adds
Kubernetes provides resource types such as Pods, Services, and Deployments. A CRD author can add another type by declaring an API group, version, names, scope, and field schema. For example, registering CronTab under stable.example.com/v1 lets Kubernetes API clients and kubectl work with objects of that type. This adds a data shape to the API; it does not make cron tasks execute.
A CRD itself is a cluster-scoped API extension. The Custom Resources created from it can be namespaced or cluster-scoped, according to the CRD's scope. With Namespaced, objects with the same name can exist in different namespaces. With Cluster, names are unique across the cluster and objects have no namespace. This choice sets the identity and operational boundary of each object, so decide it based on how the resource will be used.
A minimal CRD example
The example below adds a CronTab resource using apiextensions.k8s.io/v1. Its spec has two string fields and an integer replicas constrained to 1 through 10. It does not include a cron runner or application controller.
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: crontabs.stable.example.com
spec:
group: stable.example.com
scope: Namespaced
names:
plural: crontabs
singular: crontab
kind: CronTab
shortNames:
- ct
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
required:
- cronSpec
- image
properties:
cronSpec:
type: string
image:
type: string
replicas:
type: integer
minimum: 1
maximum: 10group and names determine the API address and identifiers. The API path for this example takes the form /apis/stable.example.com/v1/namespaces/{namespace}/crontabs. plural is used in the API path, kind is used in an object's YAML, and shortNames can provide kubectl aliases. Once clients and manifests depend on these identifiers, changing them requires care.
Each entry in versions is an API version offered to clients. served: true means the API server serves requests for that version; storage: true marks the version used when persisting objects. Multiple versions can be served, but exactly one must have storage: true. If fields differ between versions, design how objects are converted. A conversion webhook sends requests from the API server to a separate service, so a webhook outage can affect reads and writes of Custom Resources.
What the schema validates and prunes
CRDs using apiextensions.k8s.io/v1 require a structural OpenAPI schema. In the example, the API server checks that cronSpec and image are strings and replicas is an integer from 1 to 10. These checks constrain object shape and values, but they do not prove that the application can use every value successfully. For example, a string can satisfy the schema while still containing invalid cron syntax unless that syntax is checked separately.
Fields absent from the schema can be pruned, or removed, before storage. If a kubectl apply file contains a typo or a field not yet added to the schema, the API server may not store that field. Update the schema together with supported configuration and inspect the saved object with kubectl get ... -o yaml. Unknown-field preservation is an exception that should be scoped to the specific nested structure that needs it; it is not a safe default substitute for a schema.
Apply a CRD and Custom Resource
You need a Kubernetes cluster that supports apiextensions.k8s.io/v1, a kubectl configured for that cluster, and permission to create CRDs. A CRD changes the cluster-wide API, so validate it first in a personal test cluster or an approved development environment. The steps below assume files named crontab-crd.yaml and sample-crontab.yaml.
Apply the CRD:
kubectl apply -f crontab-crd.yamlConfirm that the API server recognizes the new type:
kubectl get crd crontabs.stable.example.com kubectl explain crontab.specThe CRD should appear in the
kubectl get crdresult.kubectl explainreads the published schema and describes its fields. API discovery may take a short time to become ready.A CRD does not create an instance automatically. Declare a Custom Resource in a separate file:
apiVersion: stable.example.com/v1 kind: CronTab metadata: name: sample-nightly namespace: default spec: cronSpec: "0 2 * * *" image: example.invalid/cron-task:1 replicas: 1Apply the object and inspect what the server stored:
kubectl apply -f sample-crontab.yaml kubectl get crontab sample-nightly -n default -o yamlYou should see the
sample-nightlyobject and itsspec.cronSpec,spec.image, andspec.replicasfields. Without an Operator or another controller watching this resource, no Pod or cron task is created. This YAML demonstrates API storage and validation; it does not run the image or schedule a task.
To test rejection of invalid values, use a development cluster and apply a value outside the schema, such as replicas: 0; the API server should return a validation error. To clean up the test object, delete only that Custom Resource and confirm it is gone:
kubectl delete -f sample-crontab.yaml
kubectl get crontab sample-nightly -n defaultThe second command is expected to return NotFound. Deleting the CRD removes its API endpoint and every Custom Resource of that type. Do not delete a CRD in a production cluster unless you have confirmed that its data can be removed and the affected objects have been handled.
Limits and operational choices
CRDs fit small declarative configuration that people read and change, objects whose lifecycle belongs with Kubernetes resources, and cases that need API schema validation and kubectl support. Do not use the Kubernetes API as storage for application data, large records, high-rate events, or synchronous command results. The official Kubernetes documentation warns that storing user or bulk data in Custom Resources can couple a workload too tightly to the API server. A standalone service API may be a better fit.
A CRD adds an API type without requiring a new API server, which keeps the extension simple. If you need specialized storage or more control over API behavior, API aggregation can connect a separate API server, at the cost of implementation and availability responsibilities, certificates, and operational complexity. Compare those capabilities with the maintenance cost before choosing.
During operation, review how schema changes affect existing objects and clients. Before removing an API version, confirm stored objects have been migrated to the new version. Deleting the CRD also deletes the Custom Resource data of that type. If a CRD is installed and upgraded alongside an application controller, document the CRD rollout order, controller compatibility, and backup and recovery responsibilities.
References
[Kubernetes: Extend the Kubernetes API with CustomResourceDefinitions](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/)
[Kubernetes: CustomResourceDefinition v1 API](https://kubernetes.io/docs/reference/kubernetes-api/apiextensions/custom-resource-definition-v1/)
[Kubernetes: Custom resource definition versioning](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/)
[Kubernetes: Custom Resources and API Aggregation](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/)