How to Use the Command 'kubectl apply' (with Examples)
The kubectl apply
command is an integral part of managing applications in Kubernetes. This command works by applying a configuration to a resource specified either by a file or standard input, enabling the orchestration tool to create or update resources within a Kubernetes cluster. It plays a pivotal role in managing application lifecycle within a Kubernetes environment through declarative management. It allows users to specify the desired state of applications and services, and Kubernetes ensures that the current state matches the desired state by creating or modifying resources as needed.
Apply a configuration to a resource by file name or stdin
Code:
kubectl apply -f resource_filename
Motivation:
The foundational use case for kubectl apply
is to create or update resources using a configuration file. This is especially useful during the initial deployment of applications on Kubernetes or when rolling out updates to existing applications. By applying configurations from a file, users can manage resources declaratively, letting Kubernetes handle the specifics of getting to the desired state.
Explanation:
kubectl apply
: The command begins withkubectl apply
, indicating that the resources will be created or updated.-f
: This flag is used to specify the filename path that contains the resource configuration.resource_filename
: This represents the actual YAML or JSON file that describes the desired state of the resource(s) on Kubernetes.
Example Output:
deployment.apps/my-deployment created
service/my-service unchanged
Edit the latest last-applied-configuration annotations of resources from the default editor
Code:
kubectl apply edit-last-applied -f resource_filename
Motivation:
This example enables users to directly edit the last-applied-configuration annotations of resources using their default text editor. It is beneficial when a user needs to make changes to the configuration that was last applied to ensure that future apply operations use the updated configuration.
Explanation:
kubectl apply edit-last-applied
: Initiates the edit mode for the last-applied configuration annotations.-f
: Indicates that a file will be provided as input.resource_filename
: Refers to the specific YAML or JSON configuration file for the resource whose last-applied-configuration annotations need editing.
Example Output:
Upon execution, this command will open the file in the default editor set in your environment. For example, it might open something like:
"resource_filename.yaml" ... (file content displayed in the editor)
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 command is designed for users who want to update the last-applied-configuration annotations to reflect the current configuration file’s content. This can be crucial when ensuring that the Kubernetes configuration is in sync with the source of truth file, which can help resolve conflicts in configuration states.
Explanation:
kubectl apply set-last-applied
: This aspect of the command applies changes to update annotations with the current file contents.-f
: A file parameter flag that passes the filename to the command.resource_filename
: This is the specified file containing the updated resource configuration that will be set as the last-applied configuration.
Example Output:
configmap/my-config configured
View the latest last-applied-configuration annotations by type/name or file
Code:
kubectl apply view-last-applied -f resource_filename
Motivation:
The need to view the last-applied-configuration annotations arises in numerous scenarios, such as troubleshooting discrepancies between intended and actual configurations of a Kubernetes resource. This command gives users a way to verify what configuration Kubernetes thinks was last applied.
Explanation:
kubectl apply view-last-applied
: This phrase in the command is used to output the last-applied-configuration annotations.-f
: Indicates the path to the configuration file whose last applied settings will be displayed.resource_filename
: The specific file for which the last-applied configurations need to be viewed.
Example Output:
{
"apiVersion": "v1",
"kind": "ConfigMap",
...
}
Conclusion:
The kubectl apply
command is versatile and fundamental to the management of Kubernetes resources using declarative configurations. Its ability to create, update, edit, set, and view configurations ensures that applications and services within a Kubernetes cluster remain consistent with the desired state. Mastery of this command empowers users to effectively orchestrate and manipulate the components within a Kubernetes ecosystem.