kubectl apply

摘要

依檔案名稱或 stdin 將組態套用到資源。必須指定資源名稱。如果資源尚不存在,將會建立此資源。若要使用 'apply',請務必先使用 'apply' 或 'create --save-config' 建立資源。

接受 JSON 和 YAML 格式。

Alpha 免責聲明:--prune 功能尚未完成。除非您知道目前狀態,否則請勿使用。請參閱 https://issues.k8s.io/34274

kubectl apply (-f FILENAME | -k DIRECTORY)

範例

  # Apply the configuration in pod.json to a pod
  kubectl apply -f ./pod.json
  
  # Apply resources from a directory containing kustomization.yaml - e.g. dir/kustomization.yaml
  kubectl apply -k dir/
  
  # Apply the JSON passed into stdin to a pod
  cat pod.json | kubectl apply -f -
  
  # Apply the configuration from all files that end with '.json'
  kubectl apply -f '*.json'
  
  # Note: --prune is still in Alpha
  # Apply the configuration in manifest.yaml that matches label app=nginx and delete all other resources that are not in the file and match label app=nginx
  kubectl apply --prune -f manifest.yaml -l app=nginx
  
  # Apply the configuration in manifest.yaml and delete all the other config maps that are not in the file
  kubectl apply --prune -f manifest.yaml --all --prune-allowlist=core/v1/ConfigMap

選項

--all

選取指定資源類型之命名空間中的所有資源。

--allow-missing-template-keys     預設值:true

如果為 true,則在範本中缺少欄位或地圖金鑰時,忽略範本中的任何錯誤。僅適用於 golang 和 jsonpath 輸出格式。

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

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

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

必須為 "none"、"server" 或 "client"。如果是 client 策略,則僅列印將傳送的物件,而不傳送它。如果是 server 策略,則提交伺服器端請求,而不保留資源。

--field-manager string     預設值:"kubectl-client-side-apply"

用於追蹤欄位擁有權的管理員名稱。

-f, --filename strings

包含要套用之組態的檔案。

--force

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

--force-conflicts

如果為 true,伺服器端套用將強制變更以應對衝突。

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

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

-h, --help

apply 的說明

-k, --kustomize string

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

--openapi-patch     預設值:true

如果為 true,當 openapi 存在且可以在 openapi 規格中找到資源時,使用 openapi 計算差異。否則,回復為使用內建類型。

-o, --output string

輸出格式。選項之一:(json、yaml、name、go-template、go-template-file、template、templatefile、jsonpath、jsonpath-as-json、jsonpath-file)。

--overwrite     預設值:true

透過使用修改過的組態中的值,自動解決修改過的組態與即時組態之間的衝突

--prune

自動刪除未出現在組態中,且由 apply 或 create --save-config 建立的資源物件。應與 -l 或 --all 一起使用。

--prune-allowlist strings

使用 --prune 的 <group/version/kind> 覆寫預設允許清單

-R, --recursive

處理 -f, --filename 中使用的目錄,以進行遞迴處理。當您想要管理組織在相同目錄中的相關 Manifest 檔案時非常有用。

-l, --selector string

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

--server-side

如果為 true,則套用會在伺服器端而非用戶端執行。

--show-managed-fields

如果為 true,在以 JSON 或 YAML 格式列印物件時,保留 managedFields。

--subresource string

如果指定,套用操作將在請求物件的子資源上執行。僅在使用 --server-side 時允許。此標誌為 Beta 版,未來可能會變更。

--template string

當使用 -o=go-template, -o=go-template-file 時,要使用的範本字串或範本檔案路徑。範本格式為 golang 範本 [https://go.dev.org.tw/pkg/text/template/#pkg-overview]。

--timeout duration

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

--validate string[="strict"]     預設值:"strict"

必須是以下之一:strict (或 true)、warn、ignore (或 false)。 "true" 或 "strict" 將使用 schema 驗證輸入,如果無效則請求失敗。如果 api-server 上啟用 ServerSideFieldValidation,它將執行伺服器端驗證,但如果未啟用,則會回退到較不可靠的用戶端驗證。"warn" 將在 API 伺服器上啟用伺服器端欄位驗證時,警告未知或重複的欄位,而不會阻止請求,否則行為如同 "ignore"。"false" 或 "ignore" 將不執行任何 schema 驗證,靜默丟棄任何未知或重複的欄位。

--wait

如果為 true,等待資源消失後再返回。這會等待 finalizers。

--as string

要模擬執行操作的使用者名稱。使用者可以是命名空間中的一般使用者或服務帳戶。

--as-group strings

要模擬執行操作的群組,可以重複此標誌以指定多個群組。

--as-uid string

要模擬執行操作的 UID。

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

預設快取目錄

--certificate-authority string

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

--client-certificate string

TLS 用戶端憑證檔案的路徑

--client-key string

TLS 用戶端金鑰檔案的路徑

--cluster string

要使用的 kubeconfig 叢集名稱

--context string

要使用的 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 string

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

--match-server-version

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

-n, --namespace string

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

--password string

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

--profile string     預設值:"none"

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

--profile-output string     預設值:"profile.pprof"

要將 profile 寫入的檔案名稱

--request-timeout string     預設值:"0"

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

-s, --server string

Kubernetes API 伺服器的位址和埠號

--storage-driver-buffer-duration duration     預設值:1m0s

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

--storage-driver-db string     預設值:"cadvisor"

資料庫名稱

--storage-driver-host string     預設值:"localhost:8086"

資料庫主機:埠號

--storage-driver-password string     預設值:"root"

資料庫密碼

--storage-driver-secure

使用與資料庫的安全連線

--storage-driver-table string     預設值:"stats"

資料表名稱

--storage-driver-user string     預設值:"root"

資料庫使用者名稱

--tls-server-name string

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

--token string

API 伺服器的 Bearer token 身份驗證

--user string

要使用的 kubeconfig 使用者名稱

--username string

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

--version version[=true]

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

--warnings-as-errors

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

參見

此頁面為自動產生。

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

上次修改時間為 2024 年 12 月 12 日下午 2:44 PST:Update kubectl reference for v1.32 (5c2cfa02af)