kubectl – Kubernetes apply to get to desired state

I feel like I have a terrible knowledge gap when it comes to managing the resource states within Kubernetes.

Suppose I have 2 deployments in my cluster, foo1 and foo2. They are both defined in separate yaml files, foo1.yaml and foo2.yaml that are both inside a my-dir directory and have been applied with kubectl apply -f my-dir/

Now I want to make a third deployment, but also delete my second deployment. I know that I can do this in 2 steps:

  • Make another foo3.yaml file inside the directory and then do kubectl apply -f my-dir/foo3.yaml
  • Run kubectl delete -f my-dir/foo2.yaml to get rid of the second deployment.

My question is, can I do this in one shot by keeping the “desired state” in my directory. i.e. Is there any way that I can delete foo2.yaml, create a new foo3.yaml and then just do kubectl apply -f my-dir/ to let kubernetes handle the deletion of the removed resource file as well? What am I missing here?

Read more here: Source link