How To Create Custom Resources?

We can create custom resources to fulfill our unique needs using CRDs. Let’s look into the steps to create a custom resource.

We use the YAML syntax to specify the structure and schema of the resource. The attributes defined within the YAML configuration file enable us to add objects to the Kubernetes API and build unique resources suited to our application’s requirements.

Given below is the sample YAML code, which can be used to define Custom Resources named MyApp with CRDs.

```

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: myapps.sample.com
spec:
group: sample.com
version: v1
names:
 kind: MyApp
 singular: myapp
 plural: myapps

```

We can make use of our custom resources for creating instances using it. Let’s create a custom resource

In this GitHub repository, we have some sample YAML files to create CRDs. To create the CRDs, we need to run the `kubectl apply -f <filename>`. On executing the custom resources will get created with an output as shown below. 

kubectl apply

Here instead of creating the YAML file locally, we have pushed the YAML file to GitHub and used the link to create the custom resource.

If you notice above, the name of the resource is based on the YAML file `crd1.yaml` which looks like this:

custom resource definition

To get the list of custom resource definitions, you can run the `kubectl get crds` commands, which will give an output as shown below:

kubetcl get crds

How to create instances of custom resources?

Creating Instances of Custom Resources:  We can use kubectl or another Kubernetes client to create instances of the custom resources, also called custom objects. These unique objects can represent various things, including programs, services, databases, or other things pertinent to the deployed application.

How can you create instances using custom resources? Let’s look at a sample YAML file below, which can be used to create an instance of the custom resource `MyApp.`

```

apiVersion: example.com/v1
kind: MyApp
metadata:
name: myapp-1
spec:
# under this, we can specify the custom configuration for MyApp

```

Our sample YAML file to create an instance based on the above custom resource looks like this:

Create instance from CRDs in Kubernetes

We will run the kubectl create command to create the instance, which will give an instance-created message as output. To get the list of resources of our custom type we can make use of the command `kubectl get <custom_resource_name>`. Here our custom resource name is crontab, so the command is `kubectl get crontab`.

Create instances from CRDs

After successfully creating an instance of custom resources, we have to manage the whole lifecycle of the custom resources.

Managing the Lifecycle of Custom Resources

Lifecycle management of custom resources includes creation, updation, and deletion. CRDs help us to specify use lifecycle activities that can be performed, such as create, update, and delete. We can conduct these activities on the custom resources using kubectl or other Kubernetes tools, just like any other Kubernetes object.

Let’s look into some commands we can use to manage the resource life cycle.

The `kubectl create -f myapp.yaml` command is used to create a custom resource. We used this command to create the instance.

Sometimes it happens that we need some updates in my custom resources. Can we change the custom resource specification? So the answer is yes we can update the resource file.

We can use the `kubectl apply -f myapp.yaml` command to easily update a custom resource. 

If we don’t want a custom resource, we can delete the same using the `kubectl delete myapp myapp-1` command. After deleting the resource if you check the list, you will find it empty, as the resource has been deleted. Here, the resource name is crontab, and the instance name is my-new-cron-object, so the command to delete becomes `kubectl delete crontab my-new-cron-object`.

delete custom resources in Kubernetes

You can also delete the custom resource using the delete command instead of apply, as shown below:

delete CRDs in Kubernetes

Custom Resource Definitions (CRDs)

The integrated resources in Kubernetes, such as pods, services, and volumes, provide everyday functions for common-use instances. However, you can not use them on your precise and unique requirements. This is wherein CRDs come into the picture.

As the popularity of Kubernetes maintains to grow, so does the need for customizing and extending its competencies. CRDs stands for Custom Resource Definition. The name is self-explanatory. You can use CRDs to construct your custom resource when you have a particular requirement or use case no longer blanketed by means of the integrated resources. Custom Resource Definitions (CRDs) have emerged as a powerful mechanism to outline custom resources inside a Kubernetes cluster. In this article, we are able to explore CRDs, their importance, and how they empower developers and operators to create custom resources tailored to their needs.

Similar Reads

What Are Custom Resource Definitions (CRDs)?

Custom Resource Definitions (CRDs) are extensions to the Kubernetes API that enables the creation of custom resources. While Kubernetes comes with a fixed predefined useful resource kind as pods, deployments, and offerings, CRDs permit customers to create their own resource types and controllers....

Benefits of CRDs

Let’s discuss the benefits of CRDs for Kubernetes users:...

How To Create Custom Resources?

We can create custom resources to fulfill our unique needs using CRDs. Let’s look into the steps to create a custom resource....

Conclusion

In conclusion, Custom Resource Definitions (CRDs) are a vital feature in Kubernetes, which enables customers to extend and customize the platform. CRDs provide extensibility, reusability, consistency, and automation advantages, allowing customers to create domain-specific resources and enhance their Kubernetes deployments. By leveraging CRDs, customers can harness the power of Kubernetes to build resilient, scalable, and efficient systems tailored to their requirements. Understanding and using CRDs is key to unlocking the full potential of Kubernetes for various use cases....

Contact Us