How do I force delete kubernetes pods?

2019-06-15 14:14发布

I have the following pods:

NAME                                                 READY     STATUS        RESTARTS   AGE
xxx-myactivities-79f49cdfb4-nwg22                      1/1       Terminating   0          10h
xxx-mysearch-55864b5c59-6bnwl                          1/1       Terminating   0          1d
xxx-mysearch-55864b5c59-rpn48                          1/1       Terminating   0          13h
xxx-mysearch-6ff9bbb7cb-9qgbb                          1/1       Terminating   0          3d

I am running the following code to forcefully delete those pods:

#
# Clean up dying pods
#
pods=$( kubectl get pods | grep -v Running | tail -n +2 | awk -F " " '{print $1}' )
for pod in $pods;
do
    kubectl delete pod $pod --force
done

Here is the output:

pod "xxx-myactivities-79f49cdfb4-nwg22" deleted
pod "xxx-mysearch-55864b5c59-6bnwl" deleted
pod "xxx-mysearch-55864b5c59-rpn48" deleted
pod "xxx-mysearch-6ff9bbb7cb-9qgbb" deleted

After cleaning up, those pods still hang around.

NAME                                                 READY     STATUS        RESTARTS   AGE
xxx-myactivities-79f49cdfb4-nwg22                      1/1       Terminating   0          10h
xxx-mysearch-55864b5c59-6bnwl                          1/1       Terminating   0          1d
xxx-mysearch-55864b5c59-rpn48                          1/1       Terminating   0          13h
xxx-mysearch-6ff9bbb7cb-9qgbb                          1/1       Terminating   0          3d

How do I clean up those pods?

标签: kubernetes
3条回答
聊天终结者
2楼-- · 2019-06-15 14:37

You need first discovering that deployments existed:

kubectl get deployments --all-namespaces
NAME                               READY     STATUS        RESTARTS   AGE
chetabahana-web-584b95d576-62ccj   1/1       Running       0          20m
tutorial-web-56fbccc56b-wbwjq      1/1       Running       0          1m

Delete the deployment <NAME>-xxxx like this:

kubectl delete deployment <NAME>

For example to delete tutorial-web-56fbccc56b-wbwjq do:

kubectl delete deployment tutorial

Then all corresponded pods of tutorial-xxxx will terminate by itself.

NAME                               READY     STATUS        RESTARTS   AGE
chetabahana-web-584b95d576-62ccj   1/1       Running       0          20m
tutorial-web-56fbccc56b-wbwjq      0/1       Terminating   0          1m
查看更多
姐就是有狂的资本
3楼-- · 2019-06-15 14:38

To delete all pods in terminating state with one command do:

for p in $(kubectl get pods | grep Terminating | awk '{print $1}'); do kubectl delete pod $p --grace-period=0 --force;done
查看更多
乱世女痞
4楼-- · 2019-06-15 14:55

You have these alternatives:

kubectl delete pod xxx --now 

Or

SSH into the node the stuck pod was scheduled on Running docker ps | grep {pod name} to get the Docker Container ID Running docker rm -f {container id}

Or

kubectl delete pod NAME --grace-period=0 --force
查看更多
登录 后发表回答