Kubectl Apply Command Usage in Kubernetes

Itacen Sabacok | Sep 25, 2022

Apply a configuration to a resource by file name or stdin. This resource will be created if it doesn’t exist yet.

  • The resource name must be specified.
  • JSON and YAML formats are accepted
 1# syntax
 2kubectl apply (-f FILENAME | -k DIRECTORY)
 3
 4# Apply the configuration in pod.json to a pod
 5kubectl apply -f ./pod.json        
 6
 7# Apply the JSON passed into stdin to a pod
 8cat pod.json | kubectl apply -f -  
 9
10# Apply all files with wildcard characters 
11kubectl apply -f '*.json'          
12
13# Apply resources from a directory containing kustomization.yaml
14kubectl apply -k dir/              

edit-last-applied

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

1# syntax
2kubectl apply edit-last-applied (RESOURCE/NAME | -f FILENAME)
3
4# Edit the last-applied-configuration annotations by type/name in YAML
5kubectl apply edit-last-applied deployment/nginx
6
7# Edit the last-applied-configuration annotations by file in JSON
8kubectl apply edit-last-applied -f deploy.yaml -o json

set-last-applied

Set the latest last-applied-configuration annotations by setting it to match the contents of a file.

 1# syntax
 2kubectl apply set-last-applied -f FILENAME
 3
 4# Set the last-applied-configuration of a resource to match the contents of a file
 5kubectl apply set-last-applied -f deploy.yaml
 6
 7# Execute set-last-applied against each configuration file in a directory
 8kubectl apply set-last-applied -f path/
 9
10# it will create the annotation if it does not already exist
11kubectl apply set-last-applied -f deploy.yaml --create-annotation=true

view-last-applied

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

1# syntax
2kubectl apply view-last-applied (TYPE [NAME | -l label] | TYPE/NAME | -f FILENAME)
3
4# View the last-applied-configuration annotations by type/name in YAML
5kubectl apply view-last-applied deployment/nginx
6
7# View the last-applied-configuration annotations by file in JSON
8kubectl apply view-last-applied -f deploy.yaml -o json