Skip to content

Commit 297d82a

Browse files
authored
Merge pull request #36857 from windsonsea/updapi
[zh]Sync /update-api-object-kubectl-patch.md
2 parents 345fcaa + 26ba47c commit 297d82a

File tree

1 file changed

+124
-2
lines changed

1 file changed

+124
-2
lines changed

content/zh-cn/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch.md

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,12 +585,12 @@ And you can see the `retainKeys` strategy in the
585585
`retainKey` 策略。
586586

587587
<!--
588-
## Alternate forms of the kubectl patch command
588+
### Alternate forms of the kubectl patch command
589589
590590
The `kubectl patch` command takes YAML or JSON. It can take the patch as a file or
591591
directly on the command line.
592592
-->
593-
## kubectl patch 命令的其他形式 {#alternate-forms-of-the-kubectl-patch-command}
593+
### kubectl patch 命令的其他形式 {#alternate-forms-of-the-kubectl-patch-command}
594594

595595
`kubectl patch` 命令使用 YAML 或 JSON。它可以接受以文件形式提供的补丁,也可以接受直接在命令行中给出的补丁。
596596

@@ -628,6 +628,128 @@ kubectl patch deployment patch-demo --patch 'spec:\n template:\n spec:\n cont
628628
kubectl patch deployment patch-demo --patch-file patch-file.json
629629
kubectl patch deployment patch-demo --patch '{"spec": {"template": {"spec": {"containers": [{"name": "patch-demo-ctr-2","image": "redis"}]}}}}'
630630
```
631+
<!--
632+
### Update an object's replica count using `kubectl patch` with `--subresource` {#scale-kubectl-patch}
633+
-->
634+
### 使用 `kubectl patch``--subresource` 更新一个对象的副本数 {#scale-kubectl-patch}
635+
636+
{{< feature-state for_k8s_version="v1.24" state="alpha" >}}
637+
638+
<!--
639+
The flag `--subresource=[subresource-name]` is used with kubectl commands like get, patch,
640+
edit and replace to fetch and update `status` and `scale` subresources of the resources
641+
(applicable for kubectl version v1.24 or more). This flag is used with all the API resources
642+
(built-in and CRs) which has `status` or `scale` subresource. Deployment is one of the
643+
examples which supports these subresources.
644+
645+
Here's a manifest for a Deployment that has two replicas:
646+
-->
647+
使用 kubectl 命令(如 get、patch、edit 和 replace)时带上 `--subresource=[subresource-name]` 标志,
648+
可以获取和更新资源的 `status``scale` 子资源(适用于 kubectl v1.24 或更高版本)。
649+
这个标志可用于带有 `status``scale` 子资源的所有 API 资源 (内置资源和 CR 资源)。
650+
Deployment 是支持这些子资源的其中一个例子。
651+
652+
下面是有两个副本的 Deployment 的清单。
653+
654+
{{< codenew file="application/deployment.yaml" >}}
655+
656+
<!--
657+
Create the Deployment:
658+
-->
659+
创建 Deployment:
660+
661+
```shell
662+
kubectl apply -f https://k8s.io/examples/application/deployment.yaml
663+
```
664+
665+
<!--
666+
View the Pods associated with your Deployment:
667+
-->
668+
查看与 Deployment 关联的 Pod:
669+
670+
```shell
671+
kubectl get pods -l app=nginx
672+
```
673+
674+
<!--
675+
In the output, you can see that Deployment has two Pods. For example:
676+
-->
677+
在输出中,你可以看到此 Deployment 有两个 Pod。例如:
678+
679+
```
680+
NAME READY STATUS RESTARTS AGE
681+
nginx-deployment-7fb96c846b-22567 1/1 Running 0 47s
682+
nginx-deployment-7fb96c846b-mlgns 1/1 Running 0 47s
683+
```
684+
685+
<!--
686+
Now, patch that Deployment with `--subresource=[subresource-name]` flag:
687+
-->
688+
现在用 `--subresource=[subresource-name]` 标志修补此 Deployment:
689+
690+
```shell
691+
kubectl patch deployment nginx-deployment --subresource='scale' --type='merge' -p '{"spec":{"replicas":3}}'
692+
```
693+
694+
<!--
695+
The output is:
696+
-->
697+
输出为:
698+
699+
```shell
700+
scale.autoscaling/nginx-deployment patched
701+
```
702+
703+
<!--
704+
View the Pods associated with your patched Deployment:
705+
-->
706+
查看与你所修补的 Deployment 关联的 Pod:
707+
708+
```shell
709+
kubectl get pods -l app=nginx
710+
```
711+
712+
<!--
713+
In the output, you can see one new pod is created, so now you have 3 running pods.
714+
-->
715+
在输出中,你可以看到一个新的 Pod 被创建,因此现在你有 3 个正在运行的 Pod。
716+
717+
```
718+
NAME READY STATUS RESTARTS AGE
719+
nginx-deployment-7fb96c846b-22567 1/1 Running 0 107s
720+
nginx-deployment-7fb96c846b-lxfr2 1/1 Running 0 14s
721+
nginx-deployment-7fb96c846b-mlgns 1/1 Running 0 107s
722+
```
723+
724+
<!--
725+
View the patched Deployment:
726+
-->
727+
查看所修补的 Deployment:
728+
729+
```shell
730+
kubectl get deployment nginx-deployment -o yaml
731+
```
732+
733+
```yaml
734+
...
735+
spec:
736+
replicas: 3
737+
...
738+
status:
739+
...
740+
availableReplicas: 3
741+
readyReplicas: 3
742+
replicas: 3
743+
```
744+
745+
{{< note >}}
746+
<!--
747+
If you run `kubectl patch` and specify `--subresource` flag for resource that doesn't support that
748+
particular subresource, the API server returns a 404 Not Found error.
749+
-->
750+
如果你运行 `kubectl patch` 并指定 `--subresource` 标志时,所针对的是不支持特定子资源的资源,
751+
则 API 服务器会返回一个 404 Not Found 错误。
752+
{{< /note >}}
631753

632754
<!--
633755
## Summary

0 commit comments

Comments
 (0)