Lesson 92 · Section 7: Security
Lesson 92: Operator Framework: building, installing and upgrading operators
What an operator adds to a CRD and a controller, the three parts of the Operator Framework (Operator SDK, Operator Lifecycle Manager, OperatorHub.io), how a Subscription becomes a running operator, and the exam routine: install an operator, then create its custom resource. · 17 min read · 4 min video
Official competency this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why operators exist
The last two lessons gave you two building blocks. A CustomResourceDefinition (CRD) teaches the API server a new kind of object. A custom controller watches objects of that kind and acts on them. Neither is much use alone: a CRD without a controller is only a place to store YAML, and a controller without a CRD has nothing to watch.
An operator is the two together, built for one specific application. The name comes from the job it replaces. A human operator who runs a database knows how to bootstrap it, add a replica, take a backup before an upgrade and promote a standby when the primary dies. A Deployment or a StatefulSet knows none of that; they only know how to start and replace Pods. An operator is that human knowledge written as a controller.
Parts:
- You (kubectl apply)
- Prometheus main (custom resource)
- Operator Pod (watches, reconciles)
- Secret (generated config)
- Service (prometheus-operated)
- StatefulSet (prometheus-main)
Connections:
- You to Prometheus main: create
- Prometheus main to Operator Pod: watch
- Operator Pod to StatefulSet: creates
- Operator Pod to Service: creates
- Operator Pod to Secret: creates
The diagram uses the Prometheus operator as the example. You create a 12-line object of kind Prometheus. The
operator answers with a StatefulSet, a Service, a generated configuration Secret, and it keeps all three in line with
your object for as long as it exists. Change replicas in the custom resource and the StatefulSet changes; delete
the StatefulSet and the operator recreates it.
That leaves three practical questions, and they are what the Operator Framework answers:
- How do you write an operator without starting from an empty file?
- How do you find an operator someone else has written, and trust that it is packaged properly?
- How do you install and upgrade it, with its CRDs and RBAC, without breaking the custom resources that depend on it?
What the Operator Framework is
The Operator Framework is an open source toolkit, a CNCF project, with one part for each question.
Parts:
- Developer (Go, Ansible or Helm)
- Operator SDK (scaffold, build, test)
- Bundle image (CSV and CRDs)
- OperatorHub.io (catalog of bundles)
- OLM (installs, upgrades)
- Operator (running in a Pod)
Connections:
- Developer to Operator SDK: writes
- Operator SDK to Bundle image: packs
- Bundle image to OperatorHub.io: published in
- OperatorHub.io to OLM: reads
- OLM to Operator: deploys
| Part | What it is | Who uses it |
|---|---|---|
| Operator SDK | A command line tool (operator-sdk) that scaffolds, builds, tests and packages operators | Developers who write operators |
| Operator Lifecycle Manager (OLM) | A set of controllers and CRDs that runs in your cluster and installs, upgrades and removes operators | Cluster administrators |
| OperatorHub.io | A public catalog of packaged operators, with install instructions for each one | Anyone looking for an operator |
As a CKA candidate you sit on the administrator side. You need to recognise the SDK, but your work is installing an operator, checking that it runs and creating the custom resources it manages.
Operator SDK: three ways to write an operator
The SDK supports three kinds of operator. They differ in how much the operator can do.
| Type | You write | Good for |
|---|---|---|
| Helm | Nothing new: the SDK wraps an existing Helm chart. The custom resource's spec becomes the chart's values | Install and upgrade of stateless applications |
| Ansible | Ansible roles and playbooks that run on every reconcile | Teams that already automate with Ansible |
| Go | A reconcile function in Go, on top of the same libraries the built-in controllers use | Anything, including failover and automatic tuning |
The SDK also packs the result into a bundle: a small image holding the operator's CRDs and a ClusterServiceVersion (CSV), the manifest that describes one version of the operator. You will meet the CSV again in a moment.
Capability levels: how much an operator does for you
Not every operator is equally clever. The framework defines five capability levels, and OperatorHub.io shows the level of each operator:
| Level | Name | The operator can |
|---|---|---|
| 1 | Basic Install | Install and configure the application from a custom resource |
| 2 | Seamless Upgrades | Upgrade the application and itself to new versions |
| 3 | Full Lifecycle | Take backups, restore, and recover from failures |
| 4 | Deep Insights | Expose metrics, alerts and log processing |
| 5 | Auto Pilot | Scale, tune and heal by itself |
Helm-based operators stop at level 2, because a chart can install and upgrade but cannot react to what the application is doing. Ansible and Go operators can reach all five.
How OLM installs an operator
An operator is more than a Deployment. It needs its CRDs in place first, a ServiceAccount with exactly the right RBAC rules, sometimes webhooks and certificates, and a safe route from each version to the next. OLM treats all of that as one unit.
The objects OLM adds
OLM is itself built from CRDs and controllers. Once it is installed you have these new kinds:
| Kind | Short name | What it means |
|---|---|---|
CatalogSource | catsrc | A catalog of operators the cluster can install from. A default install has operatorhubio-catalog |
PackageManifest | One installable package in a catalog, with its channels. Read-only: OLM generates it | |
Subscription | sub | "Keep this package, from this channel of this catalog, installed and up to date" |
InstallPlan | ip | The list of resources OLM will create for one install or upgrade, with an approval switch |
ClusterServiceVersion | csv | One version of one operator: its Deployment, permissions and the CRDs it owns |
OperatorGroup | og | Which namespaces the operators installed in this namespace may watch |
From Subscription to running Pod
Parts:
- Subscription (package and channel)
- InstallPlan (list of steps)
- CSV (phase: Succeeded)
- CatalogSource (operatorhubio-catalog)
- CRDs (new API kinds)
- Deployment (the operator Pod)
Connections:
- Subscription to CatalogSource: looks up
- Subscription to InstallPlan: plans
- InstallPlan to CSV: writes
- InstallPlan to CRDs: installs
- CSV to Deployment: starts
You create exactly one object, the Subscription. The rest follows:
- OLM's catalog operator sees the Subscription and looks up the package in the named CatalogSource. It picks the newest version in the channel and works out anything that version depends on.
- It writes an InstallPlan listing every resource to create.
- If the Subscription says
installPlanApproval: Automatic(the default), the plan is carried out at once. WithManual, it waits until someone setsspec.approved: trueon it. - Carrying out the plan creates the CRDs, the ServiceAccount and RBAC objects, and the CSV.
- The OLM operator sees the CSV, checks that its requirements are met, and creates the operator's Deployment.
When the Pods are ready, the CSV reaches
phase: Succeeded.
Upgrades reuse the same path. When the catalog gains a newer version in your channel, the Subscription produces a new InstallPlan, and a new CSV replaces the old one. This is the main thing OLM gives you over a plain manifest: the operator stays current, and you can put a manual approval in front of each upgrade.
Under the hood · Where OLM lives, and what OperatorGroups are for
A default OLM install creates two namespaces:
olmholds OLM itself: theolm-operator, thecatalog-operator, thepackageserver(which serves the PackageManifest API) and one Pod per CatalogSource.operatorsis where you put Subscriptions for operators that should watch the whole cluster. It contains an OperatorGroup calledglobal-operatorsthat selects all namespaces.
Every namespace that holds a Subscription needs exactly one OperatorGroup. It tells the operators installed
there which namespaces to watch. With no OperatorGroup, or with two of them, the CSV does not install and its status
says so. To install an operator that watches only its own namespace, create the namespace, an OperatorGroup whose
spec.targetNamespaces lists it, and then the Subscription.
Under the hood · OLM v1: ClusterExtension and ClusterCatalog
The objects above belong to the original OLM, often called OLM v0, with the API group operators.coreos.com. The
project has also released a redesigned OLM v1 with a much smaller API in the group olm.operatorframework.io:
- A
ClusterCatalogpoints at a catalog image, in place of a CatalogSource. - A
ClusterExtensionasks for a package, optionally with a channel or a version range, and names the namespace to install into. It takes the place of the Subscription, the InstallPlan and the CSV.
apiVersion: olm.operatorframework.io/v1
kind: ClusterExtension
metadata:
name: cert-manager
spec:
namespace: cert-manager
source:
sourceType: Catalog
catalog:
packageName: cert-manager
version: "1.x"
Both generations are in use and the catalogs work with either. If you meet a cluster, check which API group is
present with kubectl api-resources | grep -i -e operators.coreos -e operatorframework before you write YAML.
Three ways to install an operator
OLM is optional. It is not part of Kubernetes and a kubeadm cluster does not have it. Many operators are installed without it.
| Way | What you run | Upgrades | When you meet it |
|---|---|---|---|
| Plain manifests | kubectl create -f <file or URL> with the CRDs, RBAC and Deployment in one file | You apply the next version's file | Most upstream projects publish one. The likeliest form in the exam |
| Helm chart | helm install <release> <chart> | helm upgrade; note that Helm does not upgrade CRDs kept in a chart's crds/ folder | Clusters that already use Helm for components |
| OLM | A Subscription (or the install file from OperatorHub.io) | Automatic or approved by hand, following the channel | Clusters that have OLM, and every OpenShift cluster |
All three end in the same place: CRDs in the API, a controller Pod running with a ServiceAccount, and nothing managed yet. The application appears only when you create a custom resource.
Using it
Example · Install an operator from a manifest and create its custom resource
The task: "Install the Prometheus operator and run a Prometheus server with two replicas in the namespace
default." No OLM is involved.
Install the operator. The project publishes one file, bundle.yaml, with every CRD, the RBAC objects and the
Deployment:
LATEST=$(curl -s https://api.github.com/repos/prometheus-operator/prometheus-operator/releases/latest | jq -cr .tag_name)
curl -sL https://github.com/prometheus-operator/prometheus-operator/releases/download/${LATEST}/bundle.yaml \
| kubectl create -f -
Check the two things every operator install gives you, the controller and the new kinds:
kubectl get deployment prometheus-operator
kubectl api-resources --api-group=monitoring.coreos.com
NAME SHORTNAMES APIVERSION NAMESPACED KIND
alertmanagers am monitoring.coreos.com/v1 true Alertmanager
podmonitors pmon monitoring.coreos.com/v1 true PodMonitor
prometheuses prom monitoring.coreos.com/v1 true Prometheus
servicemonitors smon monitoring.coreos.com/v1 true ServiceMonitor
...
Nothing is monitoring anything yet. Create the custom resource:
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
name: main
spec:
replicas: 2
resources:
requests:
memory: 400Mi
kubectl apply -f prometheus.yaml
kubectl get prometheus main
kubectl get statefulset,pods -l app.kubernetes.io/instance=main
NAME READY AGE
statefulset.apps/prometheus-main 2/2 50s
NAME READY STATUS RESTARTS AGE
pod/prometheus-main-0 2/2 Running 0 50s
pod/prometheus-main-1 2/2 Running 0 50s
You never wrote a StatefulSet. To scale, edit the custom resource, not the StatefulSet:
kubectl patch prometheus main --type merge -p '{"spec":{"replicas":3}}'.
Example · Install OLM, then an operator from OperatorHub.io
Install OLM on a practice cluster. Either command works; the first needs a release tag, which you take from the project's releases page:
OLM=v0.28.0 # use the newest tag from the releases page
curl -sL https://github.com/operator-framework/operator-lifecycle-manager/releases/download/${OLM}/install.sh | bash -s ${OLM}
# or, with the SDK installed:
operator-sdk olm install
kubectl get pods -n olm
NAME READY STATUS RESTARTS AGE
catalog-operator-6b8c5d7f9c-4jq2m 1/1 Running 0 60s
olm-operator-7d9f6c8b5d-n8w2x 1/1 Running 0 60s
operatorhubio-catalog-k7xvd 1/1 Running 0 45s
packageserver-5c9d8b7f6d-2ptzl 1/1 Running 0 40s
packageserver-5c9d8b7f6d-hq9vw 1/1 Running 0 40s
Look the package up before you subscribe, so the channel name is not a guess:
kubectl get packagemanifests | grep cert-manager
kubectl get packagemanifest cert-manager -o jsonpath='{.status.defaultChannel}{"\n"}'
Subscribe in the operators namespace, which already has a cluster-wide OperatorGroup:
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
name: my-cert-manager
namespace: operators
spec:
name: cert-manager # the package
channel: stable # the release track
source: operatorhubio-catalog # the CatalogSource...
sourceNamespace: olm # ...and where it lives
kubectl apply -f subscription.yaml
kubectl get sub,ip,csv -n operators
NAME PACKAGE SOURCE CHANNEL
subscription.operators.coreos.com/my-cert-manager cert-manager operatorhubio-catalog stable
NAME CSV APPROVAL APPROVED
installplan.operators.coreos.com/install-8xk2f cert-manager.v1.16.5 Automatic true
NAME DISPLAY VERSION PHASE
clusterserviceversion.operators.coreos.com/cert-manager.v1.16.5 cert-manager 1.16.5 Succeeded
Succeeded on the CSV is the line to wait for. The version you see will be newer. The operator's Pods run in
operators, and the new kinds (Certificate, Issuer, ClusterIssuer) are ready to use. OperatorHub.io gives
the same Subscription as a one-line install for each operator, under its Install button.
Example · Hold upgrades until you approve them
On a production cluster you may not want an operator to upgrade itself overnight. Set manual approval in the Subscription:
spec:
name: cert-manager
channel: stable
source: operatorhubio-catalog
sourceNamespace: olm
installPlanApproval: Manual
Now every install and every upgrade stops at an unapproved InstallPlan:
kubectl get ip -n operators
NAME CSV APPROVAL APPROVED
install-m4t9c cert-manager.v1.17.0 Manual false
Read what it would change, then approve it:
kubectl get ip install-m4t9c -n operators -o jsonpath='{.spec.clusterServiceVersionNames}{"\n"}'
kubectl patch ip install-m4t9c -n operators --type merge -p '{"spec":{"approved":true}}'
A common surprise: with Manual, the first install waits too. A Subscription that seems to do nothing is often
waiting for this patch.
Example · Find out why a custom resource does nothing
You created a custom resource and no Pods appear. Work from the object towards the controller:
kubectl get prometheus main -o yaml | grep -A 12 '^status:' # many operators report conditions here
kubectl describe prometheus main | tail -n 8 # events recorded by the operator
kubectl get deployment -A | grep -i operator # is the controller running at all?
kubectl logs deployment/prometheus-operator --tail=30 # what did it try to do?
The usual causes, in order of likelihood:
- The operator is not running. Its Deployment has 0 ready replicas, often an image pull or RBAC problem. Without the controller, a custom resource is only stored data.
- The operator does not watch your namespace. Some operators watch one namespace, set by a flag, an environment
variable such as
WATCH_NAMESPACE, or an OperatorGroup. - The operator lacks permission. Its log shows
forbiddenerrors: the ServiceAccount's ClusterRole misses a rule, for example after someone edited it. - The custom resource is invalid in a way the schema allows. The log names the field.
Tricks
Common mistakes
- Installing the operator and stopping. The operator manages nothing until a custom resource exists. Read the task for the kind it wants you to create.
- Editing what the operator owns. Scaling the StatefulSet or editing the generated ConfigMap works for a few seconds, then the operator puts it back. Edit the custom resource.
- Guessing the channel or catalog in a Subscription. A wrong
channel,sourceorsourceNamespacegives no error on creation; the Subscription only reports the problem in its status. Look them up withkubectl get packagemanifest <name>. - Subscribing in a namespace with no OperatorGroup. The CSV is created but never succeeds. Use
operators, or create an OperatorGroup first. - Expecting OLM in every cluster. If
kubectl get csvanswersthe server doesn't have a resource type "csv", OLM is not installed. Use the operator's manifest or Helm chart. - Deleting CRDs to "clean up". That deletes every custom resource of the kind and everything those own.
Recap
- An operator is a CRD plus a controller that carries the knowledge needed to run one application.
- The Operator Framework has three parts: the Operator SDK builds operators (Helm, Ansible or Go), a catalog such as OperatorHub.io lists them, and OLM installs and upgrades them.
- With OLM you create a Subscription; it becomes an InstallPlan, then a CSV in phase
Succeeded, then a running Deployment.installPlanApproval: Manualputs you in front of each upgrade. - Without OLM, a manifest (
kubectl create -f) or a Helm chart installs the same CRDs, RBAC and Deployment. - The install is half the job: create the custom resource, change it instead of the objects the operator owns, and read the operator's logs when nothing happens.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Practise: try it on a real cluster
A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.
Section capstone. Prepare a namespace for an audit: a short-lived login for the auditor, read-only access for her group, an identity of its own for one app, a hardened container, and network policies that close the namespace.
Build · Level 3 · challenge · target 40 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.