Skip to main content
This lesson walks through creating a Kubernetes CustomResourceDefinition (CRD) for a Widget kind, applying it to a cluster, and validating the schema by creating both a valid and an invalid Widget. You will learn how to:
  • Define a CRD with an OpenAPI v3 schema for server-side validation.
  • Apply the CRD and wait until it is established.
  • Create a namespace and a sample custom resource (CR).
  • Observe schema validation rejecting invalid CRs.
Relevant links:

1) Edit the CRD

Open widget-crd.yaml (for example, in VS Code) and add a shortName and an OpenAPI v3 schema under the version you serve. The example below demonstrates:
  • shortNames: [wj] so kubectl get wj works.
  • spec schema requiring size and color.
  • size constrained by an enum: small, medium, large.
  • replicas with minimum, maximum, and a default.
Save the file after making these changes.
Using shortNames (for example wj) makes commands like kubectl get wj possible and more convenient.

2) Apply the CRD and wait until it is established

Apply the CRD to the cluster and wait until the API server marks it as Established. This ensures the new resource kind is served by the API.
Wait for the CRD to become Established:
Verification commands:
  • kubectl get crd widgets.training.example.com -o yaml
  • kubectl api-resources | grep widget

3) Create a namespace for the demo

Create a dedicated namespace to isolate demo resources:

4) Create a sample Widget (valid CR)

Create widget-sample.yaml with valid values that conform to the CRD schema:
Apply the CR and list it using the short name:
To display stored spec values with JSONPath:
Quick reference: CR fields and purpose

5) Apply a deliberately invalid Widget to see validation

To demonstrate server-side schema validation, create bad-widget.yaml that violates the size enum:
Apply it and observe the validation error:
Kubernetes rejects this object because size: huge is not one of the allowed enum values defined in the CRD.
If you update your CRD schema after CRs already exist, be careful: removing fields or tightening validation can cause existing objects to fail validation. Prefer adding new fields or using a migration strategy.

Summary and next steps

  • You created a namespaced CRD with an OpenAPI v3 schema and a shortName for convenience.
  • You applied a valid CR and confirmed it is stored by the API.
  • You verified that server-side validation prevents invalid CRs from being created.
Next recommended steps:
  • Add additional validation (patterns, maxLength, nested object validation) to the CRD schema as needed.
  • Implement a controller for the Widget kind to reconcile and act on Widget objects.
  • Read more: Extending the Kubernetes API with CRDs.

Watch Video