本頁說明如何在還原程序中修改 Kubernetes 資源。
總覽
您可能基於多種原因,想在還原程序中修改 Kubernetes 資源。例如:
您可能想使用其他儲存空間佈建工具佈建 PVC (例如從 Kubernetes 樹狀結構內建驅動程式移至 CSI 驅動程式)。
您可能想以其他名稱還原命名空間。
您可能想變更 Deployment 或 StatefulSet 中的副本數量。
GKE 備份功能提供一種機制,稱為「替代規則」,您可以選擇在 RestorePlan
中定義這些規則。
替代規則是一組比對條件,與替代欄位值一併指定。您可以在還原程序中使用替代規則,如下所示:
首先,Backup for GKE 會根據還原設定中的範圍參數 (例如:命名空間、群組/種類等),計算要還原的資源集。
Backup for GKE 會根據替代規則的排序清單,評估選取要還原的每個資源。如果規則與資源相符,系統會以取代的欄位值更新資源。所有資源都會與所有規則比對,且同一資源可能會進行多次替換。
評估完所有規則後,Backup for GKE 會在目標叢集中建立產生的資源集。
替代規則參數
如要定義替代規則,請提供下列參數:
參數 | 必要 | 說明 |
---|---|---|
targetNamespaces | 選用 |
這是命名空間清單。如要讓資源符合這項規則,資源必須是命名空間資源,且具有下列其中一個命名空間:
|
targetGroupKinds | 選用 |
這是 Kubernetes 資源群組/種類元組的清單:
|
targetJsonPath | 必填 | 這是與資源比對的 JSONPath 運算式。請注意,這項規則不僅會比對資源,也會比對資源中的特定屬性。 |
originalValuePattern | 選用 | 這是與屬性目前值相符的規則運算式。如未提供,屬性一律會相符。 |
newValue | 必填 | 這是要用於相符屬性值的新值。 |
如要進一步瞭解如何在 Google Cloud 控制台中定義替代規則,請參閱「規劃一組還原作業」。
如要使用 gcloud CLI 定義替代規則,請建立包含 substitutionRules YAML 陣列的檔案,並將 --substitution-rules-file=
參數加入 gcloud beta container backup-restore restore-plans create
指令。
替代規則範例
下列範例採用 gcloud CLI 使用的 YAML 格式。
變更 StorageClass
在本範例中,我們將所有還原的 PersistentVolumeClaim 資源中的 StorageClass 從 standard
變更為 premium-rwo
:
substitutionRules:
- targetGroupKinds:
- resourceGroup: ''
resourceKind: PersistentVolumeClaim
targetJsonPath: "{..storageClassName}"
originalValuePattern: standard
newValue: premium-rwo
複製命名空間
在本例中,我們會將命名空間從 Alpha 複製到 Beta,也就是建立新的「beta」命名空間,並將「alpha」中的所有資源還原到新的「beta」命名空間。請注意,這個範例需要兩項替代規則:一項適用於命名空間本身,另一項則適用於命名空間內的資源。
substitutionRules:
- targetNamespaces:
- ''
targetGroupKinds:
- resourceGroup: ''
resourceKind: Namespace
targetJsonPath: "{.metadata.name}"
originalValuePattern: alpha
newValue: beta
- targetNamespaces:
- alpha
targetJsonPath: "{.metadata.namespace}"
originalValuePattern: alpha
newValue: beta
在複製的命名空間中變更 StorageClass 和副本計數
在本例中,我們會複製命名空間,然後對新命名空間中的資源套用一組變更:
將 PVC 上的 StorageClass 從
standard
變更為premium-rwo
將部署項目的副本數量從
7
變更為3
substitutionRules:
- targetNamespaces:
- ''
targetGroupKinds:
- resourceGroup: ''
resourceKind: Namespace
targetJsonPath: "{.metadata.name}"
originalValuePattern: alpha
newValue: beta
- targetNamespaces:
- alpha
targetJsonPath: "{.metadata.namespace}"
originalValuePattern: alpha
newValue: beta
- targetNamespaces:
- beta
targetGroupKinds:
- resourceGroup: ''
resourceKind: PersistentVolumeClaim
targetJsonPath: "{..storageClassName}"
originalValuePattern: standard
newValue: premium-rwo
- targetNamespaces:
- beta
targetGroupKinds:
- resourceGroup: apps
resourceKind: Deployment
targetJsonPath: "{$.spec.replicas}"
originalValuePattern: '7'
newValue: '3'