Home > Net >  Is there a convinient way to pipe multiple kubectl commands to each other?
Is there a convinient way to pipe multiple kubectl commands to each other?

Time:01-14

I am looking for a way to do something like that

kubectl get pods -l app=foo | kubectl delete

I think this would be a great way to deal with multiple ressources at once, but I cannot find a reasonably easy way to do this.

CodePudding user response:

You may use command substitution:

Kubectl delete $(kubectl get pod -l app=foo -o name)

CodePudding user response:

kubectl get -o name will write out resource names in kind/name format, one to a line. You can use this in combination with tools like xargs(1) to run pipelines like you suggest.

kubectl get job -l app=foo -o name | xargs kubectl delete

# help we're using the long-format label names and I don't remember
# what goes after `kubectl get -l`
kubectl get job -o name | grep foo | xargs kubectl delete

For the very specific command you show, I've often found it easier to use kubectl rollout restart to trigger a Deployment's redeployment sequence without actually making any changes. This will delete all of the pods managed by the Deployment, but only after creating new pods first; so you get the effect of restarting misbehaving Pods but without actually taking the whole application down.

kubectl rollout restart deployment/foo
  •  Tags:  
  • Related