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

  1. 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
  1. Apply the CRD:
    • Use kubectl to apply the CRD YAML file to your Kubernetes cluster.
kubectl apply -f crontab-crd.yaml
  1. Verify the CRD:
    • Check if the CRD has been successfully created.
kubectl get crd

Using Custom Resources

Creating a Custom Resource

  1. 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
  1. Apply the Custom Resource:
    • Use kubectl to apply the custom resource YAML file.
kubectl apply -f my-crontab.yaml
  1. Verify the Custom Resource:
    • Check if the custom resource has been successfully created.
kubectl get crontab

Managing Custom Resources

Updating a Custom Resource

  1. Edit the Custom Resource:
    • Use kubectl edit to modify the custom resource.
kubectl edit crontab my-crontab
  1. Apply Changes:
    • Save the changes to update the custom resource.

Deleting a Custom Resource

  1. Delete the Custom Resource:
    • Use kubectl delete to remove the custom resource.
kubectl delete crontab my-crontab

Practical Exercise

Exercise: Create and Manage a Custom Resource

  1. Create a CRD for a custom resource named Book:

    • Define the CRD YAML file for Book with fields title, author, and publishedYear.
  2. Apply the CRD:

    • Use kubectl to apply the CRD YAML file.
  3. Create a custom resource instance of Book:

    • Define a YAML file for a Book instance with specific values for title, author, and publishedYear.
  4. Apply the custom resource:

    • Use kubectl to apply the custom resource YAML file.
  5. Update the custom resource:

    • Modify the author field of the Book instance using kubectl edit.
  6. Delete the custom resource:

    • Use kubectl delete to remove the Book instance.

Solution

  1. 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
  1. Apply the CRD:
kubectl apply -f book-crd.yaml
  1. 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
  1. Apply the Custom Resource:
kubectl apply -f my-book.yaml
  1. Update the Custom Resource:
kubectl edit book my-book
  1. Delete the Custom Resource:
kubectl delete book my-book

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

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

Module 8: Security in Kubernetes

Module 9: Scaling and Performance

Module 10: Kubernetes Ecosystem and Tools

Module 11: Case Studies and Real-World Applications

Module 12: Preparing for Kubernetes Certification

© Copyright 2024. All rights reserved