Apply

Applying Changes on Kubernetes Clusters

Apply

Motivation

Apply is a command that will update a Kubernetes cluster to match state defined locally in files.

kubectl apply
  • Fully declarative - don’t need to specify create or update - just manage files
  • Merges user owned state (e.g. Service selector) with state owned by the cluster (e.g. Service clusterIp)

Definitions

  • Resources: Objects in a cluster - e.g. Deployments, Services, etc.
  • Resource Config: Files declaring the desired state for Resources - e.g. deployment.yaml. Resources are created and updated using Apply with these files.

kubectl apply Creates and Updates Resources through local or remote files. This may be through either raw Resource Config or kustomization.yaml.

Usage

Though Apply can be run directly against Resource Config files or directories using -f, it is recommended to run Apply against a kustomization.yaml using -k. The kustomization.yaml allows users to define configuration that cuts across many Resources (e.g. namespace).

Users run Apply on directories containing kustomization.yaml files using -k or on raw ResourceConfig files using -f.

CRUD Operations

Creating Resources

Any Resources that do not exist and are declared in Resource Config when Apply is run will be Created.

Updating Resources

Any Resources that already exist and are declared in Resource Config when Apply is run may be Updated.

Added Fields

Any fields that have been added to the Resource Config will be set on the Resource.

Updated Fields

Any fields that contain different values for the fields specified locally in the Resource Config from what is in the Resource will be updated by merging the Resource Config into the live Resource. See merging for more details.

Deleted Fields

Fields that were in the Resource Config the last time Apply was run, will be deleted from the Resource, and return to their default values.

Unmanaged Fields

Fields that were not specified in the Resource Config but are set on the Resource will be left unmodified.

Deleting Resources

Declarative deletion of Resources does not yet exist in a usable form, but is under development.

Resource Creation Ordering

Certain Resource Types may be dependent on other Resource Types being created first. e.g. Namespaced Resources on the Namespaces, RoleBindings on Roles, CustomResources on the CRDs, etc.

When used with a kustomization.yaml, Apply sorts the Resources by Resource type to ensure Resources with these dependencies are created in the correct order.


Last modified October 31, 2020: docs render fix (cd35140)