🚢📦🖥️ Lesson 15 - Custom Resource Definitions (CRDs)

Introduction

Custom Resource Definitions (CRDs) allow you to extend the Kubernetes API to create your own custom resources. They enable you to define and manage custom data structures for specific use cases, beyond the built-in Kubernetes objects. This lesson covers creating and managing CRDs, providing the foundation to leverage this powerful feature.


Creating Custom Resource Definitions (CRDs)

CRDs allow you to define new types of resources in Kubernetes, enabling you to store and manage custom application configurations and state.

Example CRD:

```yaml
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
```

Applying the CRD:

```bash
kubectl apply -f crontab-crd.yaml
```

Example CronTab Instance:

```yaml
apiVersion: stable.example.com/v1
kind: CronTab
metadata:
  name: my-crontab
spec:
  cronSpec: "* * * * */5"
  image: my-cron-image
  replicas: 3
```

Managing Custom Resource Definitions (CRDs)

Managing CRDs involves updating, validating, and controlling the lifecycle of custom resources.

Updating a CRD:

Modify the CRD definition YAML file to include the changes. Apply the updated CRD using `kubectl apply -f crontab-crd.yaml`.

Versioning:

Use multiple versions to manage changes to the custom resource schema without breaking existing functionality.

```yaml
versions:
- name: v1
  served: true
  storage: true
- name: v2
  served: true
  storage: false
  schema:
    openAPIV3Schema:
      type: object
      properties:
        spec:
          type: object
          properties:
            cronSpec:
              type: string
            image:
              type: string
            replicas:
              type: integer
            timeout:
              type: string
```

Validating Custom Resources:

Use OpenAPI v3 schemas to validate custom resources.

```yaml
schema:
  openAPIV3Schema:
    type: object
    properties:
      spec:
        type: object
        properties:
          cronSpec:
            type: string
          image:
            type: string
          replicas:
            type: integer
            minimum: 1
            maximum: 10
          timeout:
            type: string
            pattern: "^[0-9]+[smhd]$"
```

Best Practices:

  • Versioning: Always version your CRDs to manage schema changes without disruption.
  • Validation: Use OpenAPI v3 schemas to validate custom resources and ensure data integrity.
  • Documentation: Document CRDs thoroughly to ensure users understand their purpose and usage.
  • Lifecycle Management: Implement lifecycle management practices, such as deprecating old versions and migrating resources to new versions.

Best Practices for Using CRDs

  • Design for Extensibility: Design CRDs with extensibility in mind to accommodate future requirements and changes.
  • Use Declarative Configurations: Manage CRDs and their instances using declarative configurations with YAML files.
  • Monitor Custom Resources: Implement monitoring for custom resources to track their status and health.
  • Access Control: Apply RBAC policies to control access to custom resources and ensure only authorized users can manage them.

Summary

Custom Resource Definitions (CRDs) are a powerful feature in Kubernetes that allows you to extend the API and create custom resources. By understanding how to create and manage CRDs, you can define and control custom data structures tailored to your specific use cases. Following best practices for CRDs ensures their effective use and integration into your Kubernetes environment.

Key Takeaways

#
Key Takeaway
1
CRDs allow you to define new types of resources in Kubernetes, enabling you to manage custom application configurations and state.
2
Creating and applying CRDs involves defining the schema, applying the CRD, and creating instances of custom resources.
3
Managing CRDs includes updating, versioning, validating, and controlling the lifecycle of custom resources.
4
Following best practices for CRDs ensures their effective use and integration into your Kubernetes environment.

Explore the contents of the other lectures - by click a lecture.

Lectures:

S No
Lecture
Topics
1
Introduction to Kubernetes Overview, Concepts, Benefits
2
Getting Started with K8s + Kind Installation, Configuration, Basic Commands
3
Getting Started with K8s + Minikube Installation, Configuration, Basic Commands
4
Kubernetes Architecture Control Plane, Nodes, Components
5
Core Concepts Pods, ReplicaSets, Deployments
6
Service Discovery and Load Balancing Services, Endpoints, Ingress
7
Storage Orchestration Persistent Volumes, Persistent Volume Claims, Storage Classes
8
Automated Rollouts and Rollbacks Deployment Strategies, Rolling Updates, Rollbacks
9
Self-Healing Mechanisms Probes, Replication, Autoscaling
10
Configuration and Secret Management ConfigMaps, Secrets
11
Resource Management Resource Quotas, Limits, Requests
12
Advanced Features and Use Cases DaemonSets, StatefulSets, Jobs, CronJobs
13
Networking in Kubernetes Network Policies, Service Mesh, CNI Plugins
14
Security Best Practices RBAC, Network Policies, Pod Security Policies
15
Custom Resource Definitions (CRDs) Creating CRDs, Managing CRDs
16
Helm and Package Management Helm Charts, Repositories, Deploying Applications
17
Observability and Monitoring Metrics Server, Prometheus, Grafana
18
Scaling Applications Horizontal Pod Autoscaling, Vertical Pod Autoscaling
19
Kubernetes API and Clients kubectl, Client Libraries, Custom Controllers
20
Multi-Tenancy and Cluster Federation Namespaces, Resource Isolation, Federation V2
21
Cost Optimization Resource Efficiency, Cost Management Tools
22
Disaster Recovery and Backups Backup Strategies, Tools, Best Practices
Prompt Engineering
In the dynamic world of containers, Kubernetes is the captain that navigates through the seas of scale, steering us towards efficiency and innovation.😊✨ - The Alchemist "

GitHub Link: 
Tags:
  • Kubernetes
  • K8s
  • Container Orchestration
  • Cloud Native
  • Docker
  • kubectl
  • Kubernetes Architecture
  • Control Plane
  • Nodes
  • Services
  • Pods
  • ReplicaSets
  • Deployments
  • Service Discovery
  • Load Balancing
  • Storage Orchestration
  • Persistent Volumes
  • Volume Claims
  • Storage Classes
  • Rollouts
  • Rollbacks
  • Self-Healing
  • ConfigMaps
  • Secrets
  • Resource Management
  • Quotas
  • Limits
  • Advanced Features
  • Networking
  • RBAC
  • Network Policies
  • Pod Security
  • CRDs
  • Helm
  • Monitoring
  • Prometheus
  • Grafana
  • Scaling
  • API Clients
  • Multi-Tenancy
  • Cluster Federation
  • Cost Optimization
  • Disaster Recovery
  • Backups
Share Now:
Last Updated: December 15, 2024 16:21:15