K8S刪除pod的4種方法小結(jié)
通過名稱name刪除
#Kubernetes向進程發(fā)送一個SIGTERM信號并等待一定的秒數(shù)(默認為30),使其正常關(guān)閉。如果它沒有及時關(guān)閉,則通過SIGKILL終止該進程。因此,為了確保你的進程總是正常關(guān)閉,進程需要正確處理SIGTERM信號。 kubectl delete pod pod_name
通過標簽label刪除
kubectl delete pod -l nio4444=true
通過namespace刪除
#刪除整個命名空間(pod將會伴隨命名空間自動刪除) kubectl delete -ns namespace_name
通過namespace刪除
# 刪除命名空間中的所有pod,但保留命名空間 kubectl delete pod --all
附:強制刪除Kubernetes pods
為什么需要強制刪除pod?
有時,Pod在超時后會卡在一個無法到達的Node上,處于終止/未知狀態(tài)。
當用戶試圖在一個不可到達的節(jié)點上刪除Pod時,Pod也可能進入這些狀態(tài)。
在這些情況下,你可以強行刪除pods。
如果你想使用kubectl版本>=1.5強行刪除一個Pod,請執(zhí)行以下操作。
kubectl delete pods pod_name --grace-period=0 --force
如果你使用的是 kubectl <= 1.4 的任何版本,你應該省略 --force 選項并使用。
kubectl delete pods pod_name --grace-period=0
現(xiàn)在讓我們用上面的方法刪除 pod “pod-delete-demo” 。
root@kmaster-rj:~# kubectl delete pod pod-delete-demo --force --grace-period=0 --namespace=default warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely. pod "pod-delete-demo" force delete
注意:強制刪除pod不會等待來自kubelet的確認,即pod已被終止。
如果即使在這些命令之后,pod仍然停留在未知狀態(tài),請使用以下命令從集群中刪除pod。
kubectl patch pod pod-delete-demo -p '{"metadata":{"finalizers":null}}'
確認 pod 已被刪除:
root@kmaster-rj:~# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES my-dep-557548758d-d2pmd 1/1 Running 0 2d23h 172.16.213.194 kworker-rj2 <none> <none> my-dep-557548758d-gprnr 1/1 Running 0 2d23h 172.16.213.49 kworker-rj1 <none> <none>
總結(jié)
到此這篇關(guān)于K8S刪除pod的4種方法小結(jié)的文章就介紹到這了,更多相關(guān)K8S刪除pod內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Kubernetes?安裝flannel組件的過程(本地?kube-flannel.yml?文件)
文章介紹了如何在無法直接訪問raw.githubusercontent.com的情況下,通過科學上網(wǎng)或下載kube-flannel.yml文件源碼來部署Flannel網(wǎng)絡插件的方法,感興趣的朋友一起看看吧2025-03-03Kubernetes k8s configmap 容器技術(shù)解析
這篇文章主要為大家介紹了k8s configmap 容器技術(shù)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08