Sub topic 16
Extending Kubernetes with Custom Resources and Operators
This chapter assumes you know Pods, Deployments and kubectl, and that you have read how the control loop, spec and status work. Here you will see how the same pattern extends to resource types you define yourself.
Custom resources add new kinds to the API
A CustomResourceDefinition (CRD) is an object that registers a new resource kind with the API server. After you create a CRD, the API server serves a REST endpoint for the new kind. It stores the objects in etcd and validates them against a schema. You use kubectl get, apply and watch on them as you do for built-in kinds.
A CRD by itself does nothing. It only stores data. The behavior comes from a controller that watches the new kind.
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: backups.example.com
spec:
group: example.com
scope: Namespaced
names:
kind: Backup
plural: backups
singular: backup
versions:
- name: v1
served: true
storage: true
subresources:
status: {}
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
required: ["schedule"]
properties:
schedule:
type: string
retention:
type: integer
minimum: 1
status:
type: object
properties:
lastRun:
type: stringTwo details matter here:
- The
openAPIV3Schemarejects invalid objects at admission time, so your controller sees fewer malformed inputs. subresources: status: {}gives the object a separate/statusendpoint. Users writespecand the controller writesstatus, and neither overwrites the other.
Now a user can declare intent:
apiVersion: example.com/v1
kind: Backup
metadata:
name: nightly
spec:
schedule: "0 2 * * *"
retention: 7What an operator is
An operator is a custom resource plus a controller that encodes operational knowledge about an application. The controller does what a human administrator would do: create a database cluster, run a backup, replace a failed member, or perform an upgrade. The user states spec, and the operator reconciles the cluster toward it.
An operator is not a new mechanism. It uses the same pieces as built-in controllers: a watch, an informer cache, a work queue and a Reconcile function.
Common errors
- Forgetting the status subresource. Without it, a controller's update of the whole object can overwrite user changes to
spec, or the reverse. - Registering the CRD but not running the controller. Objects are accepted and stay unchanged forever. Check that the controller Pod is running before debugging the resource.
- Changing a schema incompatibly. Existing stored objects may no longer validate. Add a new version and a conversion plan instead of editing
v1in place. - Deleting a CRD. This deletes every object of that kind. Treat it as a destructive action.
Exercise
You apply a valid Backup object and kubectl get backup nightly shows it, but nothing else happens and status stays empty. What is the most likely cause, and where do you look first?
Show answer
The CRD only stores the object. No controller is reconciling it, probably because the operator is not running or is crashing. Check that the operator's Pod is running and read its logs.