Skip to content

Commit aba444c

Browse files
authored
Merge pull request #38738 from Zhuzhenghao/fix/update-declarative-config
[zh] sync declarative-config.md
2 parents 4f4bde1 + bd9830b commit aba444c

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

content/zh-cn/docs/tasks/manage-kubernetes-objects/declarative-config.md

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ kubectl delete -f <文件名>
538538
```
539539

540540
<!--
541-
### Alternative: `kubectl apply -f <directory/> -prune -l your=label`
541+
### Alternative: `kubectl apply -f <directory/> --prune -l your=label`
542542
543543
Only use this if you know what you are doing.
544544
-->
@@ -547,7 +547,7 @@ Only use this if you know what you are doing.
547547
只有在充分理解此命令背后含义的情况下才建议这样操作。
548548

549549
<!--
550-
`kubectl apply -prune` is in alpha, and backwards incompatible
550+
`kubectl apply --prune` is in alpha, and backwards incompatible
551551
changes might be introduced in subsequent releases.
552552
-->
553553
{{< warning >}}
@@ -556,12 +556,16 @@ changes might be introduced in subsequent releases.
556556
{{< /warning >}}
557557

558558
{{< warning >}}
559+
<!--
560+
You must be careful when using this command, so that you
561+
do not delete objects unintentionally.
562+
-->
559563
在使用此命令时必须小心,这样才不会无意中删除不想删除的对象。
560564
{{< /warning >}}
561565

562566
<!--
563567
As an alternative to `kubectl delete`, you can use `kubectl apply` to identify objects to be deleted after their
564-
configuration files have been removed from the directory. Apply with `-prune`
568+
configuration files have been removed from the directory. Apply with `--prune`
565569
queries the API server for all objects matching a set of labels, and attempts
566570
to match the returned live object configurations against the object
567571
configuration files. If an object matches the query, and it does not have a
@@ -575,6 +579,10 @@ it is deleted.
575579
如果某对象在查询中被匹配到,但在目录中没有文件与其相对应,并且其中还包含
576580
`last-applied-configuration` 注解,则该对象会被删除。
577581

582+
{{< comment >}}
583+
TODO(pwittrock): We need to change the behavior to prevent the user from running apply on subdirectories unintentionally.
584+
{{< /comment >}}
585+
578586
```shell
579587
kubectl apply -f <directory/> --prune -l <labels>
580588
```
@@ -679,6 +687,8 @@ kind: Deployment
679687
metadata:
680688
annotations:
681689
# ...
690+
# 注意注解中并不包含 replicas
691+
# 这是因为更新并不是通过 kubectl apply 来执行的
682692
kubectl.kubernetes.io/last-applied-configuration: |
683693
{"apiVersion":"apps/v1","kind":"Deployment",
684694
"metadata":{"annotations":{},"name":"nginx-deployment","namespace":"default"},
@@ -687,7 +697,7 @@ metadata:
687697
"ports":[{"containerPort":80}]}]}}}}
688698
# ...
689699
spec:
690-
replicas: 2
700+
replicas: 2 # written by scale
691701
# ...
692702
minReadySeconds: 5
693703
selector:
@@ -753,7 +763,7 @@ kind: Deployment
753763
metadata:
754764
annotations:
755765
# ...
756-
# 注解中包含更新后的 image,nginx 1.11.9,
766+
# 注解中包含更新后的 image,nginx 1.16.1,
757767
# 但不包含更新后的 replicas
758768
kubectl.kubernetes.io/last-applied-configuration: |
759769
{"apiVersion":"apps/v1","kind":"Deployment",
@@ -767,8 +777,8 @@ spec:
767777
matchLabels:
768778
# ...
769779
app: nginx
770-
replicas: 2
771-
# minReadySeconds 此字段被清除
780+
replicas: 2 # 由 `kubectl scale` 设置,被 `kubectl apply` 命令忽略
781+
# minReadySeconds 此字段被`kubectl apply`清除
772782
# ...
773783
template:
774784
metadata:
@@ -777,7 +787,7 @@ spec:
777787
app: nginx
778788
spec:
779789
containers:
780-
- image: nginx:1.16.1
790+
- image: nginx:1.16.1 # 由 `kubectl apply` 设置
781791
# ...
782792
name: nginx
783793
ports:
@@ -1430,6 +1440,14 @@ It is OK to use imperative deletion with declarative management.
14301440
在声明式管理方法中使用指令式命令来删除对象是可以的。
14311441
{{< /note >}}
14321442

1443+
{{< comment >}}
1444+
TODO(pwittrock): We need to make using imperative commands with
1445+
declarative object configuration work so that it doesn't write the
1446+
fields to the annotation, and instead. Then add this bullet point.
1447+
1448+
- using imperative commands with declarative configuration to manage where each manages different fields.
1449+
{{< /comment >}}
1450+
14331451
<!--
14341452
### Migrating from imperative command management to declarative object configuration
14351453

@@ -1446,52 +1464,51 @@ configuration involves several manual steps:
14461464
kubectl get <kind>/<name> -o yaml > <kind>_<name>.yaml
14471465
```
14481466

1449-
1. Manually remove the `status` field from the configuration file.
1467+
2. Manually remove the `status` field from the configuration file.
14501468

14511469
{{< note >}}
1452-
This step is optional, as `kubectl apply` does not update the status field
1453-
even if it is present in the configuration file.
1470+
This step is optional, as `kubectl apply` does not update the status field even if it is present in the configuration file.
14541471
{{< /note >}}
14551472

1456-
1. Set the `kubectl.kubernetes.io/last-applied-configuration` annotation on the object:
1473+
3. Set the `kubectl.kubernetes.io/last-applied-configuration` annotation on the object:
14571474

14581475
```shell
1459-
kubectl replace -save-config -f <kind>_<name>.yaml
1476+
kubectl replace --save-config -f <kind>_<name>.yaml
14601477
```
14611478

1462-
1. Change processes to use `kubectl apply` for managing the object exclusively.
1479+
4. Change processes to use `kubectl apply` for managing the object exclusively.
14631480
-->
14641481
1. 将现时对象导出到本地配置文件:
14651482

14661483
```shell
14671484
kubectl get <kind>/<name> -o yaml > <kind>_<name>.yaml
14681485
```
14691486

1470-
1. 手动移除配置文件中的 `status` 字段。
1487+
2. 手动移除配置文件中的 `status` 字段。
14711488

1472-
<!--
1473-
This step is optional, as `kubectl apply` does not update the status field
1474-
even if it is present in the configuration file.
1475-
-->
14761489
{{< note >}}
14771490
这一步骤是可选的,因为 `kubectl apply` 并不会更新 status 字段,即便
14781491
配置文件中包含 status 字段。
14791492
{{< /note >}}
14801493

1481-
1. 设置对象上的 `kubectl.kubernetes.io/last-applied-configuration` 注解:
1494+
3. 设置对象上的 `kubectl.kubernetes.io/last-applied-configuration` 注解:
14821495

14831496
```shell
14841497
kubectl replace --save-config -f <kind>_<name>.yaml
14851498
```
1486-
1. 更改过程,使用 `kubectl apply` 专门管理对象。
1499+
4. 更改过程,使用 `kubectl apply` 专门管理对象。
1500+
1501+
{{< comment >}}
1502+
TODO(pwittrock): Why doesn't export remove the status field? Seems like it should.
1503+
{{< /comment >}}
14871504

14881505
<!--
14891506
### Migrating from imperative object configuration to declarative object configuration
14901507

14911508
1. Set the `kubectl.kubernetes.io/last-applied-configuration` annotation on the object:
14921509

14931510
```shell
1494-
kubectl replace -save-config -f <kind>_<name>.yaml
1511+
kubectl replace --save-config -f <kind>_<name>.yaml
14951512
```
14961513

14971514
1. Change processes to use `kubectl apply` for managing the object exclusively.
@@ -1501,7 +1518,7 @@ configuration involves several manual steps:
15011518
1. 在对象上设置 `kubectl.kubernetes.io/last-applied-configuration` 注解:
15021519

15031520
```shell
1504-
kubectl replace -save-config -f <kind>_<name>.yaml
1521+
kubectl replace --save-config -f <kind>_<name>.yaml
15051522
```
15061523

15071524
1. 自此排他性地使用 `kubectl apply` 来管理对象。

0 commit comments

Comments
 (0)