Kubernetes has come a long way since its initial release in 2014, beginning with key built-in resources such as Pods, Services, Deployments, and Replication Controllers. As developers started building more complex applications, they needed resources that went beyond what Kubernetes initially provided. They required features like custom workflows, advanced configurations, and support for non-standard infrastructure components. Built-in resources (e.g., Pods, Services) worked well for standard deployments but lacked the flexibility to manage these unique requirements. This created a need for more adaptable solutions.
As a result, Third-Party Resources (TPRs) were introduced in 2016 with Kubernetes 1.2 as an attempt to offer flexibility for external resources. However, TPRs fell short in data validation, functionality, and performance, leading to the introduction of Custom Resource Definitions (CRDs) in 2017 with Kubernetes 1.7.
In this article, we will discuss why developers are eager to enhance Kubernetes with Custom Resource Definitions (CRDs) and what types of challenges or workflows CRDs can address that built-in resources cannot.
By the end of this article, you will understand the critical function CRDs play in Kubernetes and why they have become an essential tool for developers looking to construct complex, flexible processes in Kubernetes.
What is a custom resource definition (CRD)?
Just as programmable driving modes in modern cars allow drivers to personalize their experiences by adjusting things like seat positions or steering sensitivity, CRDs are a powerful feature that enables Kubernetes users to extend the platform with custom resources tailored to their specific needs.
Unlike TPRs, CRDs offer advanced features, including API versioning, seamless integration with custom controllers, and scalability, making them first-class citizens in the Kubernetes ecosystem. While CRDs didn’t single-handedly transform Kubernetes, they have significantly contributed to its adaptability, giving developers the power to create resources specific to their applications’ needs.
CRDs allow developers to model non-standard resources in Kubernetes, giving them the flexibility to manage anything from databases and backups to complex workflows and custom infrastructure components. With the help of CRDs, Kubernetes transforms from a simple tool for orchestrating containers into a highly flexible platform that can manage almost any infrastructure or application.
CRDs can be paired with custom controllers, which use newly defined resources to automate complex tasks. This collaboration allows developers to manage applications or infrastructure declaratively, just as Kubernetes already does for its core resources.
Ultimately, CRDs enable Kubernetes to become an expandable system that can be reshaped to meet specific requirements. Developers can specify their resources, create custom objects and have Kubernetes manage them properly, reducing the burden of maintaining varied and specialized applications.
Security Considerations for CRDs
While CRDs are incredibly powerful, they come with potential security implications. Improper configurations, unvalidated data, or overly permissive access can open up vulnerabilities in your Kubernetes cluster. Some good ways to avoid these risks include:
- Enforcing RBAC policies to restrict who can define or manage CRDs.
- Defining strong validation schemas to ensure custom resources adhere to expected structures.
- Securing custom controllers by running them with minimal permissions and regular updates.
To fully appreciate CRDs’ potential, it is essential to understand the relationship between Kubernetes API and the differences between native and custom resources.
Kubernetes API and native resources vs custom resources
The Kubernetes API is Kubernetes’ engine room. It handles how resources, both native and custom, are created, defined, and managed. It exposes Kubernetes resources (like pods, services, etc.) by providing a RESTful interface that allows clients to interact with its API server using HTTP requests.
So, running commands like “kubectl get pods -n e-commerce” basically uses the HTTP “get” method to pull information from all the pods in that namespace. This is useful as it uses Kubernetes resource-based URLs to represent Kubernetes resources.
Native Kubernetes resources (like deployments and pods) are usually predefined in the API and have their schemas and controllers. This means that APIs have a way of accessing and handling these native resources.
Each native resource in Kubernetes has a defined structure that includes four critical components used by Kubernetes to understand, create, and manage them. They are:
- ApiVersion: Specifies the version of the Kubernetes API to use for a particular resource to best interpret its schema and behavior.
- Kind: Defines the resource type being managed to help Kubernetes understand its role and how controllers should handle it.
- Metadata: Contains info about the resource’s name, namespace, labels, and annotations. Metadata allows the resource to be referenced and discovered.
- Spec: The spec defines things like configurations and requirements and gives Kubernetes insight into the desired state of the resource to be managed. Kubernetes can then use its API to track when the resource deviates from this state.
It is important to note that Kubernetes has built-in knowledge of these native resources through its predefined schemas and controllers to manage them more easily. The downside is that these schemas and controllers only cover some potential needs developers may have.
To address the growing complexity of developer needs without expanding the core Kubernetes API, CRDs were introduced. CRDs allow developers to define their own resource types, seamlessly integrating them into the Kubernetes ecosystem. This flexibility enables custom resources to feel like native Kubernetes components.
Although Custom Resources function much like native resources, they have important distinctions that affect how they are used and managed within Kubernetes. Let’s explore these differences between native and custom resources.
Native Resources vs Custom Resources
While Kubernetes API manages both, they portray differences in structure, behavior, and purpose.