Skip to content

Declarative vs imperative Kubernetes management

Kubernetes management generally falls into two primary approaches: imperative and declarative.^[03.k8s集群.md]

Imperative Management

Imperative management involves instructing Kubernetes on exactly how to perform an action, step-by-step, using direct commands.^[03.k8s集群.md]

Common characteristics include: * Operational verbs: Commands typically use action-oriented verbs like create, delete, scale, or edit.^[03.k8s集群.md] * Direct interaction: Users interact directly with the cluster via the command line to alter state. * Live editing: Changes can be applied to running resources "online" using commands like kubectl edit, which opens the live configuration in a text editor.^[03.k8s集群.md] * Ad-hoc tasks: This method is often used for immediate, one-off operations such as entering a container with kubectl exec or quickly exposing a port with kubectl expose.^[03.k8s集群.md]

Declarative Management

Declarative management focuses on defining the desired state of the system, typically using configuration files (YAML), and letting the cluster handle the implementation details.^[03.k8s集群.md]

Common characteristics include: * Configuration files: Relies on resource manifests (e.g., .yaml files) that define the specification.^[03.k8s集群.md] * Idempotency: The kubectl apply command is typically used. Rather than creating a new resource from scratch every time, it calculates the difference between the local file and the live object to achieve the desired state.^[03.k8s集群.md] * Version control friendly: Because the infrastructure is defined as code, it integrates well with version control systems ("recorded offline").^[03.k8s集群.md]

Sources

^[03.k8s集群.md]