Kubernetes Concepts: Operators and CRDs
1. Custom Resource Definitions (CRDs)
- What are CRDs? Custom Resource Definitions (CRDs) extend Kubernetes by allowing users to define their own custom resources.
- Why use CRDs? They enable the management of application-specific resources within Kubernetes, just like native resources (Pods, Services, Deployments).
-
Example Use Case:
- Defining a
Database
CRD to manage database instances via Kubernetes API.
- Defining a
2. Kubernetes Operators
-
What is a Kubernetes Operator?
- An Operator is a controller that automates application management using CRDs.
- It continuously watches for changes and ensures the desired state of the application.
-
Why use Operators?
- Automate complex deployments (e.g., databases, monitoring stacks).
- Handle upgrades, backups, and failure recovery.
-
Example Use Case:
- A PostgreSQL Operator to manage database instances in Kubernetes.
3. Key Differences Between CRDs and Operators
Feature | CRD | Operator |
---|---|---|
Purpose | Extends Kubernetes API | Automates application lifecycle |
Complexity | Defines custom resources | Uses CRDs + controllers to manage resources |
Use Case | Define resource structures | Automate complex operations (e.g., upgrades, backups) |
4. Popular Kubernetes Operators
- Prometheus Operator – Deploy and manage Prometheus monitoring stack.
- Elasticsearch Operator – Manage Elasticsearch clusters.
- Kafka Operator – Handle Kafka brokers in Kubernetes.
To implement a Custom Resource Definition (CRD) in your Kubernetes cluster, follow these steps:
1. Create the CRD Definition
Save the following YAML as mycrd.yaml
:
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: widgets.example.com
spec:
group: example.com
names:
kind: Widget
plural: widgets
scope: Namespaced
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
size:
type: string
color:
type: string
Apply it using:
kubectl apply -f mycrd.yaml
2. Define a Custom Resource (CR)
After creating the CRD, define an instance of the custom resource. Save this as widget.yaml
:
apiVersion: example.com/v1
kind: Widget
metadata:
name: my-widget
spec:
size: "large"
color: "blue"
Apply it:
kubectl apply -f widget.yaml
3. Verify the Custom Resource
Check if the CRD is created:
kubectl get crds
Check the created custom resource:
kubectl get widgets
Describe the resource:
kubectl describe widget my-widget
4. Clean Up
To remove the CRD:
kubectl delete crd widgets.example.com
Next Steps
- Implement a custom controller to manage
Widget
resources. - Extend the CRD with validation rules.
- Integrate with Kubernetes Operators for automation.
Happy Learning !!!
Top comments (0)