Using Custom Resources (CRD)

Overview
Custom Resources extend the Kubernetes API. A CustomResourceDefinition
(CRD) is used to define these custom resources.
Documentation
Example CRD
In this example, we define a CronTab
resource as described in the Kubernetes documentation.
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: crontabs.stable.example.com
spec:
group: stable.example.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
cronSpec:
type: string
image:
type: string
replicas:
type: integer
scope: Namespaced
names:
plural: crontabs
singular: crontab
kind: CronTab
shortNames:
- ct
Steps to Use CRDs
-
Create the CRD Definition
Define your custom resource in a YAML file using the structure above. Save this file as
crontab-crd.yaml
. -
Apply the CRD
Use
kubectl
to create the CRD in your cluster: -
Define a Custom Resource
Once the CRD is applied, you can define custom resources based on it. Example:
apiVersion: "stable.example.com/v1" kind: CronTab metadata: name: my-new-cron-object spec: cronSpec: "* * * * */5" image: my-awesome-cron-image replicas: 1
Save this as
my-new-cron-object.yaml
. -
Create the Custom Resource
Apply the custom resource to your cluster:
-
Interact with the Custom Resource
Use standard Kubernetes commands to interact with your custom resource: To get the resource:
To describe the resource:
Conclusion
Custom Resources in Kubernetes are a powerful way to introduce new API objects tailored to your application's needs, enhancing the flexibility and functionality of your Kubernetes cluster.
Documentation