Voiced by Amazon Polly |
Kubernetes, with its robust extensibility, is a widely adopted platform for managing containerized applications. One of the key features that make Kubernetes flexible is Custom Resource Definitions (CRDs). They enable users to define and manage custom resources, expanding Kubernetes beyond its default set of APIs. This blog delves into the concept of CRDs, their significance, how to create them, their features, and advantages, concluding with a hands-on demonstration.
Learn from Industry Certified DevOps Experts and Become a Certified DevOps Professional with a High Paying Job
- Experienced Authorized Instructor led Training
- Live Hands-on Labs
What is a Custom Resource Definition (CRD)?
- A Custom Resource Definition (CRD) is a way to extend Kubernetes’ API to support custom types. Out of the box, Kubernetes provides a fixed set of APIs to manage resources such as Pods, Services, and Deployments.
- CRDs allow users to define their own resource types, complete with associated behaviours, making Kubernetes adaptable to unique application requirements.
- For example, if your organization needs a specific resource called Widget for managing internal processes, a CRD can help you define this resource and interact with it using Kubernetes-like commands.
Why Use CRDs?
CRDs are used to:
- Extend Kubernetes Functionality: They allow for the creation of resources tailored to specific use cases, such as managing custom workloads, configurations, or domain-specific applications.
- Declarative Management: Custom resources can be managed declaratively using kubectl and YAML manifests, aligning with Kubernetes’ design principles.
- Simplify Operational Complexity: By integrating domain-specific resources directly into Kubernetes, you can consolidate operational workflows, reducing the need for external tooling.
- Enable Automation: Custom controllers can be paired with CRDs to implement specific logic, such as reconciling resource states or automating application behavior.
Features and Advantages of CRDs
Features:
- Versioning: CRDs support versioning, allowing gradual transitions between API versions.
- Validation: Built-in Open API validation ensures the schema adheres to defined rules.
- Defaulting: You can specify default values for resource fields.
- Sub resources: CRDs can have status and scale sub resources for better integration with Kubernetes.
Advantages:
- Ease of Use: They are simpler to implement compared to aggregating APIs.
- Native Integration: Leverage Kubernetes RBAC, authentication, and API management for custom resources.
- Scalability: As Kubernetes itself manages custom resources, scalability remains consistent.
- Custom Logic: Pair CRDs with operators to implement domain-specific workflows.
How to Create a CRD
Creating a CRD involves defining its schema and applying it to the cluster. Here is a step-by-step overview:
- Define the CRD Manifest: This includes the resource name, version, group, and schema.
- Use kubectl apply to register the CRD with the Kubernetes API server.
- Create Custom Resources: After defining the CRD, you can create custom resources of this kind.
Hands-on: Working with a Custom Resource Definition
Objective:
Create a CRD for managing a fictional resource called Widget.
Steps:
- Define the CRD Manifest: Save the following YAML file as widget-crd.yaml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: name: widgets.example.com spec: group: example.com versions: - name: v1 served: true storage: true schema: openAPIV3Schema: type: object properties: spec: type: object properties: name: type: string replicas: type: integer scope: Namespaced names: plural: widgets singular: widget kind: Widget shortNames: - wg |
This YAML defines a Custom Resource Definition (CRD) for a Kubernetes custom resource type named Widget. Key points:
- API Group and Version:
- Group: example.com
- Version: v1 (served and stored).
- Naming:
- Plural: widgets
- Singular: widget
- Kind: Widget
- Shorthand: wg
- Scope:
- Name spaced, meaning resources are limited to specific namespaces.
- Schema:
- Fields in spec include:
- name (string)
- replicas (integer)
- Validated via OpenAPI schema.
- Fields in spec include:
- Usage:
- Allows users to create and manage Widget resources (e.g., kubectl get widgets or kubectl get wg).
- Apply the CRD:
1 |
kubectl apply -f widget-crd.yaml |
- Create a Custom Resource: Save the following YAML file as my-widget.yaml:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
apiVersion: example.com/v1 kind: Widget metadata: name: my-widget spec: name: MyWidget replicas: 3 |
Apply the custom resource:
1 |
kubectl apply -f my-widget.yaml |
- Verify the Custom Resource: Check if the resource was created:
1 2 3 |
kubectl get widgets kubectl describe widget my-widget |
How to Delete a CRD
Deleting a CRD removes the custom resource type and all its instances from your cluster. Use caution, as this action is irreversible.
- Delete the Custom Resources: Ensure you remove all instances of the custom resource before deleting the CRD to avoid dangling references.
1 |
kubectl delete widgets --all |
- Delete the CRD: Use the kubectl delete command to remove the CRD itself.
1 |
kubectl delete crd widgets.example.com |
- Verify Deletion: Ensure the CRD is deleted:
1 |
kubectl get crds |
If the CRD was successfully deleted, it will no longer appear in the list.
Real-world Examples of CRDs
- Prometheus Operator: The Prometheus Operator uses CRDs to manage Prometheus instances declaratively.
- Cert-Manager: It defines CRDs for managing certificates and issuers.
- Cross plane: Uses CRDs to represent cloud resources like databases, clusters, and more.
Conclusion
Custom Resource Definitions are a powerful way to extend Kubernetes’ capabilities. They allow organizations to integrate custom workflows and manage application-specific resources seamlessly within the Kubernetes ecosystem. By combining CRDs with custom controllers, you can create fully automated systems tailored to your unique requirements.
Become a Certified DevOps Professional, without leaving you job. Attend 8+ DevOps certification Training at less than the price of 2!
- Experienced Authorized Instructor led Training
- Live Hands-on Labs
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner and many more.
To get started, go through our Consultancy page and Managed Services Package, CloudThat’s offerings.
WRITTEN BY Komal Singh
Click to Comment