0
0
Kubernetesdevops~10 mins

Custom Resource Definitions (CRDs) in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Custom Resource Definitions (CRDs)
Write CRD YAML
Apply CRD to cluster
Kubernetes API Server registers new resource
User creates Custom Resource (CR)
Kubernetes stores CR in etcd
Controllers watch and act on CRs
Custom behavior executed
End
This flow shows how you write and apply a CRD, then create custom resources that Kubernetes stores and controllers manage.
Execution Sample
Kubernetes
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: widgets.example.com
spec:
  group: example.com
  versions:
  - name: v1
    served: true
    storage: true
  scope: Namespaced
  names:
    plural: widgets
    singular: widget
    kind: Widget
This YAML defines a CRD named 'widgets.example.com' that adds a new resource type 'Widget' to Kubernetes.
Process Table
StepActionResource State ChangeAPI Server Response
1Apply CRD YAML with kubectlCRD 'widgets.example.com' created in clusterCRD accepted and registered
2API Server registers new resource type 'Widget'API server schema updatedResource type available for use
3User creates a Widget custom resourceNew Widget object stored in etcdWidget resource created successfully
4Controller watches Widget resourcesController detects new WidgetController starts managing Widget
5Controller acts on WidgetCustom behavior executed (e.g., create pods)Status updated on Widget resource
6User queries Widget resourceCurrent state returnedWidget details shown
7User deletes Widget resourceWidget removed from etcdDeletion confirmed
8User deletes CRDCRD and all Widgets removedCRD deleted, resource type removed
💡 CRD lifecycle ends when CRD is deleted or cluster is shut down
Status Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 7Final
CRD 'widgets.example.com'Not presentCreatedPresentPresentPresentDeleted
Widget resourcesNoneNoneOne createdManaged by controllerDeletedNone
API Server schemaNo Widget typeWidget type addedWidget type presentWidget type presentWidget type presentWidget type removed
Key Moments - 3 Insights
Why does the API server need to register the CRD before I can create custom resources?
Because the API server must know about the new resource type schema to accept and store custom resources. See execution_table step 2 where the resource type is registered before creation.
What happens if I delete the CRD but still have custom resources?
Deleting the CRD removes the resource type and all its custom resources from the cluster, as shown in execution_table step 8.
How does the controller know when to act on a custom resource?
Controllers watch the API server for changes to custom resources and react accordingly, as shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the new resource type 'Widget' registered by the API server?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Check the 'Resource State Change' column for when the API server schema updates.
According to the variable tracker, what is the state of Widget resources after Step 3?
ANone
BOne created
CManaged by controller
DDeleted
💡 Hint
Look at the 'Widget resources' row under 'After Step 3' column.
If you delete the CRD, what happens to the custom resources according to the execution table?
AThey remain but become unmanaged
BThey become read-only
CThey are deleted along with the CRD
DThey are archived automatically
💡 Hint
See execution_table step 8 for the effect of deleting the CRD.
Concept Snapshot
Custom Resource Definitions (CRDs) let you add new resource types to Kubernetes.
Write a CRD YAML and apply it to register the new type.
Create custom resources of that type like built-in resources.
Controllers watch and manage these custom resources.
Deleting the CRD removes the resource type and all its instances.
Full Transcript
Custom Resource Definitions (CRDs) allow you to extend Kubernetes by adding your own resource types. The process starts by writing a CRD YAML file that defines the new resource's group, version, scope, and names. When you apply this YAML to the cluster, the Kubernetes API server registers the new resource type. After registration, you can create custom resources of this type, which Kubernetes stores like any other resource. Controllers watch these custom resources and perform actions based on their state. If you delete the CRD, Kubernetes removes the resource type and all associated custom resources. This flow enables you to customize Kubernetes behavior with your own resource definitions.