Introduction
A Custom Resource Definition (CRD) in Kubernetes allows you to extend the Kubernetes API by defining your own custom resources. These custom resources can represent any kind of domain-specific entity, and you can manage them using standard Kubernetes tools like kubectl
.
Overview of Custom Resource Definitions (CRDs)
- Custom Resources: These are extensions of the Kubernetes API. They allow you to create your own custom resource types that can be managed like built-in resources (e.g., Pods, Services).
- Custom Resource Definitions (CRDs): These are used to define the schema and behavior of custom resources. Once a CRD is created, you can create instances of the custom resource it defines.
Steps to Create and Use a Custom Resource Definition
Step 1: Define the Custom Resource Definition (CRD)
The CRD defines the structure and behavior of your custom resource. Here is an example of a simple CRD for a custom resource called MyApp
.
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
port:
type: integer
subresources:
status: {}
scope: Namespaced
names:
plural: myapps
singular: myapp
kind: MyApp
shortNames:
- ma
Explanation:
apiVersion
: The API version for CRDs isapiextensions.k8s.io/v1
.kind
: This isCustomResourceDefinition
since you are defining a new custom resource.metadata.name
: The name of the CRD should be in the form ofplural.group
. In this case, it’smyapps.example.com
.spec.group
: The API group your custom resource belongs to. In this case, it’sexample.com
.spec.versions
: The different versions of your custom resource. Each version can have its own schema.spec.scope
: Specifies whether the custom resource is namespaced or cluster-scoped. In this case, it’sNamespaced
.spec.names
: Specifies the names used for the custom resource:plural
: The plural name (myapps
).singular
: The singular name (myapp
).kind
: The kind of the resource (MyApp
).shortNames
: Optional shorthand names for the resource.
Step 2: Apply the CRD to Your Kubernetes Cluster
To create the CRD in your cluster, apply the YAML file using kubectl
:
kubectl apply -f myapp-crd.yaml
This command registers the MyApp
custom resource with your Kubernetes API server.
Step 3: Create Instances of Your Custom Resource
After the CRD is applied, you can create instances of your custom resource. Here’s an example:
apiVersion: example.com/v1
kind: MyApp
metadata:
name: myapp-sample
spec:
replicas: 3
image: nginx:latest
port: 80
This YAML file creates an instance of the MyApp
custom resource with the specified spec
fields.
Apply this file using kubectl
:
kubectl apply -f myapp-instance.yaml
Step 4: Interact with Your Custom Resource
You can interact with your custom resource using kubectl
just like any other Kubernetes resource.
# List all instances of MyApp
kubectl get myapps
# Get details of a specific MyApp instance
kubectl get myapp myapp-sample -o yaml
# Delete a MyApp instance
kubectl delete myapp myapp-sample
Advanced Features
CRDs support various advanced features, including:
- Validation: You can define schemas to enforce validation rules on your custom resources.
- Subresources: You can add status subresources for tracking status information or scale subresources for managing replica counts.
- Conversion Webhooks: These allow you to implement custom conversion logic between different versions of your custom resource.
Conclusion
Custom Resource Definitions (CRDs) are a powerful way to extend Kubernetes with domain-specific abstractions, making it possible to manage custom application-specific resources as first-class citizens in your Kubernetes environment. By defining a CRD, you gain the flexibility to tailor Kubernetes to fit the unique needs of your applications.
Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.Academy
Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.
My book Ansible By Examples: 200+ Automation Examples For Linux and AWX System Administrator and DevOps
Donate
Want to keep this project going? Please donate