Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubenetes POD delete with Pattern Match or Wilcard

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

like image 642
pauldx Avatar asked Dec 24 '19 22:12

pauldx


People also ask

How do I delete a Kubernetes pod?

Destroy Pod The action of deleting the pod is simple. To delete the pod you have created, just run kubectl delete pod nginx . Be sure to confirm the name of the pod you want to delete before pressing Enter. If you have completed the task of deleting the pod successfully, pod nginx deleted will appear in the terminal.

How do I delete Crashloopbackoff pod?

type=OnDelete . In case of node failure, the pod will recreated on new node after few time, the old pod will be removed after full recovery of broken node. worth noting it is not going to happen if your pod was created by DaemonSet or StatefulSet .


2 Answers

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)' 
like image 62
Jonas Avatar answered Sep 20 '22 12:09

Jonas


As the question asks, this is about using a wild card. Let me give examples on using wild cards to delete pods.

Delete Pods which contain the word "application"

Replace <namespace> with the namespace you want to delete pods from.

kubectl get pods -n <namespace> --no-headers=true | awk '/application/{print $1}'| xargs  kubectl delete -n <namespace> pod 

This will give a response like the following. It will print out the deleted pods.

pod "sre-application-7fb4f5bff9-8crgx" deleted pod "sre-application-7fb4f5bff9-ftzfd" deleted pod "sre-application-7fb4f5bff9-rrkt2" deleted 

Delete Pods which contain "application" or "service"

Replace <namespace> with the namespace you want to delete pods from.

kubectl get pods -n <namespace> --no-headers=true | awk '/application|service/{print $1}'| xargs  kubectl delete -n <namespace> pod 

This will give a response like the following. It will print out the deleted pods.

pod "sre-application-7fb4f5bff9-8crgx" deleted pod "sre-application-7fb4f5bff9-ftzfd" deleted pod "sre-service-7fb4f5bff9-rrkt2" deleted 
like image 45
Keet Sugathadasa Avatar answered Sep 16 '22 12:09

Keet Sugathadasa