I am learning Kubernetes recently, and I am not very clear about the difference between "kubectl apply" and "kubectl replace". Is there any situation that we can only use one of them?
问题:
回答1:
From: https://github.com/kubernetes/website/blob/master/content/en/docs/concepts/cluster-administration/manage-deployment.md
Disruptive updates
In some cases, you may need to update resource fields that cannot be updated once initialized, or you may just want to make a recursive change immediately, such as to fix broken pods created by a Deployment. To change such fields, use
replace --force
, which deletes and re-creates the resource.
回答2:
The difference is that replace
first deletes the resources, then creates it from the file you give it; whereas apply
attempts to directly update, in the current live resource, only the attributes you give it in the file. See In-place updates and disruptive updates.
A consequence of that is that the file you use in an apply
can be an incomplete spec, ie only what you want to change; whereas with replace
, the spec must be complete.
So you could apply
a file that changes only an annotation, without specifying any other properties of the resource; but if you tried to use the same file with a replace
command, the command would fail, due to missing information.
Also, apply
only works on some properties of resources; if you need to update properties for which apply
doesn't apply (sorry for the pun!), you have to use replace
instead.