Sub topic 18
Packaging, deploying and operating operators
This chapter assumes you can read a reconcile loop, know what a CRD and RBAC role are, and have scaffolded a project with kubebuilder. Here you will ship the operator and keep it running.
What you ship
An operator is more than one container image. A deployable bundle holds four parts:
- The CRDs.
- The RBAC objects: a ServiceAccount, a Role or ClusterRole, and bindings.
- The manager Deployment.
- Webhook configuration and certificates, if you use webhooks.
Install them in dependency order. The CRD must exist before any custom resource, and the RBAC must exist before the manager starts.
Choosing a packaging method
| Method | Best for | Watch out for |
|---|---|---|
| Kustomize (`make deploy`) | Kubebuilder projects, simple installs | You manage upgrades yourself |
| Helm chart | Teams that already use Helm | Helm does not upgrade files in `crds/` |
| OLM bundle | Catalogs and managed upgrades | Extra components to run |
Kubebuilder generates the Kustomize layout under config/. Running make deploy IMG=registry.example.com/webapp-operator:v0.1.0 applies it.
A production-minded manager Deployment
The manager needs leader election, so you can run several replicas and only one reconciles. It also needs health probes.
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp-operator
namespace: webapp-system
spec:
replicas: 2
selector:
matchLabels:
app: webapp-operator
template:
metadata:
labels:
app: webapp-operator
spec:
serviceAccountName: webapp-operator
containers:
- name: manager
image: registry.example.com/webapp-operator:v0.1.0
args:
- --leader-elect
- --health-probe-bind-address=:8081
livenessProbe:
httpGet:
path: /healthz
port: 8081
readinessProbe:
httpGet:
path: /readyz
port: 8081With two replicas, the standby takes over within the lease timeout if the leader dies. Without --leader-elect, both replicas reconcile the same objects and conflict with each other.
Operating day to day
- Metrics. controller-runtime exposes Prometheus metrics, including
controller_runtime_reconcile_errors_totaland reconcile duration. Alert on a rising error count. - Logs. Log the resource name and namespace on every reconcile so you can trace one object.
- Events. Emit Kubernetes events for meaningful changes. They show up in
kubectl describe.
Upgrades and removal
Upgrade the CRD first, then the manager. Keep schema changes backward compatible, and use a conversion webhook when versions differ.
Common errors
- Forbidden errors in the logs. The ClusterRole is missing a verb. Fix the
+kubebuilder:rbacmarkers and regenerate the manifests. - Webhook timeouts. The certificate expired or the Service has no ready endpoints. Check cert-manager.
- Old CRD schema after a Helm upgrade. Apply the CRDs separately with
kubectl apply --server-side.
Exercise
Why must you delete custom resources before uninstalling an operator that uses finalizers?
Show answer
Finalizers are removed by the operator's reconcile loop. If the operator is already gone, nothing clears them, so the objects stay in Terminating and the namespace can hang on deletion.