Introduction
Custom Resource Definitions (CRDs) allow you to extend Kubernetes' capabilities by defining your own custom resources. This is particularly useful when you need to manage application-specific configurations or resources that are not natively supported by Kubernetes.
Key Concepts
Custom Resources
- Custom Resources (CRs): These are extensions of the Kubernetes API that allow you to create, configure, and manage custom objects.
- Custom Resource Definitions (CRDs): These define the schema and behavior of custom resources. They are used to register custom resources with the Kubernetes API.
Why Use CRDs?
- Extend Kubernetes: Add new types of resources to Kubernetes without modifying the core code.
- Declarative Management: Manage custom resources using the same declarative approach as native Kubernetes resources.
- Automation: Automate the management of custom resources using Kubernetes controllers.
Creating a Custom Resource Definition
Step-by-Step Guide
- Define the CRD YAML File:
- Create a YAML file that defines the custom resource. This file includes metadata, specifications, and validation schemas.
apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: name: crontabs.stable.example.com spec: group: stable.example.com versions: - name: v1 served: true storage: true schema: openAPIV3Schema: type: object properties: spec: type: object properties: cronSpec: type: string image: type: string replicas: type: integer scope: Namespaced names: plural: crontabs singular: crontab kind: CronTab shortNames: - ct
- Apply the CRD:
- Use
kubectl
to apply the CRD YAML file to your Kubernetes cluster.
- Use
- Verify the CRD:
- Check if the CRD has been successfully created.
Using Custom Resources
Creating a Custom Resource
- Define the Custom Resource YAML File:
- Create a YAML file for the custom resource instance.
apiVersion: stable.example.com/v1 kind: CronTab metadata: name: my-crontab spec: cronSpec: "* * * * */5" image: my-cron-image replicas: 3
- Apply the Custom Resource:
- Use
kubectl
to apply the custom resource YAML file.
- Use
- Verify the Custom Resource:
- Check if the custom resource has been successfully created.
Managing Custom Resources
Updating a Custom Resource
- Edit the Custom Resource:
- Use
kubectl edit
to modify the custom resource.
- Use
- Apply Changes:
- Save the changes to update the custom resource.
Deleting a Custom Resource
- Delete the Custom Resource:
- Use
kubectl delete
to remove the custom resource.
- Use
Practical Exercise
Exercise: Create and Manage a Custom Resource
-
Create a CRD for a custom resource named
Book
:- Define the CRD YAML file for
Book
with fieldstitle
,author
, andpublishedYear
.
- Define the CRD YAML file for
-
Apply the CRD:
- Use
kubectl
to apply the CRD YAML file.
- Use
-
Create a custom resource instance of
Book
:- Define a YAML file for a
Book
instance with specific values fortitle
,author
, andpublishedYear
.
- Define a YAML file for a
-
Apply the custom resource:
- Use
kubectl
to apply the custom resource YAML file.
- Use
-
Update the custom resource:
- Modify the
author
field of theBook
instance usingkubectl edit
.
- Modify the
-
Delete the custom resource:
- Use
kubectl delete
to remove theBook
instance.
- Use
Solution
- CRD YAML File:
apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: name: books.library.example.com spec: group: library.example.com versions: - name: v1 served: true storage: true schema: openAPIV3Schema: type: object properties: spec: type: object properties: title: type: string author: type: string publishedYear: type: integer scope: Namespaced names: plural: books singular: book kind: Book shortNames: - bk
- Apply the CRD:
- Custom Resource YAML File:
apiVersion: library.example.com/v1 kind: Book metadata: name: my-book spec: title: "Kubernetes for Beginners" author: "John Doe" publishedYear: 2023
- Apply the Custom Resource:
- Update the Custom Resource:
- Delete the Custom Resource:
Conclusion
In this section, you learned how to extend Kubernetes by creating and managing Custom Resource Definitions (CRDs). You now understand how to define CRDs, create custom resources, and manage them using kubectl
. This knowledge allows you to tailor Kubernetes to your specific application needs, making it a powerful tool for managing complex workloads. In the next module, we will explore monitoring and logging in Kubernetes, which is crucial for maintaining the health and performance of your applications.
Kubernetes Course
Module 1: Introduction to Kubernetes
- What is Kubernetes?
- Kubernetes Architecture
- Key Concepts and Terminology
- Setting Up a Kubernetes Cluster
- Kubernetes CLI (kubectl)
Module 2: Core Kubernetes Components
Module 3: Configuration and Secrets Management
Module 4: Networking in Kubernetes
Module 5: Storage in Kubernetes
Module 6: Advanced Kubernetes Concepts
Module 7: Monitoring and Logging
- Monitoring with Prometheus
- Logging with Elasticsearch, Fluentd, and Kibana (EFK)
- Health Checks and Probes
- Metrics Server
Module 8: Security in Kubernetes
Module 9: Scaling and Performance
Module 10: Kubernetes Ecosystem and Tools
Module 11: Case Studies and Real-World Applications
- Deploying a Web Application
- CI/CD with Kubernetes
- Running Stateful Applications
- Multi-Cluster Management