Merge pull request #37318 from windsonsea/dcacy

[zh] updated define-command-argument-container.md
pull/37335/head
Kubernetes Prow Robot 2022-10-17 03:13:06 -07:00 committed by GitHub
commit b08d886f24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 18 deletions

View File

@ -15,7 +15,7 @@ weight: 10
This page shows how to define commands and arguments when you run a container
in a {{< glossary_tooltip term_id="pod" >}}.
-->
本页将展示如何为 {{< glossary_tooltip text="Pod" term_id="pod" >}}
本页将展示如何为 {{< glossary_tooltip text="Pod" term_id="pod" >}}
中容器设置启动时要执行的命令及其参数。
## {{% heading "prerequisites" %}}
@ -33,9 +33,11 @@ field in the configuration file. To define arguments for the command, include
the `args` field in the configuration file. The command and arguments that
you define cannot be changed after the Pod is created.
-->
## 创建 Pod 时设置命令及参数
## 创建 Pod 时设置命令及参数 {#define-a-command-and-arguments-when-you-create-a-pod}
创建 Pod 时,可以为其下的容器设置启动时要执行的命令及其参数。如果要设置命令,就填写在配置文件的 `command` 字段下,如果要设置命令的参数,就填写在配置文件的 `args` 字段下。一旦 Pod 创建完成,该命令及其参数就无法再进行更改了。
创建 Pod 时,可以为其下的容器设置启动时要执行的命令及其参数。如果要设置命令,就填写在配置文件的
`command` 字段下,如果要设置命令的参数,就填写在配置文件的 `args` 字段下。
一旦 Pod 创建完成,该命令及其参数就无法再进行更改了。
<!--
The command and arguments that you define in the configuration file
@ -43,21 +45,21 @@ override the default command and arguments provided by the container image.
If you define args, but do not define a command, the default command is used
with your new arguments.
-->
如果在配置文件中设置了容器启动时要执行的命令及其参数,那么容器镜像中自带的命令与参数将会被覆盖而不再执行。如果配置文件中只是设置了参数,却没有设置其对应的命令,那么容器镜像中自带的命令会使用该新参数作为其执行时的参数。
如果在配置文件中设置了容器启动时要执行的命令及其参数,那么容器镜像中自带的命令与参数将会被覆盖而不再执行。
如果配置文件中只是设置了参数,却没有设置其对应的命令,那么容器镜像中自带的命令会使用该新参数作为其执行时的参数。
{{< note >}}
<!--
The `command` field corresponds to `entrypoint` in some container runtimes.
-->
{{< note >}}
在有些容器运行时中,`command` 字段对应 `entrypoint`,请参阅下面的
[说明事项](#notes)。
在有些容器运行时中,`command` 字段对应 `entrypoint`,请参阅下面的[说明事项](#notes)。
{{< /note >}}
<!--
In this exercise, you create a Pod that runs one container. The configuration
file for the Pod defines a command and two arguments:
-->
本示例中,将创建一个只包含单个容器的 Pod。在 Pod 配置文件中设置了一个命令与两个参数:
本示例中,将创建一个只包含单个容器的 Pod。在 Pod 配置文件中设置了一个命令与两个参数:
{{< codenew file="pods/commands.yaml" >}}
@ -73,8 +75,8 @@ file for the Pod defines a command and two arguments:
<!--
1. List the running Pods:
-->
2. 获取正在运行的 Pods
2. 获取正在运行的 Pod
```shell
kubectl get pods
```
@ -98,12 +100,12 @@ from the Pod:
The output shows the values of the HOSTNAME and KUBERNETES_PORT environment variables:
-->
日志中显示了 HOSTNAME 与 KUBERNETES_PORT 这两个环境变量的值:
```
command-demo
tcp://10.3.240.1:443
```
<!--
## Use environment variables to define arguments
@ -111,7 +113,7 @@ In the preceding example, you defined the arguments directly by
providing strings. As an alternative to providing strings directly,
you can define arguments by using environment variables:
-->
## 使用环境变量来设置参数
## 使用环境变量来设置参数 {#use-env-var-to-define-arguments}
在上面的示例中,我们直接将一串字符作为命令的参数。除此之外,我们还可以将环境变量作为命令的参数。
@ -131,14 +133,14 @@ and
[Secrets](/docs/concepts/configuration/secret/).
-->
这意味着你可以将那些用来设置环境变量的方法应用于设置命令的参数,其中包括了
[ConfigMaps](/zh-cn/docs/tasks/configure-pod-container/configure-pod-configmap/) 与
[Secrets](/zh-cn/docs/concepts/configuration/secret/)。
[ConfigMap](/zh-cn/docs/tasks/configure-pod-container/configure-pod-configmap/) 与
[Secret](/zh-cn/docs/concepts/configuration/secret/)。
{{< note >}}
<!--
The environment variable appears in parentheses, `"$(VAR)"`. This is
required for the variable to be expanded in the `command` or `args` field.
-->
{{< note >}}
环境变量需要加上括号,类似于 `"$(VAR)"`。这是在 `command``args` 字段使用变量的格式要求。
{{< /note >}}
@ -149,7 +151,7 @@ In some cases, you need your command to run in a shell. For example, your
command might consist of several commands piped together, or it might be a shell
script. To run your command in a shell, wrap it like this:
-->
## 在 Shell 来执行命令
## 在 Shell 来执行命令 {#run-a-command-in-a-shell}
有时候,你需要在 Shell 脚本中运行命令。
例如,你要执行的命令可能由多个命令组合而成,或者它就是一个 Shell 脚本。
@ -160,7 +162,6 @@ command: ["/bin/sh"]
args: ["-c", "while true; do echo hello; sleep 10;done"]
```
## {{% heading "whatsnext" %}}
<!--