@@ -21,7 +21,6 @@ in this task demonstrate a strategic merge patch and a JSON merge patch.
21
21
这个任务展示如何使用 ` kubectl patch ` 就地更新 API 对象。
22
22
这个任务中的练习演示了一个策略性合并 patch 和一个 JSON 合并 patch。
23
23
24
-
25
24
## {{% heading "prerequisites" %}}
26
25
27
26
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
@@ -62,7 +61,7 @@ kubectl get pods
62
61
The output shows that the Deployment has two Pods. The `1/1` indicates that
63
62
each Pod has one container:
64
63
-->
65
- 输出显示 Deployment 有两个 Pod。` 1/1 ` 表示每个 Pod 有一个容器:
64
+ 输出显示 Deployment 有两个 Pod。` 1/1 ` 表示每个 Pod 有一个容器:
66
65
67
66
```
68
67
NAME READY STATUS RESTARTS AGE
@@ -85,7 +84,7 @@ you want each Pod to have two containers: one that runs nginx and one that runs
85
84
<!--
86
85
Create a file named `patch-file.yaml` that has this content:
87
86
-->
88
- 创建一个名为 ` patch-file.yaml ` 的文件。内容如下:
87
+ 创建一个名为 ` patch-file.yaml ` 的文件。内容如下:
89
88
90
89
``` yaml
91
90
spec :
@@ -116,7 +115,7 @@ kubectl get deployment patch-demo --output yaml
116
115
<!--
117
116
The output shows that the PodSpec in the Deployment has two Containers:
118
117
-->
119
- 输出显示 Deployment 中的 PodSpec 有两个容器:
118
+ 输出显示 Deployment 中的 PodSpec 有两个容器:
120
119
121
120
``` yaml
122
121
containers :
@@ -133,7 +132,7 @@ containers:
133
132
<!--
134
133
View the Pods associated with your patched Deployment:
135
134
-->
136
- 查看与 patch Deployment 相关的 Pod:
135
+ 查看与 patch Deployment 相关的 Pod:
137
136
138
137
``` shell
139
138
kubectl get pods
@@ -145,8 +144,8 @@ were running previously. The Deployment terminated the old Pods and created two
145
144
new Pods that comply with the updated Deployment spec. The `2/2` indicates that
146
145
each Pod has two Containers:
147
146
-->
148
- 输出显示正在运行的 Pod 与以前运行的 Pod 有不同的名称。Deployment 终止了旧的 Pod,并创建了两个
149
- 符合更新的部署规范的新 Pod。` 2/2 ` 表示每个 Pod 有两个容器:
147
+ 输出显示正在运行的 Pod 与以前运行的 Pod 有不同的名称。Deployment 终止了旧的 Pod,
148
+ 并创建了两个符合更新后的 Deployment 规约的新 Pod。` 2/2 ` 表示每个 Pod 有两个容器:
150
149
151
150
```
152
151
NAME READY STATUS RESTARTS AGE
@@ -157,7 +156,7 @@ patch-demo-1081991389-jmg7b 2/2 Running 0 1m
157
156
<!--
158
157
Take a closer look at one of the patch-demo Pods:
159
158
-->
160
- 仔细查看其中一个 patch-demo Pod:
159
+ 仔细查看其中一个 patch-demo Pod:
161
160
162
161
``` shell
163
162
kubectl get pod < your-pod-name> --output yaml
@@ -166,7 +165,7 @@ kubectl get pod <your-pod-name> --output yaml
166
165
<!--
167
166
The output shows that the Pod has two Containers: one running nginx and one running redis:
168
167
-->
169
- 输出显示 Pod 有两个容器: 一个运行 nginx,一个运行 redis:
168
+ 输出显示 Pod 有两个容器: 一个运行 nginx,一个运行 redis:
170
169
171
170
```
172
171
containers:
@@ -206,25 +205,29 @@ patch 策略由 Kubernetes 源代码中字段标记中的 `patchStrategy` 键的
206
205
type PodSpec struct {
207
206
...
208
207
Containers []Container ` json:"containers" patchStrategy:"merge" patchMergeKey:"name" ...`
208
+ ...
209
+ }
209
210
```
210
211
211
212
<!--
212
213
You can also see the patch strategy in the
213
214
[OpenApi spec](https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json):
214
215
-->
215
- 你还可以在 [OpenApi spec](https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json)
216
- 规范中看到 patch 策略:
216
+ 你还可以在
217
+ [ OpenApi 规范] ( https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json ) 中看到
218
+ patch 策略:
217
219
218
- ` ` ` json
220
+ ``` yaml
219
221
" io.k8s.api.core.v1.PodSpec " : {
220
- ...
221
- " containers" : {
222
- " description" : " List of containers belonging to the pod. ...
223
- },
224
- " x-kubernetes-patch-merge-key" : " name" ,
225
- " x-kubernetes-patch-strategy" : " merge"
226
- },
222
+ ...,
223
+ " containers " : {
224
+ " description " : " List of containers belonging to the pod. .... "
225
+ },
226
+ " x-kubernetes-patch-merge-key " : " name" ,
227
+ " x-kubernetes-patch-strategy " : " merge"
228
+ }
227
229
```
230
+ <!-- for editors: intionally use yaml instead of json here, to prevent syntax highlight error. -->
228
231
229
232
<!--
230
233
And you can see the patch strategy in the
@@ -253,7 +256,7 @@ Patch your Deployment:
253
256
-->
254
257
对 Deployment 执行 patch 操作:
255
258
256
- ```
259
+ ` ` ` shell
257
260
kubectl patch deployment patch-demo --patch-file patch-file-tolerations.yaml
258
261
```
259
262
@@ -271,23 +274,11 @@ The output shows that the PodSpec in the Deployment has only one Toleration:
271
274
-->
272
275
输出结果显示 Deployment 中的 PodSpec 只有一个容忍度设置:
273
276
274
- ```shell
275
-
276
- containers:
277
- - image: redis
278
- imagePullPolicy: Always
279
- name: patch-demo-ctr-2
280
- ...
281
- - image: nginx
282
- imagePullPolicy: Always
283
- name: patch-demo-ctr
284
- ...
285
- ```
286
277
``` yaml
287
278
tolerations :
288
- - effect: NoSchedule
289
- key: disktype
290
- value: ssd
279
+ - effect : NoSchedule
280
+ key : disktype
281
+ value : ssd
291
282
` ` `
292
283
293
284
<!--
@@ -302,6 +293,8 @@ strategic merge patch uses the default patch strategy, which is `replace`.
302
293
type PodSpec struct {
303
294
...
304
295
Tolerations []Toleration ` json:"tolerations,omitempty" protobuf:"bytes,22,opt,name=tolerations"`
296
+ ...
297
+ }
305
298
```
306
299
307
300
<!--
@@ -321,7 +314,7 @@ replaces the existing list.
321
314
<!--
322
315
The `kubectl patch` command has a `type` parameter that you can set to one of these values:
323
316
-->
324
- `kubectl patch` 命令有一个 `type` 参数,你可以将其设置为以下值之一:
317
+ ` kubectl patch ` 命令有一个 ` type ` 参数,你可以将其设置为以下值之一:
325
318
326
319
<table >
327
320
<!--
@@ -353,7 +346,7 @@ did a strategic merge patch.
353
346
Next, do a JSON merge patch on your same Deployment. Create a file named `patch-file-2.yaml`
354
347
that has this content:
355
348
-->
356
- 下一步,在相同的 Deployment 上执行 JSON 合并 patch。创建一个名为 ` patch-file-2` 的文件。内容如下:
349
+ 下一步,在相同的 Deployment 上执行 JSON 合并 patch。创建一个名为 ` patch-file-2 ` 的文件。内容如下:
357
350
358
351
``` yaml
359
352
spec :
@@ -400,7 +393,7 @@ spec:
400
393
<!--
401
394
List the running Pods :
402
395
-->
403
- 列表中运行的 Pod:
396
+ 列出正运行的 Pod:
404
397
405
398
` ` ` shell
406
399
kubectl get pods
@@ -475,8 +468,8 @@ The way to remove the value for `spec.strategy.rollingUpdate` when updating the
475
468
476
469
Create another file named `patch-file-retainkeys.yaml` that has this content:
477
470
-->
478
- 更新 `type` 取值的同时移除 `spec.strategy.rollingUpdate` 现有值的方法是
479
- 为策略性合并操作设置 `retainKeys` 策略:
471
+ 更新 `type` 取值的同时移除 `spec.strategy.rollingUpdate`
472
+ 现有值的方法是为策略性合并操作设置 `retainKeys` 策略:
480
473
481
474
创建另一个名为 `patch-file-retainkeys.yaml` 的文件,内容如下:
482
475
@@ -536,8 +529,8 @@ The patch you did in the preceding exercise is called a *strategic merge patch w
536
529
-->
537
530
# ## 关于使用 retainKeys 策略的策略合并 patch 操作的说明 {#notes-on-the-strategic-merge-patch-using-the-retainkeys-strategy}
538
531
539
- 在前文练习中所执行的称作 *带 `retainKeys` 策略的策略合并 patch(Strategic Merge
540
- Patch with retainKeys Strategy)*。
532
+ 在前文练习中所执行的称作 ** 带 `retainKeys` 策略的策略合并 patch(Strategic Merge
533
+ Patch with retainKeys Strategy)** 。
541
534
这种方法引入了一种新的 `$retainKey` 指令,具有如下策略:
542
535
543
536
- 其中包含一个字符串列表;
@@ -559,30 +552,37 @@ type DeploymentSpec struct {
559
552
...
560
553
// +patchStrategy=retainKeys
561
554
Strategy DeploymentStrategy ` json:"strategy,omitempty" patchStrategy:"retainKeys" ...`
555
+ ...
556
+ }
562
557
```
563
558
564
559
<!--
565
560
You can also see the `retainKeys` strategy in the [OpenApi spec](https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json):
566
561
-->
567
- 你也可以查看 [OpenAPI 规范](https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json)中的 `retainKeys` 策略:
562
+ 你也可以查看
563
+ [ OpenAPI 规范] ( https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json ) 中的
564
+ ` retainKeys ` 策略:
568
565
569
- ```json
566
+ ``` yaml
570
567
" io.k8s.api.apps.v1.DeploymentSpec " : {
571
- ...
572
- "strategy": {
573
- "$ref": "#/definitions/io.k8s.api.apps.v1.DeploymentStrategy",
574
- "description": "The deployment strategy to use to replace existing pods with new ones.",
575
- "x-kubernetes-patch-strategy": "retainKeys"
576
- },
568
+ ...,
569
+ " strategy " : {
570
+ " $ref " : " #/definitions/io.k8s.api.apps.v1.DeploymentStrategy" ,
571
+ " description " : " The deployment strategy to use to replace existing pods with new ones." ,
572
+ " x-kubernetes-patch-strategy " : " retainKeys"
573
+ },
574
+ ....
575
+ }
577
576
```
577
+ <!-- for editors: intionally use yaml instead of json here, to prevent syntax highlight error. -->
578
578
579
579
<!--
580
580
And you can see the `retainKeys` strategy in the
581
581
[Kubernetes API documentation](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#deploymentspec-v1-apps).
582
582
-->
583
583
而且你也可以在
584
- [Kubernetes API 文档](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#deploymentspec-v1-apps).
585
- 中看到 `retainKey` 策略。
584
+ [ Kubernetes API 文档] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#deploymentspec-v1-apps)中看到
585
+ ` retainKey ` 策略。
586
586
587
587
<!--
588
588
## Alternate forms of the kubectl patch command
@@ -592,8 +592,7 @@ directly on the command line.
592
592
-->
593
593
## kubectl patch 命令的其他形式 {#alternate-forms-of-the-kubectl-patch-command}
594
594
595
- `kubectl patch` 命令使用 YAML 或 JSON。它可以接受以文件形式提供的补丁,也可以
596
- 接受直接在命令行中给出的补丁。
595
+ ` kubectl patch ` 命令使用 YAML 或 JSON。它可以接受以文件形式提供的补丁,也可以接受直接在命令行中给出的补丁。
597
596
598
597
<!--
599
598
Create a file named `patch-file.json` that has this content:
@@ -654,17 +653,13 @@ and
654
653
[ ` kubectl scale ` ] ( /docs/reference/generated/kubectl/kubectl-commands/#scale ) 和
655
654
[ ` kubectl apply ` ] ( /docs/reference/generated/kubectl/kubectl-commands/#apply ) 。
656
655
657
-
658
- <!--
659
656
{{< note >}}
657
+ <!--
660
658
Strategic merge patch is not supported for custom resources.
661
- {{< /note >}}
662
659
-->
663
- {{< note >}}
664
660
定制资源不支持策略性合并 patch。
665
661
{{< /note >}}
666
662
667
-
668
663
## {{% heading "whatsnext" %}}
669
664
670
665
<!--
@@ -675,6 +670,6 @@ Strategic merge patch is not supported for custom resources.
675
670
-->
676
671
* [ Kubernetes 对象管理] ( /zh-cn/docs/concepts/overview/working-with-objects/object-management/ )
677
672
* [ 使用指令式命令管理 Kubernetes 对象] ( /zh-cn/docs/tasks/manage-kubernetes-objects/imperative-command/ )
678
- * [使用配置文件执行 Kubernetes 对象的指令式管理 ](/zh-cn/docs/tasks/manage-kubernetes-objects/imperative-config)
673
+ * [ 使用配置文件对 Kubernetes 对象进行命令式管理 ] ( /zh-cn/docs/tasks/manage-kubernetes-objects/imperative-config/ )
679
674
* [ 使用配置文件对 Kubernetes 对象进行声明式管理] ( /zh-cn/docs/tasks/manage-kubernetes-objects/declarative-config/ )
680
675
0 commit comments