S Andrew
S Andrew

Reputation: 7198

How to delete and recreate pods using yaml file in kubernetes

I have a yaml file which I can use to create pods. I am using the dashboard so I can simply select yaml file and it will create pods. Pod will start the container and container will run the docker image. So now lets say I have done some changes in the docker image and want to deploy it again. For this, I will delete the already running pod and will upload the yaml file.

Instead of deleting and uploading yaml file again, is there any keyword available which will delete the already running pod/deployment and will recreate it.

Thanks

Upvotes: 3

Views: 18849

Answers (5)

dj_donut
dj_donut

Reputation: 1

In my case, I needed to recreate a group of many pods, controlled by different deployments/statefulsets with restartPolicy: Always (For example, if data changes on volume and apps need to restart to take it in effect / pull updated image / etc)

  1. Assign label for pods. On deployment/statefulset/daemonset manifest add label at the .spec.template.metadata.labels.
  2. Apply/recreate resources (in any way convenient for you). Then check if the changes have been applied:
# kubectl get po -l "app_group=my_grp"
  1. Recreate pods by deleting them (according to their controllers restart policy .spec.template.spec.restartPolicy):
# kubectl delete po -l "app_group=my_grp"

Make sure that the old pods are terminating and new ones must be created instead:

# kubectl get po -w

But there's probably a better way.

Upvotes: 0

kanobius
kanobius

Reputation: 881

In my case, it happened after an Xcode project renaming. It was a project using cocoapods and the path to the project in the Podfile wasn't updated. I've found it after doing a binary search on git commits (i.e. checkout old commit, see if refactor works, move to a newer commit, see if refactor works, continue until finding the commit which breaks refactor, analyze changes to find the reason).

Upvotes: 0

Vivek Raj
Vivek Raj

Reputation: 519

kubectl replace --force -f podfile.yaml

Upvotes: 1

Breedly
Breedly

Reputation: 14226

I can't remember the StackOverflow question where I first saw this method, but here it is again:

kubectl --namespace thenamespace get pod thepod -o yaml | kubectl replace --save-config -f -

You can do that with all k8s resources.

Upvotes: 2

If you are using this for development you might get away with

containers:
- image: my/app:dev
  imagePullPolicy: Always

With this, whenever your pod is recreated, you will get fresh image version.

That said, you need to use something like Deployment to have a pod restarted automaticaly, and then you can just kubectl delete my-pod-xxxxx-yyy to wipe old one and in few sec get the fresh, current one.

For prod, don't do that please. Just use tagged images and apply changed image to your Deployment with kubectl apply -f my.yaml or preferably something like Helm (but that is more complicated topic for starters)

Upvotes: 3

Related Questions