kubernetes
kubectl
custom resource definitions
CRD
kubernetes commands

How to list applied Custom Resource Definitions in kubernetes with kubectl

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Kubernetes, Custom Resource Definitions (CRDs) extend the Kubernetes API to allow for the creation of bespoke resources. Organizations and developers use CRDs to tailor Kubernetes functionalities to specific use-case requirements. Managing and listing these Custom Resource Definitions is crucial for maintaining cluster health and ensuring that applications and services behave as expected.

This article explores the methods to list applied CRDs using the kubectl command line tool, providing technical explanations, examples, and a concise summary table.

Understanding Custom Resource Definitions

Custom Resource Definitions allow users to create their own resources just like Pods, Services, and Deployments. This offers a way to define APIs tailored to specific use cases. Here’s a snapshot of how CRDs function:

  • Resource specification: CRDs are defined in YAML files, much like other Kubernetes objects.
  • Versioning: CRDs support versioning, enabling gradual transitions across scheme changes.
  • Schema validation: Employs OpenAPI Schema to enforce valid configurations.
  • Namespaced vs. Cluster-scoped: CRDs can be either namespaced or cluster-scoped.

Listing CRDs with kubectl

Using kubectl, CRDs can be listed and examined in various ways. Below are the different ways to achieve this task, along with technical details and examples.

Basic Listing

To retrieve a list of all applied Custom Resource Definitions, use:

bash
kubectl get crds

This command will return a list of CRD names along with their established age.

Detailed Listing

For a more detailed information output, the -o wide flag can be employed:

bash
kubectl get crds -o wide

Here’s an example of typical output:

 
NAME                  CREATED AT
somecustomresource    2023-01-15T14:16:51Z
anotherresource       2023-01-16T09:03:22Z

Each row includes names of CRDs and the time they were created.

Specific Details with YAML/JSON

To fetch complete details of a specific CRD in YAML or JSON format, use:

bash
kubectl get crd <crd-name> -o yaml

or

bash
kubectl get crd <crd-name> -o json

Output for YAML provides extensively detailed insights, including apiVersion, kind, metadata, and specifications. This is useful for debugging or reviewing CRD configurations.

Filtering with Custom Columns

You can extract specific details using custom columns:

bash
kubectl get crds -o custom-columns=NAME:.metadata.name,VERSION:.spec.version

This command gives a clear view of each CRD and its respective version.

Using Label Selectors

CRDs might carry labels assigned during creation, useful for filtered searches. For instance:

bash
kubectl get crds -l app=custom-app

This query retrieves CRDs associated with the label app=custom-app.

Applying CRDs

Applying a new CRD involves submitting the YAML definition to the cluster. Here’s an outline:

  1. Write the CRD YAML: Define the resource's attributes, name, spec, etc.
  2. Apply the CRD:
bash
    kubectl apply -f my-custom-resource-definition.yaml
  1. Verify the CRD:
bash
    kubectl get crd

After application, the CRD becomes available like any other Kubernetes resource.

Summary Table of Commands

TaskCommand
List all CRDskubectl get crds
List CRDs with detailskubectl get crds -o wide
Fetch specific CRD detailskubectl get crd <crd-name> -o yaml kubectl get crd <crd-name> -o json
Extract using columnskubectl get crds -o custom-columns=NAME:.metadata.name,VERSION:.spec.version
Filter with labelskubectl get crds -l <label>
Apply a new CRDkubectl apply -f <yaml-file>
Verify new CRDkubectl get crd <crd-name>

Conclusion

Custom Resource Definitions expand Kubernetes capabilities, enabling more complex and specialized workloads to run efficiently. Understanding how to list and manage CRDs with kubectl ensures that administrators and developers can effectively leverage these powerful tools. From simple queries to detailed inspections and labels, knowing how to navigate the CRD landscape is a foundational skill for anyone working within Kubernetes environments.


Course illustration
Course illustration

All Rights Reserved.