DEV Community

Cover image for Kubernetes Custom Resources
CiCube for CICube

Posted on • Originally published at cicube.io

Kubernetes Custom Resources


cicube.io

Introduction

Kubernetes Custom Resources extend the API of Kubernetes and define new resource types. This makes it possible to narrow solutions to act on a Kubernetes cluster according to whatever need exists. In the following, we will elaborate on particularities of custom resources, have a closer look at when to use, and how they manifest extensibility. Whether you consider implementing a so-called simple CRD or an Aggregated API, you will find here the detailed insights that will help you make informed decisions.

What are Custom Resources?

What makes Custom Resources so powerful in Kubernetes is that they allow me to extend the Kubernetes API with new resource types. Consequently, this means I can tailor my environment according to specific application needs. Unlike built-in resources, Custom Resources will dynamically appear or disappear depending on their registration to allow more modular cluster setups.

For instance, I would define a CRD for a new resource type, say, Database. It would be a custom resource that one could then manage through kubectl, just like any other object in Kubernetes, and would make perfect sense to the team. And, for example, when I run kubectl create, kubectl get, and kubectl delete, I'm operating not just the built-in resources but my custom ones too. One nice feature is that these custom resources can also function independent of the overall cluster's life cycle. For example, even though my cluster may be down for maintenance, it does not mean my custom resource objects are not manageable. This allows a lot of flexibility and control over the kind of resources I handle, which in turn really aids operational efficiency in a Kubernetes ecosystem.

Role of Custom Controllers

The custom controller, to me, is an integral part as it extends the functionalities of Kubernetes resources in working with custom resources; this is done by leading in a declarative API model. When I use a custom resource accompanying a custom controller, it allows me to declare a desired state of my resource and ensure the actual state is consistent with that declaration.

This separation means I focus on what needs to be done rather than how that might be achieved, which is quite the reverse of an imperative API approach.

One of the central patterns in this landscape is the Operator pattern. This allows me to encapsulate domain-specific knowledge of my applications into the Kubernetes API so that I have a smarter API that is more capable of managing complex scenarios itself. For instance, I could write a controller that automatically manages the lifecycle of a database application, scaling, performing backups, or failover without requiring manual intervention. Moreover, the deployment and updating of custom controllers take place without interfering with the life cycle of the Kubernetes cluster itself. This implies that I can upgrade the controllers easily without necessarily bringing down the entire cluster or causing disruptions to other workloads, hence greatly enhancing operational efficiency.

API Integration: When to Choose Custom Resources

In considering when to extend a new API with Kubernetes, versus creating a standalone service, there are a few determining factors. In general, this will boil down to whether your API describes and supports a declarative model or whether your API is targeted at imperative operations. If your API is specifying a desired state-for instance, defining how an application should be configured-then custom resources may be appropriate. It allows for easy integration with Kubernetes tooling such as kubectl.

Another aspect to decide upon is how you want your API to relate to the Kubernetes UI. If you want to have representation in the Kubernetes dashboard, then your API should be aggregated, so it can be presented with the native resource types. Another thing to consider is the scoping of resources. A CR naturally scopes itself to either a cluster or namespaces, which is perfect for resource isolation and ease of management.

This might be informed lastly by leveraging features of support for APIs in Kubernetes: features intrinsic in Kubernetes, such as validations, authentication, and tooling, might simplify for you the development and maintenance if your API can fit within the Kubernetes framework. Ultimately, weighing these considerations will lead you to the most effective integration strategy.


Monitoring GitHub Actions Workflows

CICube is a GitHub Actions monitoring tool that provides you with detailed insights into your workflows to further optimize your CI/CD pipeline. With CICube, you will be able to track your workflow runs, understand where the bottlenecks are, and tease out the best from your build times. Go to cicube.io now and create a free account to better optimize your GitHub Actions workflows!

CICube GitHub Actions Workflow Duration Monitoring

ConfigMaps vs Custom Resources: Which One to Choose

When to use a ConfigMap versus a custom resource in Kubernetes, there is a set of criteria that can help make a decision. First, ConfigMaps are a good fit when the configuration file formats already exist and are well-documented such as mysql.cnf or pom.xml.

This would be useful when the main application that consumes the configuration is designed to read configurations from files inside a Pod or relies on environment variables. Other scenarios where ConfigMaps apply best are when frequent rolling updates are required, as they can easily be fitted into any given deployment strategies, ensuring that the updated configurations arrive without any downtimes.

On the other hand, custom resources thrive in the scenarios that are a little bit more complex-when the features of Kubernetes API become necessary. If your use case involves needing rich interaction through the Kubernetes API-such as watch capability, or requires structured object representation of application domains, then a custom resource becomes more helpful. With custom resources, you get complete flexibility in terms of defining an entire API model, and would allow the facilitation of automation using custom controllers. In the end, decisions for using either a ConfigMap or a custom resource depend on what your application needs, how that would consume configuration data, what is the nature of the interactions needed, and how complex its management logic is.

CustomResourceDefinitions (CRDs) vs Aggregated APIs

There are primarily two ways to extend Kubernetes with personalized resources, namely CustomResourceDefinitions and Aggregated APIs. A difference in understanding will help you decide which one will suit your requirements.

Ease of Use

Generally speaking, CRDs offer an easier and less-complicated way of employing custom resources without necessarily coding. This usability determines the appropriateness of the CRD in simple scenarios-where no extra complexity is needed. On other occasions, Aggregated APIs call for programming skills and constructing of a respective service, which complicates deployment.

Programming Requirements

CRDs require no coding to be able to setup the resource itself; you will be able to create CRDs with simple YAML definitions. On the other hand, Aggregated APIs require you to code an API server and are a good choice for advanced usage when you need any custom business logic.

Service Dependencies

CRDs are served directly by the Kubernetes API server, which means they work seamlessly without requiring an external service. Aggregated APIs are served by an additional API server, which one has to maintain-a source of failure and added overhead in deployment.

Validation Features

Basic validation, using OpenAPI standards, can be supported by CRDs by defining the validation rules inside the resource specification. This will be very important to ensure that only valid configurations will be accepted. Aggregated APIs can offer even more complex mechanisms for validation - like arbitrary validation via webhooks - what would make them very interesting in applications where strong data integrity is in need.

Custom Storage Options

Another big advantage of using Aggregated APIs is flexibility in terms of custom storage solutions. If your application uses some special way of data storage, which cannot be covered by standard Kubernetes storage options, then Aggregated API allows you to implement your way of storing data. CRDs, on the other hand, are bound to the Kubernetes model of storage, which may not fit the requirements of every application.

Examples of Use Cases

CRDs fit best when one needs to implement simple extensions to the API with simple CRUD operations, such as defining a custom resource for managing application configurations that hardly change. Aggregated APIs apply to more complex applications that involve a number of dependencies and require advanced functionality, such as services that rely heavily on business logic and data validation. In the end, both CRDs and Aggregated APIs have their merits and best-fit scenarios. Your choice should align with the complexity of your requirements, the skill set of your team, and the operational overhead you are willing to manage.

Important Considerations for Custom Resources

Among several factors that are to be weighed in the case of a Kubernetes cluster about the inclusion of custom resources, one major concern is the possibility of new failure points being introduced. This is due to the fact that, by nature, a custom resource would be served either by a custom controller or additional API servers, bugs, and misconfiguration of which may lead to failures affecting system performance. Therefore, this risk underlines the need for exhaustive testing and monitoring before and after deployment.

Another critical factor is that of storage consumption. The custom resources consume the storage, just as the built-in Kubernetes resources, such as ConfigMaps, take up space. Thus, if one creates a large number of these types of resources, it will easily overload the storage capacity of the API server and performance will suffer.

Moreover, the authentication, as well as authorization requirements, must be put into consideration. With custom resources, just like the standard Kubernetes resources, Role-Based Access Control or RBAC is used. Thus, it's necessary to ensure proper access privileges to block unauthorized access.

Second, it's important to understand how client libraries support custom resources. Not all client libraries support all kinds of custom resources, and that might affect the manner in which your applications interact with them. Knowing the client libraries available-especially for languages such as Go and Python-makes for a smooth integration and operational environment.

Conclusions

With Custom Resources implemented inside a Kubernetes cluster, the latter gains huge capability for customization and scaling. Understanding just how CRDs and Aggregated APIs work, decisions will be well-informed to align with your project's goals. Be it ease of use or requiring flexibility in advanced features, when to use CRDs, or AA server integration becomes important for effectively leveraging extensibility in Kubernetes.

Top comments (0)