Mastering Kubernetes Custom Resource Definitions (CRDs): A Practical Example
In the dynamic world of Kubernetes, Custom Resource Definitions (CRDs) empower users to extend the Kubernetes API to support novel workloads and use cases. This article provides a comprehensive, step-by-step guide to creating a CRD example, ensuring you understand the process and can apply it to your unique needs.
Understanding Custom Resource Definitions
CRDs allow you to define new resources that can be managed by Kubernetes, enabling you to extend the Kubernetes API beyond its core resources. By using CRDs, you can create custom controllers, validate and process custom data, and integrate with third-party APIs seamlessly.
Key Components of a CRD
- API Version: The version of the API that the CRD belongs to (e.g.,
apiextensions.k8s.io/v1). - Kind: The type of resource (e.g.,
CustomResourceDefinition). - Metadata: Data that helps uniquely identify the CRD, such as name and namespace.
- Spec: The desired state of the CRD, including the resource's schema and validation rules.
Creating a Simple CRD Example: Guestbook
Let's create a simple CRD example, a "Guestbook" resource, which will store messages left by users. This CRD will have a single field, message, to store the text of the message.

Defining the Guestbook CRD
Create a new file named guestbook_crd.yaml and add the following content:
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: guestbooks.example.com
spec:
group: example.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
message:
type: string
scope: Namespaced
names:
plural: guestbooks
singular: guestbook
kind: Guestbook
shortNames:
- gb
Applying the CRD
Apply the CRD using the following command:
kubectl apply -f guestbook_crd.yaml
After applying the CRD, you can create, update, and delete Guestbook resources using the standard Kubernetes API.

Creating a Custom Controller for the Guestbook CRD
To make the most of your CRD, create a custom controller that watches for changes to the Guestbook resource and performs actions based on those changes. This could involve processing the message, storing it in a database, or triggering other events.
Building the Controller
Using a programming language of your choice (e.g., Go, Python, or Java), create a new controller that extends k8s.io/client-go/informers/shared/informers.go. Implement the logic to watch for changes to the Guestbook resource and process the message field as needed.
Deploying the Controller
Package your controller as a Kubernetes deployment and apply it to your cluster:

kubectl apply -f controller_deployment.yaml
Now, whenever a new Guestbook resource is created or updated, your custom controller will process the message field according to your specified logic.
Conclusion
In this article, we explored the power of Kubernetes Custom Resource Definitions (CRDs) and walked through a practical example of creating a CRD and a custom controller for managing the custom resource. By mastering CRDs, you can extend the Kubernetes API to support your unique workloads and use cases, unlocking new possibilities for your applications.






















