How to use the command 'kubectl apply' (with examples)
The command ‘kubectl apply’ is used to manage applications through files defining Kubernetes resources. It is used to create and update resources in a cluster.
Use case 1: Apply a configuration to a resource by file name or stdin
Code:
kubectl apply -f resource_filename
Motivation: This use case allows us to apply a configuration file to a resource in a Kubernetes cluster. It is useful when we want to create or update resources based on a configuration file.
Explanation:
kubectl apply
: Command to manage applications through files defining Kubernetes resources.-f resource_filename
: Specifies the configuration file to be applied to the resource.
Example Output:
deployment.apps/my-deployment created
Use case 2: Edit the latest last-applied-configuration annotations of resources from the default editor
Code:
kubectl apply edit-last-applied -f resource_filename
Motivation: This use case allows us to edit the last-applied-configuration annotations of a resource using the default editor. It is useful when we need to make changes to the existing configuration of a resource.
Explanation:
kubectl apply
: Command to manage applications through files defining Kubernetes resources.edit-last-applied
: Subcommand to edit the latest last-applied-configuration annotations of resources.-f resource_filename
: Specifies the configuration file whose annotations need to be edited.
Example Output: The default editor opens with the last-applied-configuration annotations of the specified resource file, allowing us to make changes.
Use case 3: Set the latest last-applied-configuration annotations by setting it to match the contents of a file
Code:
kubectl apply set-last-applied -f resource_filename
Motivation: This use case allows us to set the last-applied-configuration annotations of a resource to match the contents of a file. It is useful when we want to update the annotations based on a specific configuration.
Explanation:
kubectl apply
: Command to manage applications through files defining Kubernetes resources.set-last-applied
: Subcommand to set the latest last-applied-configuration annotations of resources.-f resource_filename
: Specifies the configuration file whose contents will be used to set the annotations.
Example Output:
annotation.kubectl.kubernetes.io/last-applied-configuration configured
Use case 4: View the latest last-applied-configuration annotations by type/name or file
Code:
kubectl apply view-last-applied -f resource_filename
Motivation: This use case allows us to view the latest last-applied-configuration annotations of a resource by type/name or file. It is useful when we want to inspect the current configuration of a resource.
Explanation:
kubectl apply
: Command to manage applications through files defining Kubernetes resources.view-last-applied
: Subcommand to view the latest last-applied-configuration annotations of resources.-f resource_filename
: Specifies the configuration file or type/name of the resource to view.
Example Output:
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"Service"...}
Conclusion:
The ‘kubectl apply’ command is a powerful tool for managing Kubernetes resources. It allows us to create, update, edit, set, and view the configuration of resources in a cluster. By using different subcommands and arguments, we can perform a variety of operations on resources efficiently.