Kubernetes Controller = control loop
A Kubernetes controller is a control loop(reconcile loop) that
continuously watches the state of kubernets cluster and makes changes to
align the actual state with your desired state.
This is similar to = File Watcher(fsnotify)
How Controllers Run (Reconciliation Loop = Infinite Loop)
1. Observe: Look at the current state of the world
2. Analyze: Compare the Current State with the Desired State
3. Act: Take corrective action to bridge the gap
Types of Controllers
1. Kubernetes Operator (Domain specific Controller)
Instead of managing built-in resources like Pods or Deployments this Controller manages Custom Resources (CR). CRD means my specific resource(e.g., a MyDatabase resource). Every Kubernetes controller runs an infinite reconcile loop:
while (1) {
if (state changed)
Apply state
}
Code Example
-
hello World pod is created everytime its deleted
Kubebuilder = framework/SDK
Framework to build Kubernetes APIs(or Kinds) using
Custom Resource Definitions (CRDs)
- API types under `api/`
- Controller under `internal/controller/`
- CRD/RBAC/manifests under `config/`
- Makefile targets (`make run`, `make deploy`)
Terms
Custom Resource Definition (CRD)
Resources which are created by user. Controller manages Custom Resources (CR).
Controller-Runtime
A Go library (built by Kubernetes SIGs) that provides caching, clients, and leader election