Files
website/content/zh/docs/tasks/inject-data-application/define-command-argument-container.md
Qiming 9bf026e951 Clean zh/docs directory for dangling YAML files (#17341)
There are many YAML manifests sneaking into the `zh/docs/` directory.
These files should go to the `zh/examples` directory instead. Having
these "garbage" files (not referenced anywhere) is creating confusion
for the release meister when merging branches. For example, some YAML
files found in the 'master' branch are no longer there in the
release-1.16 branch. It is tedious, if possible at all, to solve all
this kind of conflicts during a rebase.

The PR cleanses the zh/docs directory for all dangling YAML files.
2019-11-04 08:24:48 +08:00

4.8 KiB
Raw Permalink Blame History

title, content_template
title content_template
为容器设置启动时要执行的命令及其入参 templates/task

{{% capture overview %}}

本页将展示如何为Kubernetes Pod下的容器设置启动时要执行的命令及其入参。

{{% /capture %}}

{{% capture prerequisites %}}

{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}

{{% /capture %}}

{{% capture steps %}}

创建Pod时为其下的容器设置启动时要执行的命令及其入参

创建Pod时,可以为其下的容器设置启动时要执行的命令及其入参。如果要设置命令,就 填写在配置文件的command字段下,如果要设置命令的入参,就填写在配置文件的args 字段下。一旦Pod创建完成,该命令及其入参就无法再进行更改了。

如果在配置文件中设置了容器启动时要执行的命令及其入参,那么容器镜像中自带的命令 与入参将会被覆盖而不再执行。如果配置文件中只是设置了入参,却没有设置其对应的命 令,那么容器镜像中自带的命令会使用该新入参作为其执行时的入参。

本示例中,将创建一个只包含单个容器的Pod。在Pod配置文件中设置了一个命令与两个入参:

{{< codenew file="pods/commands.yaml" >}}

  1. 基于 YAML 文件创建一个 Pod

    kubectl apply -f https://k8s.io/examples/pods/commands.yaml
    
  2. 列举运行中的 Pods

    kubectl get pods
    

    查询结果显示在command-demo这个Pod下运行的容器已经启动完成

  3. 如果要获取容器启动时执行命令的输出结果,可以通过Pod的日志进行查看

    kubectl logs command-demo
    

    日志中显示了 HOSTNAME 与 KUBERNETES_PORT 这两个环境变量的值:

    command-demo
    tcp://10.3.240.1:443
    

使用环境变量来设置入参

在上面的示例中,我们直接将一串字符作为命令的入参。除此之外,我们还可以 将环境变量作为命令的入参。

env:
- name: MESSAGE
  value: "hello world"
command: ["/bin/echo"]
args: ["$(MESSAGE)"]

这样一来,我们就可以将那些用来设置环境变量的方法应用于设置命令的入参,其 中包括了ConfigMapsSecrets.

{{< note >}} 注意: 环境变量需要加上括号,类似于"$(VAR)"。这是在commandargs字段使用变量的格式要求。 {{< /note >}}

通过shell来执行命令

有时候,需要通过shell来执行命令。 例如,命令可能由多个命令组合而成,抑或包含 在一个shell脚本中。这时,就可以通过如下方式在shell中执行命令:

command: ["/bin/sh"]
args: ["-c", "while true; do echo hello; sleep 10;done"]

注意

下表给出了Docker 与 Kubernetes中对应的字段名称。

描述 Docker 字段名称 Kubernetes 字段名称
容器运行的命令 Entrypoint command
传递给命令的参数集合 Cmd args

如果要覆盖默认的 Entrypoint 与 Cmd,需要遵循如下规则:

  • 如果在容器配置中没有设置command 或者 args,那么将使用Docker镜像自带的命 令及其入参。

  • 如果在容器配置中只设置了command但是没有设置args,那么容器启动时只会执行该 命令,Docker镜像中自带的命令及其入参会被忽略。

  • 如果在容器配置中只设置了args,那么Docker镜像中自带的命令会使用该新入参作为 其执行时的入参。

  • 如果在容器配置中同时设置了commandargs,那么Docker镜像中自带的命令及 其入参会被忽略。容器启动时只会执行配置中设置的命令,并使用配置中设置的入参作为 命令的入参。

下表涵盖了各类设置场景:

镜像 Entrypoint 镜像 Cmd 容器命令 容器参数 运行的命令
[/ep-1] [foo bar] <not set> <not set> [ep-1 foo bar]
[/ep-1] [foo bar] [/ep-2] <not set> [ep-2]
[/ep-1] [foo bar] <not set> [zoo boo] [ep-1 zoo boo]
[/ep-1] [foo bar] [/ep-2] [zoo boo] [ep-2 zoo boo]

{{% /capture %}}

{{% capture whatsnext %}}

{{% /capture %}}