Custom Resource Definition (CRDs)
Suheb Ghare
DevOps/ Site Reliability Engineer @ mumzworld.com | AWS Certified, Infrastructure Automation, Kubernetes, GitOps
Custom Resource Definition (CRD) is an extension mechanism that allows users to define custom resources and their schema in a Kubernetes cluster. It enables the Kubernetes API server to understand and handle new resource types beyond the built-in resources like pods, services, deployments, etc.
Why CRDs are Used:
Example of a CRD:
Let's say you have a distributed application that requires a set of resources specific to its architecture. You can define a CRD to represent a custom resource called 'MyApp' with specific properties. Below is an illustrative example:
Step 1: Define the CRD (e.g. myapp-crd.yaml):
领英推荐
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: myapps.example.com
spec:
group: example.com
names:
kind: MyApp
plural: myapps
singular: myapp
scope: Namespaced
version: v1alpha1
additionalPrinterColumns:
- JSONPath: .spec.version
name: Version
type: string
- JSONPath: .spec.size
name: Size
type: string
- JSONPath: .status.nodes
name: Nodes
type: integer
validation:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
version:
type: string
size:
type: string
nodes:
type: integer
In this example, we define a CRD named 'myapps.example.com'. It specifies the properties of the custom resource, such as the API group, version, and the schema for the custom resource.
Step 2: Use the Custom Resource (e.g. myapp-instance.yaml):
apiVersion: example.com/v1alpha1
kind: MyApp
metadata:
name: myapp-instance
spec:
version: "1.0"
size: "Medium"
In this example, we create an instance of the 'MyApp' custom resource named 'myapp-instance'. It specifies the desired state of the custom resource using the properties defined in the CRD.
Applying CRD and Custom Resource:
# Apply the CRD
kubectl apply -f myapp-crd.yaml
# Apply the Custom Resource
kubectl apply -f myapp-instance.yaml
# Check the status of the custom resource
kubectl get myapps
Custom Resources and CRDs provide a powerful abstraction for extending and customizing Kubernetes to suit the needs of diverse applications. They enable users to manage application-specific resources using the same familiar Kubernetes API and tooling.
--
7 个月Cloud-native application management is evolving! ? Which method are you relying on? Let's see what the community prefers. Vote and share your insights! ?? https://shorturl.at/tstj5
Product Owner | Backend & DevOps
1 年CRDs are a game-changer in Kubernetes for custom resource management! This detailed breakdown not only highlights their significance in extending Kubernetes but also demonstrates how they streamline application-specific resource handling. Leveraging CRDs empowers teams to build tailored solutions while maintaining consistency across clusters. Thanks for sharing this insightful guide!