Skip to content

Commit 61c1cd6

Browse files
committed
[zh] Translate imperative-command task
1 parent c7ab385 commit 61c1cd6

File tree

1 file changed

+294
-0
lines changed

1 file changed

+294
-0
lines changed
Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
---
2+
title: 使用指令式命令管理 Kubernetes 对象
3+
content_type: task
4+
weight: 30
5+
---
6+
<!--
7+
title: Managing Kubernetes Objects Using Imperative Commands
8+
content_type: task
9+
weight: 30
10+
-->
11+
<!-- overview -->
12+
13+
<!--
14+
Kubernetes objects can quickly be created, updated, and deleted directly using
15+
imperative commands built into the `kubectl` command-line tool. This document
16+
explains how those commands are organized and how to use them to manage live objects.
17+
-->
18+
使用构建在 `kubectl` 命令行工具中的指令式命令可以直接快速创建、更新和删除
19+
Kubernetes 对象。本文档解释这些命令的组织方式以及如何使用它们来管理现时对象。
20+
21+
## {{% heading "prerequisites" %}}
22+
23+
<!--
24+
Install [`kubectl`](/docs/tasks/tools/install-kubectl/).
25+
-->
26+
安装[`kubectl`](/zh/docs/tasks/tools/install-kubectl/)
27+
28+
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
29+
30+
<!-- steps -->
31+
32+
<!--
33+
## Trade-offs
34+
35+
The `kubectl` tool supports three kinds of object management:
36+
37+
* Imperative commands
38+
* Imperative object configuration
39+
* Declarative object configuration
40+
41+
See [Kubernetes Object Management](/docs/concepts/overview/working-with-objects/object-management/)
42+
for a discussion of the advantages and disadvantage of each kind of object management.
43+
-->
44+
## 权衡取舍 {#trade-offs}
45+
46+
`kubectl` 工具能够支持三种对象管理方式:
47+
48+
* 指令式命令
49+
* 指令式对象配置
50+
* 声明式对象配置
51+
52+
关于每种对象管理的优缺点的讨论,可参见
53+
[Kubernetes 对象管理](/zh/docs/concepts/overview/working-with-objects/object-management/)
54+
55+
<!--
56+
## How to create objects
57+
58+
The `kubectl` tool supports verb-driven commands for creating some of the most common
59+
object types. The commands are named to be recognizable to users unfamiliar with
60+
the Kubernetes object types.
61+
62+
- `run`: Create a new Deployment object to run Containers in one or more Pods.
63+
- `expose`: Create a new Service object to load balance traffic across Pods.
64+
- `autoscale`: Create a new Autoscaler object to automatically horizontally scale a controller, such as a Deployment.
65+
-->
66+
## 如何创建对象 {#how-to-create-objects}
67+
68+
`kubectl` 工具支持动词驱动的命令,用来创建一些最常见的对象类别。
69+
命令的名称设计使得不熟悉 Kubernetes 对象类型的用户也能做出判断。
70+
71+
- `run`:创建一个新的 Deployment 对象来在一个或多个 Pod 中运行 Container。
72+
- `expose`:创建一个新的 Service 对象为若干 Pod 提供流量负载均衡。
73+
- `autoscale`:创建一个新的 Autoscaler 对象来自动对某控制器(如 Deployment)
74+
执行水平扩缩。
75+
76+
<!--
77+
The `kubectl` tool also supports creation commands driven by object type.
78+
These commands support more object types and are more explicit about
79+
their intent, but require users to know the type of objects they intend
80+
to create.
81+
82+
- `create <objecttype> [<subtype>] <instancename>`
83+
-->
84+
`kubectl` 命令也支持一些对象类型驱动的创建命令。
85+
这些命令可以支持更多的对象类别,并且在其动机上体现得更为明显,不过要求
86+
用户了解它们所要创建的对象的类别。
87+
88+
- `create <对象类别> [<子类别>] <实例名称>`
89+
90+
<!--
91+
Some objects types have subtypes that you can specify in the `create` command.
92+
For example, the Service object has several subtypes including ClusterIP,
93+
LoadBalancer, and NodePort. Here's an example that creates a Service with
94+
subtype NodePort:
95+
96+
```shell
97+
kubectl create service nodeport <myservicename>
98+
```
99+
-->
100+
某些对象类别拥有自己的子类别,可以在 `create` 命令中设置。
101+
例如,Service 对象有 ClusterIP、LoadBalancer 和 NodePort 三种子类别。
102+
下面是一个创建 NodePort 子类别的 Service 的示例:
103+
104+
```shell
105+
kubectl create service nodeport <服务名称>
106+
```
107+
108+
<!--
109+
In the preceding example, the `create service nodeport` command is called
110+
a subcommand of the `create service` command.
111+
112+
You can use the `-h` flag to find the arguments and flags supported by
113+
a subcommand:
114+
-->
115+
在前述示例中,`create service nodeport` 命令也称作 `create service`
116+
命令的子命令:
117+
118+
```shell
119+
kubectl create service nodeport -h
120+
```
121+
122+
<!--
123+
## How to update objects
124+
125+
The `kubectl` command supports verb-driven commands for some common update operations.
126+
These commands are named to enable users unfamiliar with Kubernetes
127+
objects to perform updates without knowing the specific fields
128+
that must be set:
129+
130+
- `scale`: Horizontally scale a controller to add or remove Pods by updating the replica count of the controller.
131+
- `annotate`: Add or remove an annotation from an object.
132+
- `label`: Add or remove a label from an object.
133+
-->
134+
## 如何更新对象 {#how-to-update-objects}
135+
136+
`kubectl` 命令也支持一些动词驱动的命令,用来执行一些常见的更新操作。
137+
这些命令的设计是为了让一些不了解 Kubernetes 对象的用户也能执行更新操作,
138+
但不需要了解哪些字段必须设置:
139+
140+
- `scale`:对某控制器进行水平扩缩以便通过更新控制器的副本个数来添加或删除 Pod。
141+
- `annotate`:为对象添加或删除注解。
142+
- `label`:为对象添加或删除标签。
143+
144+
<!--
145+
The `kubectl` command also supports update commands driven by an aspect of the object.
146+
Setting this aspect may set different fields for different object types:
147+
148+
- `set` `<field>`: Set an aspect of an object.
149+
-->
150+
`kubectl` 命令也支持由对象的某一方面来驱动的更新命令。
151+
设置对象的这一方面可能对不同类别的对象意味着不同的字段:
152+
153+
- `set <字段>`:设置对象的某一方面。
154+
155+
<!--
156+
In Kubernetes version 1.5, not every verb-driven command has an associated aspect-driven command.
157+
-->
158+
{{< note >}}
159+
在 Kubernetes 1.5 版本中,并非所有动词驱动的命令都有对应的方面驱动的命令。
160+
{{< /note >}}
161+
162+
<!--
163+
The `kubectl` tool supports these additional ways to update a live object directly,
164+
however they require a better understanding of the Kubernetes object schema.
165+
166+
- `edit`: Directly edit the raw configuration of a live object by opening its configuration in an editor.
167+
- `patch`: Directly modify specific fields of a live object by using a patch string.
168+
For more details on patch strings, see the patch section in
169+
[API Conventions](https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#patch-operations).
170+
-->
171+
`kubectl` 工具支持以下额外的方式用来直接更新现时对象,不过这些操作要求
172+
用户对 Kubernetes 对象的模式定义有很好的了解:
173+
174+
- `edit`:通过在编辑器中打开现时对象的配置,直接编辑其原始配置。
175+
- `patch`:通过使用补丁字符串(Patch String)直接更改某现时对象的的特定字段。
176+
关于补丁字符串的更详细信息,参见
177+
[API 约定](https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#patch-operations)
178+
的 patch 节。
179+
180+
<!--
181+
## How to delete objects
182+
183+
You can use the `delete` command to delete an object from a cluster:
184+
185+
- `delete <type>/<name>`
186+
-->
187+
## 如何删除对象 {#how-to-delete-objects}
188+
189+
你可以使用 `delete` 命令从集群中删除一个对象:
190+
191+
- `delete <类别>/<名称>`
192+
193+
<!--
194+
You can use `kubectl delete` for both imperative commands and imperative object
195+
configuration. The difference is in the arguments passed to the command. To use
196+
`kubectl delete` as an imperative command, pass the object to be deleted as
197+
an argument. Here's an example that passes a Deployment object named nginx:
198+
-->
199+
你可以使用 `kubectl delete` 来执行指令式命令或者指令式对象配置。不同之处在于
200+
传递给命令的参数。要将 `kubectl delete` 作为指令式命令使用,将要删除的对象作为
201+
参数传递给它。下面是一个删除名为 `nginx` 的 Deployment 对象的命令:
202+
203+
```shell
204+
kubectl delete deployment/nginx
205+
```
206+
207+
<!--
208+
## How to view an object
209+
210+
{{< comment >}}
211+
TODO(pwittrock): Uncomment this when implemented.
212+
213+
You can use `kubectl view` to print specific fields of an object.
214+
215+
- `view`: Prints the value of a specific field of an object.
216+
217+
{{< /comment >}}
218+
-->
219+
## 如何查看对象 {#how-to-view-an-object}
220+
221+
用来打印对象信息的命令有好几个:
222+
223+
- `get`:打印匹配到的对象的基本信息。使用 `get -h` 可以查看选项列表。
224+
- `describe`:打印匹配到的对象的详细信息的汇集版本。
225+
- `logs`:打印 Pod 中运行的容器的 stdout 和 stderr 输出。
226+
227+
<!--
228+
## Using `set` commands to modify objects before creation
229+
230+
There are some object fields that don't have a flag you can use
231+
in a `create` command. In some of those cases, you can use a combination of
232+
`set` and `create` to specify a value for the field before object
233+
creation. This is done by piping the output of the `create` command to the
234+
`set` command, and then back to the `create` command. Here's an example:
235+
-->
236+
## 使用 `set` 命令在创建对象之前修改对象
237+
238+
有些对象字段在 `create` 命令中没有对应的标志。在这些场景中,
239+
你可以使用 `set``create` 命令的组合来在对象创建之前设置字段值。
240+
这是通过将 `create` 命令的输出用管道方式传递给 `set` 命令来实现的,
241+
最后执行 `create` 命令来创建对象。下面是一个例子:
242+
243+
```sh
244+
kubectl create service clusterip my-svc --clusterip="None" -o yaml --dry-run=client | kubectl set selector --local -f - 'environment=qa' -o yaml | kubectl create -f -
245+
```
246+
247+
<!--
248+
1. The `kubectl create service -o yaml --dry-run=client` command creates the configuration for the Service, but prints it to stdout as YAML instead of sending it to the Kubernetes API server.
249+
1. The `kubectl set selector --local -f - -o yaml` command reads the configuration from stdin, and writes the updated configuration to stdout as YAML.
250+
1. The `kubectl create -f -` command creates the object using the configuration provided via stdin.
251+
-->
252+
1. 命令 `kubectl create service -o yaml --dry-run=client` 创建 Service 的配置,但
253+
将其以 YAML 格式在标准输出上打印而不是发送给 API 服务器。
254+
1. 命令 `kubectl set selector --local -f - -o yaml` 从标准输入读入配置,并将更新后的
255+
配置以 YAML 格式输出到标准输出。
256+
1. 命令 `kubectl create -f -` 使用标准输入上获得的配置创建对象。
257+
258+
<!--
259+
## Using `--edit` to modify objects before creation
260+
261+
You can use `kubectl create --edit` to make arbitrary changes to an object
262+
before it is created. Here's an example:
263+
-->
264+
## 在创建之前使用 `--edit` 更改对象
265+
266+
你可以用 `kubectl create --edit` 来在对象被创建之前执行任意的变更。
267+
下面是一个例子:
268+
269+
```sh
270+
kubectl create service clusterip my-svc --clusterip="None" -o yaml --dry-run=client > /tmp/srv.yaml
271+
kubectl create --edit -f /tmp/srv.yaml
272+
```
273+
274+
<!--
275+
1. The `kubectl create service` command creates the configuration for the Service and saves it to `/tmp/srv.yaml`.
276+
1. The `kubectl create --edit` command opens the configuration file for editing before it creates the object.
277+
-->
278+
1. 命令 `kubectl create service` 创建 Service 的配置并将其保存到
279+
`/tmp/srv.yaml` 文件。
280+
1. 命令 `kubectl create --edit` 在创建 Service 对象打开其配置文件进行编辑。
281+
282+
## {{% heading "whatsnext" %}}
283+
284+
<!--
285+
* [Managing Kubernetes Objects Using Object Configuration (Imperative)](/docs/tasks/manage-kubernetes-objects/imperative-config/)
286+
* [Managing Kubernetes Objects Using Object Configuration (Declarative)](/docs/tasks/manage-kubernetes-objects/declarative-config/)
287+
* [Kubectl Command Reference](/docs/reference/generated/kubectl/kubectl-commands/)
288+
* [Kubernetes API Reference](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/)
289+
-->
290+
* [使用指令式对象配置管理 Kubernetes 对象](/zh/docs/tasks/manage-kubernetes-objects/imperative-config/)
291+
* [使用声明式对象配置管理 Kubernetes 对象](/zh/docs/tasks/manage-kubernetes-objects/declarative-config/)
292+
* [Kubectl 命令参考](/docs/reference/generated/kubectl/kubectl-commands/)
293+
* [Kubernetes API 参考](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/)
294+

0 commit comments

Comments
 (0)