Kubernetes

4 Mins Read

Understanding Custom Resource Definitions (CRD) in Kubernetes

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
Enroll now

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:

 

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.
  • Usage:
    • Allows users to create and manage Widget resources (e.g., kubectl get widgets or kubectl get wg).

 

  • Apply the CRD:

  • Create a Custom Resource: Save the following YAML file as my-widget.yaml:

Apply the custom resource:

  • Verify the Custom Resource: Check if the resource was created:

 

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.
  • Delete the CRD: Use the kubectl delete command to remove the CRD itself.
  • Verify Deletion: Ensure the CRD is deleted:

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
Subscribe now

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 PartnerAWS Migration PartnerAWS Data and Analytics PartnerAWS DevOps Competency PartnerAWS GenAI Competency PartnerAmazon QuickSight Service Delivery PartnerAmazon EKS Service Delivery Partner AWS Microsoft Workload PartnersAmazon EC2 Service Delivery PartnerAmazon ECS Service Delivery PartnerAWS Glue Service Delivery PartnerAmazon Redshift Service Delivery PartnerAWS Control Tower Service Delivery PartnerAWS WAF Service Delivery Partner and many more.

To get started, go through our Consultancy page and Managed Services PackageCloudThat’s offerings.

WRITTEN BY Komal Singh

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!