Home > front end >  Is kubectl apply safe enough to update all pods no matter how they were created?
Is kubectl apply safe enough to update all pods no matter how they were created?

Time:02-23

A pod can be created by Deployment or ReplicaSet or DaemonSet, if I am updating a pod's container specs, is it OK for me to simply modify the yaml file that created the pod? Would it be erroneous once I have done that?

Brief Question: Is kubectl apply -f xxx.yml the silver bullet for all pod update?

CodePudding user response:

...if I am updating a pod's container specs, is it OK for me to simply modify the yaml file that created the pod?

The fact that the pod spec is part of the controller spec (eg. deployment, daemonset), to update the container spec you naturally start with the controller spec. Also, a running pod is largely immutable, there isn't much you can change directly unless you do a replace - which is what the controller already doing.

CodePudding user response:

you should not make changes to the pods directly, but update the spec.template.spec section of the deployment used to create the pods.

reason for this is that the deployment is the controller that manages the replicasets and therefore the pods that are created for your application. that means if you apply changes to the pods manifest directly, and something like a pod rescheduling/restart happens, the changes made to the pod will be lost because the replicaset will recreate the pod according to its own specification and not the specification of the last running pod.

you are safe to use kubectl apply to apply changes to existing resources but if you are unsure, you can always extract the current state of the deployment from kubernetes and pipe that output into a yaml file to create a backup:

kubectl get deploy/<name> --namespace <namespace> -o yaml > deploy.yaml

another option is to use the internal rollback mechanism of kubernetes to restore a previous revision of your deployment. see https://learnk8s.io/kubernetes-rollbacks for more infos on that.

  • Related