Kubernetes resource lifecycle (create vs apply)¶
In Kubernetes, the kubectl command-line tool provides two primary methods for resource creation and management: create and apply.^[400-devops-06-kubernetes-k8s-ithelp-day6-readme.md]
kubectl create¶
The kubectl create command is designed to initialize resources that do not currently exist in the cluster^[400-devops-06-kubernetes-k8s-ithelp-day6-readme.md]. It functions strictly as an imperative command to generate new objects.
A significant limitation of this command is that it can only be used to create resources that do not yet exist^[400-devops-06-kubernetes-k8s-ithelp-day6-readme.md]. If a resource with the same name already exists, the create command will fail or result in an error, as it lacks the logic to handle modifications to existing active resources.
kubectl apply¶
The kubectl apply command offers a more flexible approach to resource management. It functions as a declarative operation that manages the desired state of a resource^[400-devops-06-kubernetes-k8s-ithelp-day6-readme.md].
Unlike create, apply is capable of handling resources that are already running in the cluster^[400-devops-06-kubernetes-k8s-ithelp-day6-readme.md]. When executed, it checks the current configuration against the existing resource; if the resource is present, it analyzes the specifications for differences and updates the resource to match the new configuration^[400-devops-06-kubernetes-k8s-ithelp-day6-readme.md]. This makes apply the preferred method for managing changes and maintaining the lifecycle of resources throughout their existence.
Related Concepts¶
- Kubernetes Pod
- [[YAML configuration]]
- [[Declarative vs Imperative]]
Sources¶
^[400-devops-06-kubernetes-k8s-ithelp-day6-readme.md]