QA
Intermediate LevelDocker & Deployment

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:

1
CRD: Defines the new resource type schema.
2
Custom Controller: Watches for CRD instances and reconciles the desired state.
3
Reconciliation Loop: Continuously compares actual state vs. desired state and takes corrective action.

Famous Operators (OperatorHub.io):

•
PGO (CrunchyData): PostgreSQL operator with HA, backups, point-in-time recovery.
•
Prometheus Operator: Manages Prometheus/Alertmanager configuration via CRDs.
•
Strimzi: Kafka cluster management on Kubernetes.
•
Crossplane: Cloud infrastructure provisioning operator.

Code Implementation Example

yaml
# 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-1
💡PostgreSQL Operator CRD: declare a 3-node HA cluster with S3 backups in 20 lines — operator handles all operational complexity.
🎯 Interview Key Takeaway

Operators encode expert operational knowledge (failover, backup, upgrade) into Kubernetes controllers via CRDs — managing stateful workloads declaratively like native K8s resources.

#Kubernetes#Operators#CRD#Custom Controllers#Reconciliation#Intermediate
Back to all Docker & Deployment Questions→

More Docker & Deployment Interview Questions

Beginner LevelDocker & Deployment

How do Docker Multi-Stage Builds minimize image size and improve security?

Beginner LevelDocker & Deployment

What is the difference between a Docker Container and a Virtual Machine (VM)?

Beginner LevelDocker & Deployment

What is the difference between a Docker Image and a Docker Container?