Skip to content

Kubernetes Operator pattern

The Kubernetes Operator pattern is a design approach used to manage complex or "stateful" applications on Kubernetes. It extends the capabilities of the Kubernetes Control Plane by introducing custom resources and controllers tailored to specific applications[原理及源码解析__Kubernetes基本概念.md][原理及源码解析__Kubernetes基本概念.md]^[原理及源码解析__Kubernetes基本概念.md].

Rather than treating the application as a generic set of containers, the Operator acts as a domain-specific controller that encodes the operational knowledge required to deploy, scale, and manage the application lifecycle^[原理及源码解析__Kubernetes基本概念.md].

Core Components

The pattern relies on two key Kubernetes features working in tandem:

Custom Resource Definition (CRD)

The Operator pattern uses Custom Resource Definitions (CRDs) to define new types of objects that represent the application. For example, an Etcd Operator introduces a custom resource called EtcdCluster^[原理及源码解析__Kubernetes基本概念.md]. Once defined, users can create instances of this custom resource (e.g., a 3-node Etcd cluster) using standard YAML configurations^[原理及源码解析__Kubernetes基本概念.md].

Custom Controller

The Operator itself acts as a custom controller. It watches the API server for changes to the custom resources created by the CRD^[原理及源码解析__Kubernetes基本概念.md]. When a user submits a YAML file describing a desired state (e.g., creating a cluster), the controller translates that definition into specific actions—such as creating Pods, Services, or PVCs—to realize the state^[原理及源码解析__Kubernetes基本概念.md].

How It Works

The workflow of an Operator typically follows this loop^[原理及源码解析__Kubernetes基本概念.md]:

  1. Deployment: The operator is deployed to the cluster, usually as a Deployment, often with specific RBAC permissions to manage the necessary resources^[原理及源码解析__Kubernetes基本概念.md].
  2. Registration: Once running, the operator automatically registers a CRD with the Kubernetes API server, allowing the cluster to recognize new kinds of objects^[原理及源码解析__Kubernetes基本概念.md].
  3. User Intent: A user creates a custom resource instance (e.g., an EtcdCluster YAML) to request an application instance^[原理及源码解析__Kubernetes基本概念.md].
  4. Reconciliation: The operator controller detects the new object and creates the underlying infrastructure (Pods, Services, etc.) required to run the application^[原理及源码解析__Kubernetes基本概念.md].

Use Case: Stateful Applications

The primary motivation for the Operator pattern is to handle the complexities of stateful applications^[原理及源码解析__Kubernetes基本概念.md]. While standard Kubernetes controllers (like Deployment) are excellent for stateless services, stateful applications often require intricate setup and recovery procedures (e.g., leader election, database bootstrapping, backups). The Operator pattern provides a "flexible and programming-friendly" way to inject this specific logic into the Kubernetes control loop^[原理及源码解析__Kubernetes基本概念.md].

  • Kubernetes
  • [[Custom Resource Definition (CRD)]]
  • [[Controller Pattern]]

Sources

  • 原理及源码解析__Kubernetes基本概念.md