0
0
Kubernetesdevops~10 mins

Custom resources concept in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Custom resources concept
Define CustomResourceDefinition (CRD)
Apply CRD to Kubernetes API
Kubernetes API Server Registers CRD
Create Custom Resource (CR) Instance
Kubernetes Stores CR Instance
Controllers Watch CR and Act
Custom Behavior Implemented
End
This flow shows how a custom resource is defined, registered, created, and managed in Kubernetes.
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 CustomResourceDefinition named 'widgets.example.com' to create a new resource type 'Widget'.
Process Table
StepActionKubernetes API StateResult
1Apply CRD YAMLCRD 'widgets.example.com' registeredAPI server accepts new resource type
2Create Custom Resource (CR) instance 'my-widget'CR instance stored under 'widgets' resourceCR is available for use
3Controller watches CRController detects 'my-widget' creationController triggers custom logic
4Controller updates status of 'my-widget'CR status updatedUser sees updated status
5Delete CR instance 'my-widget'CR instance removedResource cleaned up
6Delete CRD 'widgets.example.com'CRD removed, all CRs deletedCustom resource type no longer available
💡 CRD and CR lifecycle complete; Kubernetes API and controllers manage custom resources.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6
CRD 'widgets.example.com'Not presentRegisteredRegisteredRegisteredRegisteredRegisteredDeleted
CR instance 'my-widget'Not presentNot presentCreatedObserved by controllerStatus updatedDeletedDeleted
Key Moments - 3 Insights
Why do we need to apply a CRD before creating a custom resource?
The CRD defines the new resource type to the Kubernetes API server. Without it, the API server won't recognize or accept custom resource instances, as shown in step 1 and 2 of the execution_table.
What happens when a controller watches a custom resource?
The controller detects changes to the custom resource and runs custom logic, such as updating status or managing related resources. This is shown in steps 3 and 4 where the controller observes and updates the CR.
What occurs when the CRD is deleted?
Deleting the CRD removes the resource type and all its instances from the cluster, as shown in step 6 where both CRD and CR instances are deleted.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Kubernetes API state after step 2?
ACR instance 'my-widget' is deleted
BOnly CRD is registered, no CR instances yet
CCRD is registered and CR instance 'my-widget' is stored
DCRD is deleted
💡 Hint
Refer to the 'Kubernetes API State' column in row for step 2.
At which step does the controller update the status of the custom resource?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Check the 'Result' column for step 4 in the execution_table.
If the CRD is not applied, what will happen when creating a custom resource?
AThe API server will accept the CR instance
BThe API server will reject the CR instance
CThe CR instance will be stored but ignored
DThe controller will create the CRD automatically
💡 Hint
Look at step 1 and 2 in execution_table to understand the role of CRD.
Concept Snapshot
Custom Resources let you add new resource types to Kubernetes.
Define a CustomResourceDefinition (CRD) YAML and apply it.
Kubernetes API registers the CRD and accepts custom resource instances.
Controllers watch these instances to implement custom behavior.
Deleting the CRD removes all related custom resources.
Full Transcript
Custom resources in Kubernetes allow users to extend the cluster with new resource types. First, you define a CustomResourceDefinition (CRD) YAML that describes the new resource. When you apply this CRD to the cluster, the Kubernetes API server registers it and can accept instances of this new resource. You then create custom resource (CR) instances of this type. Controllers watch these CR instances and perform custom actions, such as updating status or managing related resources. When you delete the CRD, all custom resources of that type are also deleted, and the resource type is removed from the API server.