🔧

Kubernetes Custom Resources and Controllers

Jul 11, 2024

Kubernetes and Custom Resources Lecture Notes

Overview

  • Kubernetes: Definition and Importance
    • Core resource types: Deployment, Service, Pod, Config Map, Secrets
    • Support for extensions (e.g., Istio, Argo CD, Keycloak)

Custom Resources and Custom Resource Definitions (CRDs)

  1. Custom Resource Definitions (CRD/CRDs)
    • Extend Kubernetes API with new types
    • Defined by a yaml file with API version, kind, metadata, and spec
    • Example: Istio's VirtualService CRD allows extending Istio's features in Kubernetes
  2. Custom Resources (CR)
    • Instances of the CRDs deployed in Kubernetes
    • Example: An instance of VirtualService defined by a user for Istio
  3. Custom Controllers
    • Watch and act on the Custom Resources based on CRDs
    • Written commonly in Golang (client-go library)
    • Responsible for performing the specified actions for CRs

Kubernetes Custom Resource Ecosystem

  • Supported Custom Resources: Istio, Argo CD, Keycloak, Cube Bench, etc.
  • Community and CNCF: Many incubated projects and popular controllers
  • Examples of Controllers: Istio, Prometheus, Argo CD, Cube Bench

Steps for Implementing Custom Resources

  1. Deploy CRDs
    • DevOps engineers deploy new CRDs to extend Kubernetes API capabilities
    • Methods: Helm, plain manifests, operators
  2. Deploy Custom Controllers
    • Custom controllers to manage the lifecycle and operations of the CRs
    • Watch/Listeners for actions like create, update, delete
  3. User Interaction with CRs
    • Users create CRs conforming to the CRDs' definitions
    • Actions performed through the custom controller logic

Practical Example: Using Istio

  • Installing Istio CRDs and Controllers: Using Helm or direct manifests as per Istio documentation
  • Use Case: Istio VirtualService
    • Deploy VirtualService CR as per CRD definitions
    • The Istio controller watches and performs necessary actions

Writing Custom Controllers

  • Languages: Golang preferred, but supports Python, Java
  • Key Components:
    • Watchers and Listeners: Monitor for CRUD actions on resources
    • Client-go Library: Facilitate interaction with Kubernetes API
    • Example Approach: Using controller-runtime library for writing controllers

Important Points About Custom Controllers

  • Written primarily in Golang due to existing ecosystem and support
  • Utilize Kubernetes client-go library, controller-runtime for handling the operations
  • Extend API and manage via Helm, operators, and manifests

Resources for Hands-On Learning

  • Kubernetes Sample Controller repository on GitHub for example implementations
  • CNCF for browsing supported and popular controllers and projects

Conclusion

  • Custom Resources and Controllers: Essential for extending Kubernetes functionalities
  • User and DevOps Roles: Deployment, management, and debugging of CRs and controllers
  • Tools and Documentation: Following Helm charts, repository guidelines for effective implementation

Additional Learning Tools

  • Links and Documentation: Referencing Kubernetes sample controller and CNCF projects
  • Exercises and Examples: Practicing deployments and controller implementations via actual use cases