Kubernetes Object Management

    Warning: A Kubernetes object should be managed using only one technique. Mixing and matching techniques for the same object results in undefined behavior.

    When using imperative commands, a user operates directly on live objects in a cluster. The user provides operations to the kubectl command as arguments or flags.

    This is the recommended way to get started or to run a one-off task in a cluster. Because this technique operates directly on live objects, it provides no history of previous configurations.

    Run an instance of the nginx container by creating a Deployment object:

    Trade-offs

    Advantages compared to object configuration:

    • Commands are expressed as a single action word.
    • Commands require only a single step to make changes to the cluster.

    Disadvantages compared to object configuration:

    • Commands do not provide an audit trail associated with changes.
    • Commands do not provide a source of records except for what is live.
    • Commands do not provide a template for creating new objects.

    See the for more details on object definitions.

    Warning: The imperative replace command replaces the existing spec with the newly provided one, dropping all changes to the object missing from the configuration file. This approach should not be used with resource types whose specs are updated independently of the configuration file. Services of type LoadBalancer, for example, have their field updated independently from the configuration by the cluster.

    Create the objects defined in a configuration file:

    1. kubectl create -f nginx.yaml

    Delete the objects defined in two configuration files:

    Update the objects defined in a configuration file by overwriting the live configuration:

    1. kubectl replace -f nginx.yaml

    Trade-offs

    Advantages compared to imperative commands:

    • Object configuration can be stored in a source control system such as Git.
    • Object configuration can integrate with processes such as reviewing changes before push and audit trails.
    • Object configuration provides a template for creating new objects.
    • Object configuration requires basic understanding of the object schema.
    • Object configuration requires the additional step of writing a YAML file.

    Advantages compared to declarative object configuration:

    • Imperative object configuration behavior is simpler and easier to understand.
    • As of Kubernetes version 1.5, imperative object configuration is more mature.

    Disadvantages compared to declarative object configuration:

    • Imperative object configuration works best on files, not directories.

    When using declarative object configuration, a user operates on object configuration files stored locally, however the user does not define the operations to be taken on the files. Create, update, and delete operations are automatically detected per-object by kubectl. This enables working on directories, where different operations might be needed for different objects.

    Note: Declarative object configuration retains changes made by other writers, even if the changes are not merged back to the object configuration file. This is possible by using the API operation to write only observed differences, instead of using the replace API operation to replace the entire object configuration.

    Process all object configuration files in the configs directory, and create or patch the live objects. You can first diff to see what changes are going to be made, and then apply:

    Recursively process directories:

    1. kubectl apply -R -f configs/

    Trade-offs

    • Changes made directly to live objects are retained, even if they are not merged back into the configuration files.
    • Declarative object configuration has better support for operating on directories and automatically detecting operation types (create, patch, delete) per-object.

    Disadvantages compared to imperative object configuration:

    • Declarative object configuration is harder to debug and understand results when they are unexpected.
    • Partial updates using diffs create complex merge and patch operations.