This article explains how to create and manage Custom Resource Definitions in Kubernetes, using a FlightTicket example to illustrate the process.
In this lesson, we explore how Custom Resource Definitions (CRDs) work in Kubernetes. You will learn how standard Kubernetes resources, such as Deployments, are created, stored in etcd, and managed by built-in controllers. Then, we’ll demonstrate how to extend Kubernetes by defining and using a custom resource—illustrated here as a “FlightTicket”—and explain why a dedicated custom controller is necessary to act upon these new resources.
When you create a Deployment in Kubernetes, the API server stores its state in etcd. A built-in controller, known as the deployment controller, continuously monitors the Deployment to ensure that the desired state (for example, maintaining three replicas) is met by creating or deleting pods as needed.Below is an example Deployment definition file:
Using the file above, you can create, query, and delete the deployment with the following commands:
Copy
Ask AI
kubectl create -f deployment.yml# Output:kubectl get deployments# Output:# NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEkubectl delete -f deployment.yml# Output:# deployment "myapp-deployment" deleted
The deployment controller automatically creates a ReplicaSet, which manages the specified number of pods. This automation lies at the heart of Kubernetes resource management.
Imagine you want to manage something entirely new on your cluster—such as booking a flight ticket. In this example, we define a custom resource called FlightTicket that represents a flight ticket booking. The resource encapsulates details such as the departure and destination airports and the number of tickets required.Below is an example of a FlightTicket resource file:
Copy
Ask AI
# flightticket.ymlapiVersion: flights.com/v1kind: FlightTicketmetadata: name: my-flight-ticketspec: from: Mumbai to: London number: 2
When you create this custom resource, you expect that:
It is stored in etcd.
A custom controller (which you will build) watches for create, update, or delete events.
The controller automatically makes the necessary API calls (for example, to an external flight booking API) to either book or cancel the ticket.
Attempting to create the FlightTicket resource without first informing Kubernetes of its existence will result in an error:
Copy
Ask AI
kubectl create -f flightticket.yml# Output:# no matches for kind "FlightTicket" in version "flights.com/v1"
This error means that Kubernetes does not yet recognize the FlightTicket type.
To allow the Kubernetes API to accept FlightTicket objects, you must create a CRD that informs the API server about this new resource type. The CRD includes details such as API version, kind, metadata, spec, and schema information (including supported fields, types, and validation rules).Here’s an example of a CRD for the FlightTicket resource:
While defining a CRD enables Kubernetes to store and retrieve FlightTicket objects in etcd, these objects remain inactive without a controller. A custom controller, often implemented in Go, observes events related to FlightTicket resources and executes business logic (such as calling external APIs to book or cancel flights).Below is a simplified snippet of what such a controller might look like:
Copy
Ask AI
package flightticketvar controllerKind = apps.SchemeGroupVersion.WithKind("FlightTicket")// Run begins watching and syncing FlightTicket resources.func (dc *FlightTicketController) Run(workers int, stopCh <-chan struct{}) { // Controller logic goes here}// callBookFlightAPI handles the API call to book a flight ticket.func (dc *FlightTicketController) callBookFlightAPI(obj interface{}) { // API call implementation goes here}
Without a custom controller, any FlightTicket resource you create remains a passive data record in etcd without triggering any external actions.
Kubernetes resources like Deployments are managed by built-in controllers.
A custom resource (FlightTicket) can be defined to represent a new domain object.
A CRD must be created so that Kubernetes recognizes your custom resource.
A custom controller is essential to actively process and respond to events related to these resources.
In upcoming lessons, we’ll dive deeper into developing custom controllers that monitor CRD events and execute automated tasks based on resource changes.