What is the Kubernetes Operator Pattern and Custom Resource Definitions (CRDs)?
❓ Interview Prompt: Explain Kubernetes operators, Custom Resource Definitions, and the reconciliation control loop for automating stateful application operations.
The Operator Pattern extends Kubernetes to manage stateful, complex applications by encoding operational knowledge into custom controllers.
The Core Insight:
Kubernetes built-in controllers (Deployment Controller, ReplicaSet Controller) automate stateless workloads. But databases, message brokers, and monitoring systems need human operator knowledge — how to do backups, failovers, scaling, version upgrades. An Operator encodes this knowledge.
Custom Resource Definition (CRD):
A CRD extends the Kubernetes API with a new resource type. For example, a PostgreSQL Operator defines a PostgresCluster CRD. You can then kubectl apply a PostgresCluster manifest just like a Deployment.
Operator Components:
Famous Operators (OperatorHub.io):
Code Implementation Example
# CRD Definition (simplified) — defines new 'PostgresCluster' resource type
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: postgresclusters.postgres-operator.crunchydata.com
spec:
group: postgres-operator.crunchydata.com
names:
kind: PostgresCluster
plural: postgresclusters
scope: Namespaced
versions:
- name: v1beta1
served: true
storage: true
---
# Using the CRD — declare a PostgreSQL HA cluster declaratively
apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
name: my-postgres
spec:
image: registry.developers.crunchydata.com/crunchydata/crunchy-postgres:ubi8-15.2-0
postgresVersion: 15
instances:
- replicas: 3 # 1 primary + 2 replicas
dataVolumeClaimSpec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 100Gi
backups:
pgbackrest:
repos:
- name: repo1
s3:
bucket: my-postgres-backups
region: us-east-1Operators encode expert operational knowledge (failover, backup, upgrade) into Kubernetes controllers via CRDs — managing stateful workloads declaratively like native K8s resources.