kubectl delete

概要

依檔案名稱、標準輸入、資源與名稱,或依資源與標籤選擇器刪除資源。

接受 JSON 與 YAML 格式。只能指定一種引數類型:檔案名稱、資源與名稱,或資源與標籤選擇器。

某些資源 (例如 Pod) 支援正常刪除。這些資源定義強制終止前的預設期間 (寬限期),但您可以使用 --grace-period 旗標覆寫該值,或傳遞 --now 以將寬限期設定為 1。由於這些資源通常代表叢集中的實體,因此刪除可能不會立即確認。如果託管 Pod 的節點關閉或無法連線到 API 伺服器,則終止可能需要比寬限期長得多的時間。若要強制刪除資源,您必須指定 --force 旗標。注意:只有一部分資源支援正常刪除。在沒有支援的情況下,--grace-period 旗標會被忽略。

重要事項:強制刪除 Pod 不會等待確認 Pod 的程序已終止,這可能會讓這些程序繼續執行,直到節點偵測到刪除並完成正常刪除為止。如果您的程序使用共用儲存空間或與遠端 API 通訊,並依賴 Pod 的名稱來識別自己,則強制刪除這些 Pod 可能會導致多個程序在不同的機器上執行,並使用相同的識別身分,這可能會導致資料損毀或不一致。只有在您確定 Pod 已終止,或者您的應用程式可以容忍同時執行相同 Pod 的多個副本時,才強制刪除 Pod。此外,如果您強制刪除 Pod,排程器可能會在節點釋放這些資源之前將新 Pod 放置在這些節點上,並導致這些 Pod 立即被驅逐。

請注意,delete 指令不執行資源版本檢查,因此如果有人在您提交刪除時提交資源的更新,他們的更新將與資源的其餘部分一起遺失。

在 CustomResourceDefinition 刪除後,探索快取的失效可能需要長達 6 小時。如果您不想等待,您可能需要執行 "kubectl api-resources" 以重新整理探索快取。

kubectl delete ([-f FILENAME] | [-k DIRECTORY] | TYPE [(NAME | -l label | --all)])

範例

  # Delete a pod using the type and name specified in pod.json
  kubectl delete -f ./pod.json
  
  # Delete resources from a directory containing kustomization.yaml - e.g. dir/kustomization.yaml
  kubectl delete -k dir
  
  # Delete resources from all files that end with '.json'
  kubectl delete -f '*.json'
  
  # Delete a pod based on the type and name in the JSON passed into stdin
  cat pod.json | kubectl delete -f -
  
  # Delete pods and services with same names "baz" and "foo"
  kubectl delete pod,service baz foo
  
  # Delete pods and services with label name=myLabel
  kubectl delete pods,services -l name=myLabel
  
  # Delete a pod with minimal delay
  kubectl delete pod foo --now
  
  # Force delete a pod on a dead node
  kubectl delete pod foo --force
  
  # Delete all pods
  kubectl delete pods --all
  
  # Delete all pods only if the user confirms the deletion
  kubectl delete pods --all --interactive

選項

--all

刪除指定資源類型命名空間中的所有資源。

-A, --all-namespaces

如果存在,則列出所有命名空間中請求的物件。即使使用 --namespace 指定,目前的環境定義中的命名空間也會被忽略。

--cascade string[="background"]     預設值:"background"

必須為 "background"、"orphan" 或 "foreground"。選取從屬項的刪除串聯策略 (例如 ReplicationController 建立的 Pod)。預設為 background。

--dry-run string[="unchanged"]     預設值:"none"

必須為 "none"、"server" 或 "client"。如果是 client 策略,則只印出將傳送的物件,而不傳送它。如果是 server 策略,則提交伺服器端請求,而不持久儲存資源。

--field-selector string

要篩選的選擇器 (欄位查詢),支援 '='、'==' 和 '!='。(例如 --field-selector key1=value1,key2=value2)。伺服器每個類型僅支援有限數量的欄位查詢。

-f, --filename strings

包含要刪除的資源。

--force

如果為 true,則立即從 API 移除資源並略過正常刪除。請注意,立即刪除某些資源可能會導致不一致或資料遺失,並且需要確認。

--grace-period int     預設值:-1

給予資源正常終止的時間 (秒)。如果為負數則忽略。設定為 1 以立即關閉。只有在 --force 為 true (強制刪除) 時才能設定為 0。

-h, --help

delete 的說明

--ignore-not-found

將 "resource not found" 視為成功刪除。當指定 --all 時,預設為 "true"。

-i, --interactive

如果為 true,則僅在使用者確認時刪除資源。

-k, --kustomize string

處理 kustomization 目錄。此旗標不能與 -f 或 -R 一起使用。

--now

如果為 true,則資源會收到立即關閉的訊號 (與 --grace-period=1 相同)。

-o, --output string

輸出模式。使用 "-o 名稱" 以獲得更簡短的輸出 (resource/名稱)。

--raw 字串

要對伺服器執行 DELETE 操作的原始 URI。使用 kubeconfig 檔案指定的傳輸方式。

-R, --recursive

遞迴處理 -f, --filename 中使用的目錄。當您想要管理組織在同一目錄中的相關 manifest 時很有用。

-l, --selector 字串

用於篩選的選擇器 (標籤查詢),支援 '=', '==' 和 '!='。(例如 -l key1=value1,key2=value2)。符合的物件必須滿足所有指定的標籤約束。

--timeout 持續時間

放棄刪除前等待的時間長度,零表示從物件大小決定逾時時間

--wait     預設值: true

如果為 true,則等待資源消失後再返回。這會等待終止器 (finalizers)。

--as 字串

模擬操作的使用者名稱。使用者可以是常規使用者或命名空間中的服務帳戶。

--as-group 字串

模擬操作的群組,可以重複此標記以指定多個群組。

--as-uid 字串

模擬操作的 UID。

--cache-dir 字串     預設值: "$HOME/.kube/cache"

預設快取目錄

--certificate-authority 字串

憑證授權單位憑證檔案的路徑

--client-certificate 字串

TLS 用戶端憑證檔案的路徑

--client-key 字串

TLS 用戶端金鑰檔案的路徑

--cluster 字串

要使用的 kubeconfig 叢集名稱

--context 字串

要使用的 kubeconfig context 名稱

--default-not-ready-toleration-seconds int     預設值: 300

表示預設新增到每個尚未具有此容忍度的 Pod 的 notReady:NoExecute 容忍度的 tolerationSeconds。

--default-unreachable-toleration-seconds int     預設值: 300

表示預設新增到每個尚未具有此容忍度的 Pod 的 unreachable:NoExecute 容忍度的 tolerationSeconds。

--disable-compression

如果為 true,則針對所有對伺服器的請求選擇退出回應壓縮

--insecure-skip-tls-verify

如果為 true,則不會檢查伺服器憑證的有效性。這會使您的 HTTPS 連線不安全

--kubeconfig 字串

用於 CLI 請求的 kubeconfig 檔案路徑。

--match-server-version

要求伺服器版本與用戶端版本相符

-n, --namespace 字串

如果存在,則此 CLI 請求的命名空間範圍

--password 字串

用於 API 伺服器的基本身份驗證密碼

--profile 字串     預設值: "none"

要擷取的 profile 名稱。選項之一 (none|cpu|heap|goroutine|threadcreate|block|mutex)

--profile-output 字串     預設值: "profile.pprof"

要將 profile 寫入的檔案名稱

--request-timeout 字串     預設值: "0"

放棄單個伺服器請求前等待的時間長度。非零值應包含相應的時間單位 (例如 1s, 2m, 3h)。值為零表示請求永不逾時。

-s, --server 字串

Kubernetes API 伺服器的位址和埠

--storage-driver-buffer-duration 持續時間     預設值: 1m0s

儲存驅動程式中的寫入將緩衝此持續時間,並作為單個交易提交到非記憶體後端

--storage-driver-db 字串     預設值: "cadvisor"

資料庫名稱

--storage-driver-host 字串     預設值: "localhost:8086"

資料庫主機:埠

--storage-driver-password 字串     預設值: "root"

資料庫密碼

--storage-driver-secure

使用安全連線連線資料庫

--storage-driver-table 字串     預設值: "stats"

資料表名稱

--storage-driver-user 字串     預設值: "root"

資料庫使用者名稱

--tls-server-name 字串

用於伺服器憑證驗證的伺服器名稱。如果未提供,則使用用於連線伺服器的主機名稱

--token 字串

用於 API 伺服器身份驗證的 Bearer token

--user 字串

要使用的 kubeconfig 使用者名稱

--username 字串

用於 API 伺服器的基本身份驗證使用者名稱

--version version[=true]

--version, --version=raw 印出版本資訊並退出;--version=vX.Y.Z... 設定報告的版本

--warnings-as-errors

將從伺服器收到的警告視為錯誤,並以非零退出代碼退出

參見

  • kubectl - kubectl 控制 Kubernetes 叢集管理器

此頁面為自動產生。

如果您計劃回報此頁面的問題,請在您的問題描述中提及此頁面是自動產生的。修復可能需要在 Kubernetes 專案的其他地方進行。

上次修改時間:2024 年 12 月 12 日下午 2:44 PST:為 v1.32 更新 kubectl 參考 (5c2cfa02af)