Wednesday, 26 February 2025

How to extend Kubernetes API controller using Custom Resources?

    How to extend Kubernetes API controller using 

Custom Resources?


     
Lets start with defining  Kubernetes API, Resource, Custom Resource (CR) and Custom controller

1. Kubernetes API  
    K8s API allows querying and managing the state of API objects such as Pods, Namespaces, ConfigMaps and Events etc

2. Resource 
   Resource is an endpoint in k8s API that stores collection of objects of certain kind like pod resource contains collection of Pod objects 

3. Custom resource 
     CR is an extension to k8s API which allows adding our own API objects like creating new type of resources beyond the built-in one for example Pods, Services etc.
   User can create and access its object using kubectl once CR is installed 

Custom resource Definition is a way to define Custom resource that act like blueprint about the structure and behavior of Custom resource.

4. Custom controller
    Custom controller watches custom resources and manages them to behave as expected by automatically handling their lifecycle.


Common use cases for Custom Resources 

 Custom resources are used to extend K8s with domain specific configurations or functionalities that are not covered by built-in resources. 

1. Defining Application specific configurations eg. CR for managing database clusters
2. Automating workflows _ eg. CR for managing deployments with complex rules.
3. Implementing Controllers and Operators- eg. CR for handling custom workloads like a machine learning job.
4. Storing Persistent Configuration- e.g. CR for defining network policies or storage settings.

Example Custom resource 


Here is a simple Custom Resource Definition and Custom Resource for managing a deployment named 'roshini' with some custom rules

1. Custom Resource Definition (CRD)

This defines a new resource type called ManagedDeployment.


2. Custom Resource (CR)

This is an instance of ManagedDeployment for managing a deployment named "roshini" with complex rules


 

How It Works:

  • The CRD registers ManagedDeployment as a new resource type in Kubernetes.
  • The CR (roshini) specifies:
    • Starts with 3 replicas
    • Enables autoscaling
    • Can scale between 2 and 10 replicas
  • A Custom Controller (if implemented) would read this CR and automatically adjust the deployment based on these rules.

This setup is useful for managing applications dynamically with Kubernetes Operators!