Custom Resource Definition in Kubernetes: What It Is and How It Works
Custom Resource Definition (CRD) in Kubernetes lets you create your own resource types beyond the built-in ones like Pods or Services. It allows you to define and manage custom objects through the Kubernetes API just like native resources.How It Works
Think of Kubernetes as a toolbox with predefined tools like Pods, Services, and Deployments. Sometimes, you need a special tool that Kubernetes doesn’t provide by default. A Custom Resource Definition (CRD) lets you add that special tool by defining your own resource type.
When you create a CRD, Kubernetes understands a new kind of object you want to manage. You can then create, update, or delete these custom objects using the same commands and API you use for built-in resources. This works because CRDs extend the Kubernetes API server, making your custom resource first-class citizens in the cluster.
It’s like adding a new category to a library catalog. Once added, you can check out books from that category just like any other.
Example
This example shows a simple CRD that defines a new resource called MyApp in the group example.com. After applying this, you can create MyApp objects in your cluster.
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: myapps.example.com
spec:
group: example.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
replicas:
type: integer
image:
type: string
scope: Namespaced
names:
plural: myapps
singular: myapp
kind: MyApp
shortNames:
- maWhen to Use
Use a CRD when you want Kubernetes to manage resources that are specific to your application or organization but are not part of the default Kubernetes objects. For example:
- Managing custom application configurations or deployments.
- Extending Kubernetes with new types for monitoring, backup, or security policies.
- Building operators that automate complex tasks by watching and reacting to your custom resources.
CRDs help keep your infrastructure consistent and manageable through Kubernetes tools and APIs.
Key Points
- CRDs let you add new resource types to Kubernetes without changing its core code.
- They extend the Kubernetes API, so you can use standard tools like
kubectlto manage them. - CRDs are essential for building custom automation and operators.
- They are defined using YAML and applied like any other Kubernetes resource.