CodeBlanks Logo Icon CodeBlanks

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

Practice 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.

Starter code

terminal
# buraya yaz

Sample solution

terminal
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: magaza-db
  namespace: magaza
spec:
  instances: 3
  storage:
    size: 20Gi

Expected output

cluster.postgresql.cnpg.io/magaza-db created
$ kubectl get cluster -n magaza
NAME        AGE   INSTANCES   READY   STATUS                     PRIMARY
magaza-db   2m    3           3       Cluster in healthy state   magaza-db-1

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 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 Correct answer
  • Helm is more powerful
  • An operator doesn't use YAML
  • There's no difference

Explanation: Jobs like failover need continuous observation and reaction.

2 Specify the number of PostgreSQL instances the operator manages.

Question
kind: Cluster
spec:
  ____: 3
Answer
kind: Cluster
spec:
  instances: 3

Explanation: In CloudNativePG, the replica count is instances.

3 Match the operators.

Term Meaning
CloudNativePG PostgreSQL
Strimzi Kafka
cert-manager Certificates
ECK Elasticsearch

All lessons in Expert