kubectl delete
通過配置文件名、stdin、資源名稱或label選擇器來刪除資源。
支持JSON和YAML格式文件。可以只指定一種類型的參數:文件名、資源名稱或label選擇器。
有些資源,如pod,支持優雅的(graceful)刪除,因為這些資源一般是集群中的實體,所以刪除不可能會立即生效,這些資源在強制終止之前默認定義了一個周期(寬限期),但是你可以使用--grace-period flag來覆蓋該值,或者通過pass --now設置該周期為1。
如果托管Pod的Node節點已經停止或者無法連接API Server,使用delete命令刪除Pod需等待時間更長。要強制刪除資源,需指定- force flag,且設置周期(寬限期)為0。
如果執行強制刪除Pod,則調度程序會在節點釋放這些Pod之前將新的Pod放在這些節點上,並使之前Pod立即被逐出。
注意:執行delete命令時不會檢查資源版本,如果在執行delete操作時有人進行了更新操作,那么更新操作將連同資源一起被刪除。
語法
$ delete ([-f FILENAME] | TYPE [(NAME | -l label | --all)])
示例
使用 pod.json中指定的資源類型和名稱刪除pod。
kubectl delete -f ./pod.json
根據傳入stdin的JSON所指定的類型和名稱刪除pod。
cat pod.json | kubectl delete -f -
刪除名為“baz”和“foo”的Pod和Service。
kubectl delete pod,service baz foo
刪除 Label name = myLabel的pod和Service。
kubectl delete pods,services -l name=myLabel
強制刪除dead node上的pod
kubectl delete pod foo --grace-period=0 --force
刪除所有pod
kubectl delete pods --all
Flags
Name | Shorthand | Default | Usage |
---|---|---|---|
all | false | select all resources in the namespace of the specified resource types. | |
cascade | true | If true, cascade the deletion of the resources managed by this resource (e.g. Pods created by a ReplicationController). Default true. | |
filename | f | [] | Filename, directory, or URL to files containing the resource to delete. |
force | false | Immediate deletion of some resources may result in inconsistency or data loss and requires confirmation. | |
grace-period | -1 | Period of time in seconds given to the resource to terminate gracefully. Ignored if negative. | |
ignore-not-found | false | Treat "resource not found" as a successful delete. Defaults to "true" when --all is specified. | |
include-extended-apis | true | If true, include definitions of new APIs via calls to the API server. [default true] | |
now | false | If true, resources are signaled for immediate shutdown (same as --grace-period=1). | |
output | o | Output mode. Use "-o name" for shorter output (resource/name). | |
recursive | R | false | Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory. |
selector | l | Selector (label query) to filter on. | |
timeout | 0s | The length of time to wait before giving up on a delete, zero means determine a timeout from the size of the object |