kubectl patch

概要

使用策略性合併修補程式、JSON 合併修補程式或 JSON 修補程式更新資源的欄位。

接受 JSON 和 YAML 格式。

注意:自訂資源不支援策略性合併修補程式。

kubectl patch (-f FILENAME | TYPE NAME) [-p PATCH|--patch-file FILE]

範例

  # Partially update a node using a strategic merge patch, specifying the patch as JSON
  kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}'
  
  # Partially update a node using a strategic merge patch, specifying the patch as YAML
  kubectl patch node k8s-node-1 -p $'spec:\n unschedulable: true'
  
  # Partially update a node identified by the type and name specified in "node.json" using strategic merge patch
  kubectl patch -f node.json -p '{"spec":{"unschedulable":true}}'
  
  # Update a container's image; spec.containers[*].name is required because it's a merge key
  kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'
  
  # Update a container's image using a JSON patch with positional arrays
  kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'
  
  # Update a deployment's replicas through the 'scale' subresource using a merge patch
  kubectl patch deployment nginx-deployment --subresource='scale' --type='merge' -p '{"spec":{"replicas":2}}'

選項

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

如果為 true,當範本中缺少欄位或地圖索引鍵時,請忽略範本中的任何錯誤。僅適用於 golang 和 jsonpath 輸出格式。

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

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

--field-manager string     預設值:"kubectl-patch"

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

-f, --filename strings

識別要更新之資源的檔案名稱、目錄或 URL。

-h, --help

patch 的說明

-k, --kustomize string

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

--local

如果為 true,patch 將對檔案內容而非伺服器端資源進行操作。

-o, --output string

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

-p, --patch string

要套用至資源 JSON 檔案的修補程式。

--patch-file string

包含要套用至資源之修補程式的檔案。

-R, --recursive

遞迴處理 -f、--filename 中使用的目錄。當您想要管理在相同目錄中組織的相關資訊清單時很有用。

--show-managed-fields

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

--subresource string

如果指定,patch 將對請求物件的子資源進行操作。此旗標為 beta 版,未來可能會變更。

--template string

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

--type string     預設值:"strategic"

提供的修補程式類型;選項之一 [json merge strategic]。

--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 環境定義名稱。

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

指出針對 notReady:NoExecute 容忍度的 tolerationSeconds,預設會新增至每個尚未具有此容忍度的 Pod。

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

指出針對 unreachable:NoExecute 容忍度的 tolerationSeconds,預設會新增至每個尚未具有此容忍度的 Pod。

--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"

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

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

要將分析檔案寫入的檔案名稱

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

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

-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 權杖

--user string

要使用的 kubeconfig 使用者名稱

--username string

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)