Custom Resource Definition (CRD)

What is a Custom Resource Definition (CRD)?

A Custom Resource Definition (CRD) is a Kubernetes object that defines a new, custom resource type. It extends the Kubernetes API, allowing you to create and manage custom objects. CRDs are the foundation for building Kubernetes-native applications and operators.

In the realm of software engineering, containerization and orchestration are two pivotal concepts that have revolutionized the way applications are developed, deployed, and managed. Among the many tools and technologies that facilitate these processes, Kubernetes, an open-source platform for automating deployment, scaling, and management of containerized applications, stands out. Within Kubernetes, Custom Resource Definitions (CRDs) play a crucial role. This article will delve into the depths of CRDs, exploring their definition, history, use cases, and specific examples.

CRDs are an integral part of the Kubernetes API, allowing users to create new types of resources without adding another API server. They have become a cornerstone of many Kubernetes-native applications, providing a way to extend Kubernetes functionality with custom resources. Understanding CRDs is essential for any software engineer working with Kubernetes and containerized applications.

Definition of Custom Resource Definition (CRD)

A Custom Resource Definition (CRD) is a feature in Kubernetes that allows users to add their own/custom objects to the Kubernetes API. CRDs provide a way to extend the Kubernetes API with custom resources, making it possible to use Kubernetes as a platform for a wide variety of applications.

CRDs are defined using YAML or JSON, like other Kubernetes objects. Once a CRD is created, users can create custom objects using the Kubernetes API, just like they would with built-in objects. These custom resources can then be manipulated using kubectl, Kubernetes' command-line interface, or through direct API calls.

Components of a CRD

A CRD consists of several components, including the Spec, Status, and Metadata. The Spec defines the desired state of the resource, while the Status reflects the current state of the resource. The Metadata includes data about the resource, such as its name, namespace, and labels.

Additionally, a CRD includes a Schema that defines the structure of the resource. The Schema is used for validation when creating or updating a resource. It ensures that the resource conforms to the expected structure and contains the required fields.

CRD vs Built-in Resources

CRDs are similar to built-in resources in Kubernetes, such as Pods, Services, and Deployments. However, while built-in resources are predefined and included in the Kubernetes API, CRDs are user-defined and added to the API.

Despite this difference, once a CRD is added to the API, it can be used just like a built-in resource. It can be created, updated, deleted, and listed using the Kubernetes API or kubectl. This makes CRDs a powerful tool for extending Kubernetes functionality.

History of Custom Resource Definition (CRD)

The concept of extending the Kubernetes API with custom resources is not new. Before CRDs, there were ThirdPartyResources (TPRs). Introduced in Kubernetes 1.2, TPRs allowed users to create custom resources, but they had several limitations and were deprecated in Kubernetes 1.7.

CRDs were introduced in Kubernetes 1.7 as a replacement for TPRs. They addressed many of the limitations of TPRs, providing a more robust and flexible way to extend the Kubernetes API. Since then, CRDs have become a fundamental part of the Kubernetes ecosystem, used by many Kubernetes-native applications and operators.

Evolution of CRDs

Since their introduction, CRDs have evolved significantly. In Kubernetes 1.8, CRDs gained support for validation using OpenAPI v3 schemas. This made it possible to validate custom resources against a schema when they are created or updated, ensuring they conform to the expected structure.

In Kubernetes 1.9, CRDs gained support for additional printer columns. This made it possible to customize the output of kubectl get commands for custom resources, similar to built-in resources. In Kubernetes 1.10, CRDs gained support for subresources, allowing custom resources to have status and scale subresources like built-in resources.

Current State of CRDs

Today, CRDs are a mature and stable feature in Kubernetes. They are used by many Kubernetes-native applications and operators, such as Prometheus, Rook, and Istio. CRDs have become a cornerstone of the Kubernetes ecosystem, enabling a wide variety of use cases.

Despite their maturity, CRDs continue to evolve. The Kubernetes community is actively working on improving CRDs, adding new features and capabilities. This ensures that CRDs remain a powerful tool for extending the Kubernetes API and enabling new use cases.

Use Cases of Custom Resource Definition (CRD)

CRDs have a wide range of use cases, thanks to their ability to extend the Kubernetes API. They are used by many Kubernetes-native applications and operators to define custom resources that represent the application's or operator's state.

For example, the Prometheus Operator uses a CRD to represent Prometheus instances. Users can create Prometheus resources using the Kubernetes API, and the Prometheus Operator ensures that a corresponding Prometheus instance is running. Similarly, the Rook Operator uses a CRD to represent Ceph clusters. Users can create CephCluster resources using the Kubernetes API, and the Rook Operator ensures that a corresponding Ceph cluster is running.

Operators and CRDs

One of the most common use cases for CRDs is in conjunction with operators. Operators are software extensions to Kubernetes that make use of custom resources to manage applications and their components. Operators follow Kubernetes principles, notably the control loop.

For example, the etcd operator, a project from CoreOS, creates, configures, and manages etcd clusters. It uses a CRD to represent etcd clusters. Users can create, update, and delete etcd cluster resources using the Kubernetes API, and the etcd operator ensures that the state of the etcd cluster matches the desired state defined in the resource.

Extending Kubernetes Functionality

CRDs can also be used to extend the functionality of Kubernetes. For example, the cert-manager project uses a CRD to represent certificates. Users can create Certificate resources using the Kubernetes API, and cert-manager ensures that a corresponding certificate is issued and kept up to date.

Similarly, the IngressRoute CRD in the Contour project extends the functionality of the Ingress resource in Kubernetes. It provides additional features, such as traffic splitting, that are not available in the Ingress resource.

Examples of Custom Resource Definition (CRD)

Let's take a look at some specific examples of CRDs to better understand how they work and how they can be used. These examples will cover the Prometheus Operator's Prometheus CRD and the Rook Operator's CephCluster CRD.

Both of these examples demonstrate how CRDs can be used to represent the state of an application or operator, and how the application or operator can use the Kubernetes API to ensure that the actual state matches the desired state defined in the CRD.

Prometheus Operator's Prometheus CRD

The Prometheus Operator's Prometheus CRD represents Prometheus instances. The CRD defines the structure of a Prometheus resource, including fields for the Prometheus version, the number of replicas, and the storage configuration.

Users can create Prometheus resources using the Kubernetes API, specifying the desired state of the Prometheus instance in the resource. The Prometheus Operator then ensures that a corresponding Prometheus instance is running, with the specified configuration.

Rook Operator's CephCluster CRD

The Rook Operator's CephCluster CRD represents Ceph clusters. The CRD defines the structure of a CephCluster resource, including fields for the Ceph version, the number of mons, and the storage configuration.

Users can create CephCluster resources using the Kubernetes API, specifying the desired state of the Ceph cluster in the resource. The Rook Operator then ensures that a corresponding Ceph cluster is running, with the specified configuration.

Conclusion

Custom Resource Definitions (CRDs) are a powerful feature in Kubernetes, allowing users to extend the Kubernetes API with custom resources. They have become a cornerstone of many Kubernetes-native applications and operators, enabling a wide variety of use cases.

Understanding CRDs is essential for any software engineer working with Kubernetes and containerized applications. By mastering CRDs, you can leverage the full power of Kubernetes, creating custom resources that perfectly fit your needs and using the Kubernetes API to manage these resources.

High-impact engineers ship 2x faster with Graph
Ready to join the revolution?
High-impact engineers ship 2x faster with Graph
Ready to join the revolution?

Code happier

Join the waitlist