Exploring Kubernetes Custom Resource Definitions (CRDs) with Real-World Examples

Anshul
3 min readSep 24, 2023

--

Kubernetes, the popular container orchestration platform, offers a wealth of native resources for deploying and managing containerized applications. However, sometimes, you need to define your custom resources that are specific to your application or infrastructure. This is where Kubernetes Custom Resource Definitions (CRDs) come into play. In this article, we’ll delve into CRDs, understand their significance, and walk through a real-world example to demonstrate how they can be used to extend Kubernetes’ capabilities.

What are Kubernetes Custom Resource Definitions (CRDs)?

In Kubernetes, CRDs are an extension mechanism that allows you to define custom resources and their schema. These resources can be created and managed just like native Kubernetes resources such as Pods, Services, or Deployments. CRDs are used to extend Kubernetes to fit the unique requirements of your applications or infrastructure, making it more versatile and adaptable.

CRDs consist of two primary components:

  1. CustomResourceDefinition (CRD): This defines the structure (schema) of your custom resource. It specifies the resource’s name, fields, validation rules, and versioning information.
  2. Custom Resource (CR): Instances of the custom resource defined by the CRD. These are the objects that Kubernetes operators and controllers manage.

Unlock the full potential of Kubernetes deployment and management with our comprehensive HELM course — Master the art of packaging, deploying, and managing applications effortlessly!
HELM : Kubernetes Packaging Manager for Developers & DevOps

Master Kubernetes the hard way and unlock unparalleled expertise in container orchestration.
Kubernetes with HELM: Kubernetes for Absolute Beginners CKA

Real-World Example: Creating a CRD for a Custom Application

Let’s explore a real-world example to understand how CRDs work. Suppose you have a custom application called “MyApp,” which requires specific configurations. Instead of modifying Kubernetes-native resources, you can create a custom resource called “MyAppConfig.”

Step 1: Define the CustomResourceDefinition (CRD)

Create a YAML file named myappconfig-crd.yaml to define the CRD:

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: myappconfigs.example.com
spec:
group: example.com
names:
kind: MyAppConfig
plural: myappconfigs
scope: Namespaced
versions:
- name: v1
served: true
storage: true
subresources:
status: {}
validation:
openAPIV3Schema:
type: object
properties:
replicas:
type: integer
minimum: 1
image:
type: string
environment:
type: array
items:
type: string

In this example, we define a CRD named myappconfigs.example.com. It specifies the resource's name, group, version, and schema using OpenAPI v3 validation. Our MyAppConfig resource has fields for replicas, image, and environment.

Step 2: Create a Custom Resource (CR)

Now, create a YAML file named myappconfig-instance.yaml to create an instance of the custom resource:

apiVersion: example.com/v1
kind: MyAppConfig
metadata:
name: myappconfig-sample
spec:
replicas: 3
image: myapp:v1.0.0
environment:
- ENV_VAR1=value1
- ENV_VAR2=value2

In this CR, we specify the desired configuration for our MyApp instance, including the number of replicas, Docker image, and environment variables.

Step 3: Apply the CRD and CR

Apply the CRD and CR to your Kubernetes cluster using kubectl:

kubectl apply -f myappconfig-crd.yaml
kubectl apply -f myappconfig-instance.yaml

Step 4: Verify the Custom Resource

You can now interact with your custom resource just like any other Kubernetes resource. For example, to get the MyAppConfig:

kubectl get myappconfig myappconfig-sample -o yaml

Step 5: Use Controllers and Operators

To make your custom resource truly functional, you can create Kubernetes controllers or operators that watch for changes to your custom resource and take appropriate actions. For example, you could create a controller that deploys the specified number of replicas of your custom application with the specified image and environment variables.

Conclusion

Kubernetes Custom Resource Definitions (CRDs) empower you to extend Kubernetes’ capabilities by defining custom resources tailored to your specific needs. This level of extensibility is invaluable for complex applications and infrastructure requirements. By following the example provided, you can start harnessing the power of CRDs to manage your custom resources within Kubernetes effectively.

--

--

Anshul

DevRel 🥑 DevOps / Cloud Engineer | Terraform, Ansible, Docker & Kubernetes Enthusiast 🐳 GCP | Jenkins | Terraform Certified