Lesson 2.1: The Operator Pattern

Introduction

In Module 1, you learned about Kubernetes controllers, Custom Resources, and the reconciliation pattern. Operators are controllers that manage Custom Resources using domain-specific knowledge. This lesson explains what operators are, when to use them, and how they differ from other Kubernetes tools.

Theory: The Operator Pattern

Operators are a pattern for packaging, deploying, and managing Kubernetes applications using Custom Resources and controllers. They extend Kubernetes by encoding operational knowledge into software and follow the controller pattern while adding domain-specific intelligence.

Core Philosophy

Operational Knowledge as Code:

  • Operators encode how to deploy, configure, and manage applications
  • This knowledge is captured in code, not documentation
  • Makes operations repeatable and consistent
  • Domain knowledge is embedded in the controller logic

Self-Service Automation:

  • Users declare what they want (Custom Resource)
  • Operator handles the complexity (deployment, scaling, backups, etc.)
  • Reduces operational burden on users
  • Follows Kubernetes declarative model

Kubernetes-Native:

  • Operators use Kubernetes APIs and patterns
  • Feel like built-in Kubernetes features
  • Integrate with existing tooling (kubectl, Helm, etc.)

Why Operators Matter

  • Automation: Reduces manual operational tasks
  • Consistency: Ensures correct application state
  • Expertise: Encodes best practices
  • Lifecycle Management: Handles full application lifecycle (installation, upgrades, backups, scaling)

Operator Capability Levels

The Operator Capability Model (Level 1-5) helps understand operator sophistication:

  • Level 1-2: Basic deployment and upgrades
  • Level 3: Full lifecycle management
  • Level 4-5: Advanced automation and self-healing

Most production operators aim for Level 3+, providing comprehensive lifecycle management.

When to Use Operators

Good Use Cases:

  • Complex stateful applications (databases, message queues)
  • Applications requiring domain-specific knowledge
  • Applications needing lifecycle management (backup, restore, upgrade)
  • Applications requiring coordination of multiple resources
  • Need continuous management and monitoring

Not Ideal For:

  • Simple stateless applications (use Deployment)
  • One-time tasks (use Job)
  • Simple configuration (use ConfigMap)
  • Static content (use Deployment + ConfigMap)

Operator vs Other Tools

Operators vs Helm:

  • Helm: Package manager, templating, installation
  • Operators: Lifecycle management, automation, intelligence
  • Often used together: Helm installs, Operator manages

Operators vs ConfigMaps:

  • ConfigMaps: Static configuration
  • Operators: Dynamic, intelligent management

Understanding when to use operators helps you make the right architectural decisions.

What is an Operator?

An operator is a Kubernetes controller that:

  • Manages Custom Resources (CRDs) you define
  • Encodes operational knowledge (how to deploy, scale, backup, etc.)
  • Automates complex application management tasks
  • Extends Kubernetes with domain-specific behavior
graph TB
    subgraph "Kubernetes Cluster"
        USER[User]
        CR[Custom Resource]
        OPERATOR[Operator Controller]
        K8S[Kubernetes Resources]
    end
    
    USER -->|Creates| CR
    CR -->|Watched by| OPERATOR
    OPERATOR -->|Reconciles| K8S
    OPERATOR -->|Updates| CR
    
    style OPERATOR fill:#FFB6C1
    style CR fill:#90EE90

Operator Philosophy

Operators follow the same pattern you learned in Lesson 1.3:

  1. Watch Custom Resources
  2. Compare desired state (spec) vs actual state
  3. Reconcile by creating/updating Kubernetes resources
  4. Update status to reflect actual state

The difference: operators encode domain knowledge about how to manage specific applications.

Operator Capability Levels

Operators can have different levels of sophistication:

graph LR
    L1[Level 1:<br/>Basic Install] --> L2[Level 2:<br/>Seamless Upgrades]
    L2 --> L3[Level 3:<br/>Full Lifecycle]
    L3 --> L4[Level 4:<br/>Deep Insights]
    L4 --> L5[Level 5:<br/>Auto Pilot]
    
    style L1 fill:#90EE90
    style L5 fill:#FFB6C1

Level 1: Basic Install

  • Deploys application
  • Basic configuration

Level 2: Seamless Upgrades

  • Handles version upgrades
  • Rolling updates

Level 3: Full Lifecycle

  • Backup and restore
  • Disaster recovery

Level 4: Deep Insights

  • Metrics and monitoring
  • Performance tuning

Level 5: Auto Pilot

  • Self-healing
  • Automatic optimization

Operators vs Other Tools

Operators vs Helm Charts

graph TB
    subgraph "Helm Chart"
        HELM[Helm Chart]
        HELM --> STATIC[Static Templates]
        HELM --> INSTALL[Install Once]
    end
    
    subgraph "Operator"
        OP[Operator]
        OP --> DYNAMIC[Dynamic Logic]
        OP --> CONTINUOUS[Continuous Management]
    end
    
    style HELM fill:#FFE4B5
    style OP fill:#FFB6C1

Helm Charts:

  • Template-based deployment
  • Install and forget
  • No ongoing management
  • Good for: Simple applications, one-time setup

Operators:

  • Code-based logic
  • Continuous reconciliation
  • Ongoing management
  • Good for: Complex applications, stateful services, databases

When to Use Operators

flowchart TD
    START[Need to Deploy App] --> Q1{Complex<br/>Lifecycle?}
    Q1 -->|No| HELM[Use Helm]
    Q1 -->|Yes| Q2{Need Ongoing<br/>Management?}
    Q2 -->|No| HELM
    Q2 -->|Yes| Q3{Stateful<br/>Application?}
    Q3 -->|No| CONSIDER[Consider Operator]
    Q3 -->|Yes| Q4{Need Domain<br/>Knowledge?}
    Q4 -->|No| CONSIDER
    Q4 -->|Yes| OPERATOR[Use Operator]
    
    style OPERATOR fill:#90EE90
    style HELM fill:#FFE4B5

Use Operators When:

  • Application has complex lifecycle (backup, restore, scaling)
  • Need continuous management and monitoring
  • Stateful applications (databases, message queues)
  • Domain-specific knowledge required
  • Want declarative management of operational tasks

Use Helm When:

  • Simple application deployment
  • One-time setup sufficient
  • No ongoing operational complexity

Real-World Operator Examples

Prometheus Operator

Manages Prometheus monitoring stack:

  • Deploys Prometheus servers
  • Configures service discovery
  • Manages alerting rules
  • Handles storage

PostgreSQL Operator

Manages PostgreSQL databases:

  • Creates database clusters
  • Handles backups
  • Manages replication
  • Performs upgrades

Elasticsearch Operator

Manages Elasticsearch clusters:

  • Deploys cluster nodes
  • Manages sharding
  • Handles scaling
  • Manages indices

Operator Architecture

An operator consists of:

graph TB
    subgraph "Operator Components"
        CRD[CRD Definition]
        CONTROLLER[Controller Code]
        RBAC[RBAC Rules]
    end
    
    subgraph "Kubernetes"
        API[API Server]
        ETCD[(etcd)]
        RESOURCES[Resources]
    end
    
    CRD --> API
    CONTROLLER -->|Watches| API
    CONTROLLER -->|Creates| RESOURCES
    CONTROLLER -->|Updates| CRD
    API --> ETCD
    
    style CONTROLLER fill:#FFB6C1
    style CRD fill:#90EE90
  1. CRD: Defines your Custom Resource (from Lesson 1.4)
  2. Controller: Reconciliation logic (from Lesson 1.3)
  3. RBAC: Permissions for the operator

Operator Workflow

Here’s how an operator works:

sequenceDiagram
    participant User
    participant API as API Server
    participant Operator as Operator
    participant Cluster as Cluster Resources
    
    User->>API: Create CustomResource
    API->>Operator: Watch Event: ADD
    Operator->>API: Read CustomResource spec
    Operator->>Operator: Determine desired state
    Operator->>API: Check current resources
    API-->>Operator: Current state
    Operator->>Operator: Compare desired vs actual
    Operator->>API: Create/Update resources
    API->>Cluster: Apply changes
    Operator->>API: Update CustomResource status
    API-->>User: Status updated
    
    Note over Operator,Cluster: Continuous reconciliation loop

This is the same reconciliation pattern from Lesson 1.3, but applied to your Custom Resources!

Key Takeaways

  • Operators are controllers that manage Custom Resources
  • Operators encode domain knowledge about application management
  • Follow the same reconciliation pattern as built-in controllers
  • Use operators for complex, stateful applications that need ongoing management
  • Operators provide declarative management of operational tasks

Understanding for Building Operators

When building operators:

  • You’ll create CRDs (like in Lesson 1.4)
  • You’ll implement controllers (using the pattern from Lesson 1.3)
  • You’ll encode operational knowledge in code
  • You’ll reconcile desired vs actual state continuously

References

Official Documentation

Further Reading

  • Kubernetes Operators by Jason Dobies and Joshua Wood - Complete guide to operators
  • Kubernetes: Up and Running by Kelsey Hightower, Brendan Burns, and Joe Beda - Chapter 16: Operators
  • Operator Framework - Tools and best practices

Next Steps

Now that you understand what operators are, let’s learn about Kubebuilder - the framework that makes building operators easier.