Docker & Kubernetes
What is an Operator?
Learn that the Operator pattern encodes human knowledge.
What is an operator?
An operator is a Kubernetes controller that turns the knowledge of operating an application (installation, backup, upgrade, failover) into code. It usually comes with a CRD: the user writes a high-level request ("3-replica PostgreSQL, a backup every night"), and the operator creates and manages the needed StatefulSets, Services, Secrets and CronJobs.
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: magaza-db
spec:
instances: 3
storage:
size: 20Gi
# the CloudNativePG operator manages the pods, replication and failover
Common operators: CloudNativePG, Strimzi (Kafka), Prometheus Operator, cert-manager, ECK (Elasticsearch).
Installing an operator
Operators are usually installed with Helm or a single manifest; the installation brings the CRDs, the operator Deployment and the RBAC permissions. After installing, you work with the types the operator introduces:
kubectl apply --server-side -f https://raw.githubusercontent.com/cloudnative-pg/cloudnative-pg/release-1.24/releases/cnpg-1.24.1.yaml
kubectl get crds | grep cnpg
kubectl get deploy -n cnpg-system
Exercise A database with an operator
Write a Cluster YAML for CloudNativePG: apiVersion: postgresql.cnpg.io/v1; name magaza-db, namespace magaza; spec.instances: 3; spec.storage.size: 20Gi.
Show example solution
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: magaza-db
namespace: magaza
spec:
instances: 3
storage:
size: 20Gi
Test yourself
-
What is the main difference between an operator and a Helm chart?
- An operator keeps running after installation and manages the app; Helm only installs/upgrades
- Helm is more powerful
- An operator doesn't use YAML
- There's no difference
Show answer
An operator keeps running after installation and manages the app; Helm only installs/upgrades — Jobs like failover need continuous observation and reaction.
-
Specify the number of PostgreSQL instances the operator manages.
kind: Cluster
spec:
____: 3
Show answer
kind: Cluster
spec:
instances: 3
-
Match the operators.
- CloudNativePG
- PostgreSQL
- Strimzi
- Kafka
- cert-manager
- Certificates
- ECK
- Elasticsearch
Next lesson: Controller logic