apply

Apply the configuration in pod.json to a pod

  1. kubectl apply -f ./pod.json

Apply resources from a directory containing kustomization.yaml - e.g. dir/kustomization.yaml

  1. kubectl apply -k dir/

Apply the JSON passed into stdin to a pod

  1. cat pod.json | kubectl apply -f -

Note: —prune is still in Alpha # Apply the configuration in manifest.yaml that matches label app=nginx and delete all other resources that are not in the file and match label app=nginx

  1. kubectl apply --prune -f manifest.yaml -l app=nginx

Apply the configuration in manifest.yaml and delete all the other config maps that are not in the file

  1. kubectl apply --prune -f manifest.yaml --all --prune-whitelist=core/v1/ConfigMap

Apply a configuration to a resource by file name or stdin. The resource name must be specified. This resource will be created if it doesn’t exist yet. To use ‘apply’, always create the resource initially with either ‘apply’ or ‘create —save-config’.

JSON and YAML formats are accepted.

Alpha Disclaimer: the —prune functionality is not yet complete. Do not use unless you are aware of what the current state is. See https://issues.k8s.io/34274.

Usage

$ kubectl apply (-f FILENAME | -k DIRECTORY)

Flags

NameShorthandDefaultUsage
allfalseSelect all resources in the namespace of the specified resource types.
allow-missing-template-keystrueIf true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
cascadebackgroundMust be “background”, “orphan”, or “foreground”. Selects the deletion cascading strategy for the dependents (e.g. Pods created by a ReplicationController). Defaults to background.
dry-runnoneMust be “none”, “server”, or “client”. If client strategy, only print the object that would be sent, without sending it. If server strategy, submit server-side request without persisting the resource.
field-managerkubectl-client-side-applyName of the manager used to track field ownership.
filenamef[]that contains the configuration to apply
forcefalseIf true, immediately remove resources from API and bypass graceful deletion. Note that immediate deletion of some resources may result in inconsistency or data loss and requires confirmation.
force-conflictsfalseIf true, server-side apply will force the changes against conflicts.
grace-period-1Period of time in seconds given to the resource to terminate gracefully. Ignored if negative. Set to 1 for immediate shutdown. Can only be set to 0 when —force is true (force deletion).
kustomizekProcess a kustomization directory. This flag can’t be used together with -f or -R.
openapi-patchtrueIf true, use openapi to calculate diff when the openapi presents and the resource can be found in the openapi spec. Otherwise, fall back to use baked-in types.
outputoOutput format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
overwritetrueAutomatically resolve conflicts between the modified and live configuration by using values from the modified configuration
prunefalseAutomatically delete resource objects, including the uninitialized ones, that do not appear in the configs and are created by either apply or create —save-config. Should be used with either -l or —all.
prune-whitelist[]Overwrite the default whitelist with <group/version/kind> for —prune
recordfalseRecord current kubectl command in the resource annotation. If set to false, do not record the command. If set to true, record the command. If not set, default to updating the existing annotation value only if one already exists.
recursiveRfalseProcess the directory used in -f, —filename recursively. Useful when you want to manage related manifests organized within the same directory.
selectorlSelector (label query) to filter on, supports ‘=’, ‘==’, and ‘!=’.(e.g. -l key1=value1,key2=value2)
server-sidefalseIf true, apply runs in the server instead of the client.
show-managed-fieldsfalseIf true, keep the managedFields when printing objects in JSON or YAML format.
templateTemplate string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
timeout0sThe length of time to wait before giving up on a delete, zero means determine a timeout from the size of the object
validatetrueIf true, use a schema to validate the input before sending it
waitfalseIf true, wait for resources to be gone before returning. This waits for finalizers.

edit-last-applied

Edit the last-applied-configuration annotations by type/name in YAML

  1. kubectl apply edit-last-applied deployment/nginx

Edit the last-applied-configuration annotations by file in JSON

  1. kubectl apply edit-last-applied -f deploy.yaml -o json

Edit the latest last-applied-configuration annotations of resources from the default editor.

The edit-last-applied command allows you to directly edit any API resource you can retrieve via the command-line tools. It will open the editor defined by your KUBE_EDITOR, or EDITOR environment variables, or fall back to ‘vi’ for Linux or ‘notepad’ for Windows. You can edit multiple objects, although changes are applied one at a time. The command accepts file names as well as command-line arguments, although the files you point to must be previously saved versions of resources.

The default format is YAML. To edit in JSON, specify “-o json”.

The flag —windows-line-endings can be used to force Windows line endings, otherwise the default for your operating system will be used.

In the event an error occurs while updating, a temporary file will be created on disk that contains your unapplied changes. The most common error when updating a resource is another editor changing the resource on the server. When this occurs, you will have to apply your changes to the newer version of the resource, or update your temporary saved copy to include the latest resource version.

Usage

$ kubectl apply edit-last-applied (RESOURCE/NAME | -f FILENAME)

Flags

NameShorthandDefaultUsage
allow-missing-template-keystrueIf true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
field-managerkubectl-client-side-applyName of the manager used to track field ownership.
filenamef[]Filename, directory, or URL to files to use to edit the resource
kustomizekProcess the kustomization directory. This flag can’t be used together with -f or -R.
outputoOutput format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
recordfalseRecord current kubectl command in the resource annotation. If set to false, do not record the command. If set to true, record the command. If not set, default to updating the existing annotation value only if one already exists.
recursiveRfalseProcess the directory used in -f, —filename recursively. Useful when you want to manage related manifests organized within the same directory.
show-managed-fieldsfalseIf true, keep the managedFields when printing objects in JSON or YAML format.
templateTemplate string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
windows-line-endingsfalseDefaults to the line ending native to your platform.

set-last-applied

Set the last-applied-configuration of a resource to match the contents of a file

  1. kubectl apply set-last-applied -f deploy.yaml

Execute set-last-applied against each configuration file in a directory

  1. kubectl apply set-last-applied -f path/

Set the last-applied-configuration of a resource to match the contents of a file; will create the annotation if it does not already exist

  1. kubectl apply set-last-applied -f deploy.yaml --create-annotation=true

Set the latest last-applied-configuration annotations by setting it to match the contents of a file. This results in the last-applied-configuration being updated as though ‘kubectl apply -f ‘ was run, without updating any other parts of the object.

Usage

$ kubectl apply set-last-applied -f FILENAME

Flags

NameShorthandDefaultUsage
allow-missing-template-keystrueIf true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
create-annotationfalseWill create ‘last-applied-configuration’ annotations if current objects doesn’t have one
dry-runnoneMust be “none”, “server”, or “client”. If client strategy, only print the object that would be sent, without sending it. If server strategy, submit server-side request without persisting the resource.
filenamef[]Filename, directory, or URL to files that contains the last-applied-configuration annotations
outputoOutput format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
show-managed-fieldsfalseIf true, keep the managedFields when printing objects in JSON or YAML format.
templateTemplate string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].

view-last-applied

View the last-applied-configuration annotations by type/name in YAML

  1. kubectl apply view-last-applied deployment/nginx

View the last-applied-configuration annotations by file in JSON

  1. kubectl apply view-last-applied -f deploy.yaml -o json

View the latest last-applied-configuration annotations by type/name or file.

The default output will be printed to stdout in YAML format. You can use the -o option to change the output format.

Usage

$ kubectl apply view-last-applied (TYPE [NAME | -l label] | TYPE/NAME | -f FILENAME)

Flags

NameShorthandDefaultUsage
allfalseSelect all resources in the namespace of the specified resource types
filenamef[]Filename, directory, or URL to files that contains the last-applied-configuration annotations
kustomizekProcess the kustomization directory. This flag can’t be used together with -f or -R.
outputoyamlOutput format. Must be one of yaml|json
recursiveRfalseProcess the directory used in -f, —filename recursively. Useful when you want to manage related manifests organized within the same directory.
selectorlSelector (label query) to filter on, supports ‘=’, ‘==’, and ‘!=’.(e.g. -l key1=value1,key2=value2)