'kubectl delete pod with deployment name

I often sometimes my pods for debugging with kubectl delete pod, so it updates the container image. But every time I have to lookup the complete name of the pod:

kubectl delete pod deployment_name-56fccbbfb8g4rj6

How can I just delete the pod without knowing the hash?



Solution 1:[1]

Depending on your shell, you can use auto completion to have your pod name suggested by the system.

  • Bash-completion is provided by many package managers (see here). You can install it with apt-get install bash-completion or yum install bash-completion, etc.

  • For Zsh this can be generated with the command kubectl completion zsh. Sourcing the completion script in your shell enables kubectl autocompletion.

    To do so in all your shell sessions, add the following to your ~/.zshrc file:

    source <(kubectl completion zsh)
    
    

Another thing is that you don't need to destroy your pod to update your image. You can simply use kubectl patch or kubectl set image.

For more reading please check Kubernetes document about patching the API objects with kubectl.

Solution 2:[2]

  • delete pods / namesapces

kubectl get pod --all-namespaces | grep -viE "compl|running" | awk 'NR>1{print $2 " -n " $1}' |while read line;do kubectl delete pod $line;done

Solution 3:[3]

that's not possible but you can delete all of them at once using kubectl here:

kubectl delete --all pods --namespace=foo

if you want to delete a single pod you have to do it manually. else try this solution.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 acid_fuji
Solution 2 wbing
Solution 3 Eddwin Paz