Kubenetes POD delete with Pattern Match or Wilcard

pauldx picture pauldx · Dec 24, 2019 · Viewed 8.5k times · Source

When I am using below it deletes the running POD after matching the pattern from commandline:

kubectl get pods -n bi-dev --no-headers=true | awk '/group-react/{print $1}' | xargs kubectl delete -n bi-dev pod

However when I am using this command as an alias in .bash_profile it doesn't execute . This is how I defined it :

 alias kdpgroup="kubectl get pods -n bi-dev --no-headers=true | awk '/group-react/{print $1}'|  kubectl delete -n bi-dev pod"

when execute this as below I get below error in commandline:

~ $ kdpgroup
error: resource(s) were provided, but no name, label selector, or --all flag specified

When I define this in .bash_profile I get this :

~ $ . ./.bash_profile
-bash: alias: }| xargs  kubectl delete -n bi-dev pod: not found
~ $

Am I missing something to delete POD using Pattern Match or with Wilcard ?

thanks

Answer

Jonas picture Jonas · Dec 27, 2019

Am I missing something to delete POD using Pattern Match or with Wilcard?

When using Kubernetes it is more common to use labels and selectors. E.g. if you deployed an application, you usually set a label on the pods e.g. app=my-app and you can then get the pods with e.g. kubectl get pods -l app=my-app.

Using this aproach, it is easier to delete the pods you are interested in, with e.g.

kubectl delete pods -l app=my-app

or with namespaces

kubectl delete pods -l app=my-app -n default

See more on Kubernetes Labels and Selectors

Set-based selector

I have some pod's running in the name of "superset-react" and "superset-graphql" and I want to search my wildcard superset and delete both of them in one command

I suggest that those pods has labels app=something-react and app=something-graphql. If you want to classify those apps, e.g. if your "superset" varies, you could add a label app-type=react and app-type=graphql to all those type of apps.

Then you can delete pods for both app types with this command:

kubectl delete pods -l 'app-type in (react, graphql)'