Files
website/content/zh/docs/tasks/inject-data-application/define-command-argument-container.md
T
Zach Corleissen abcee2dccd Update localization guidelines (#10485)
* Update localization guidelines for language labels

Continuing work

Continuing work

Continuing work

More work in progress

Add local OWNERS folders

Add an OWNERS file to Chinese

Remove shortcode for repos

Add Japanese

Alphabetize languages, change weights accordingly

More updates

Add Korean in Korean

Add English to languageName

Feedback from gochist

Move Chinese content from cn/ to zh/

Move OWNERS from cn/ to zh/

Resolve merge conflicts by updating from master

Add files back in to prep for resolution

After rebase on upstream/master, remove files

Review and update localization guidelines

Feedback from gochist, tnir, cstoku

Add a trailing newline to content/ja/OWNERS

Add a trailing newline to content/zh/OWNERS

Drop requirement for GH repo project

Clarify language about forks/branches

Edits and typos

Remove a shortcode specific to a multi-repo language setup

Update aliases and owners

Add explicit OWNERS for content/en

Migrate content from Chinese repo, update regex in config.toml

Remove untranslated strings

Add trailing newline to content/en/OWNERS

Add trailing newlines to OWNERS files

add Jaguar project description (#10433)

* add Jaguar project description

[Jaguar](https://gitlab.com/sdnlab/jaguar) is an open source solution for Kubernetes's network based on OpenDaylight.
Jaguar provides overlay network using vxlan and Jaguar CNIPlugin provides one IP address per pod.

* Minor newline tweak

blog post for azure vmss (#10538)

Add microk8s to pick-right-solution.md (#10542)

* Add microk8s to pick-right-solution.md

Microk8s is a single-command installation of upstream Kubernetes on any Linux and should be included in the list of local-machine solutions.

* capitalized Istio

Add microk8s to foundational.md (#10543)

* Add microk8s to foundational.md

Adding microk8s as credible and stable alternative to get started with Kubernetes on a local machine. This is especially attractive for those not wanting to incur the overhead of running a VM for a local cluster.

* Update foundational.md

Thank you for your suggestions! LMK if this works now?

* Rewrote first paragraph

And included a bullet list of features of microk8s

* Copyedit

fix typo (#10545)

Fix the kubectl subcommands links. (#10550)

Signed-off-by: William Zhang <warmchang@outlook.com>

Fix command issue (#10515)

Signed-off-by: mooncake <xcoder@tenxcloud.com>

remove imported community files per issue 10184 (#10501)

networking.md: Markdown fix (#10498)

Fix front matter, federation command-line tools (#10500)

Clean up glossary entry (#10399)

update slack link (#10536)

typo in StatefulSet docs (#10558)

fix discription about horizontal pod autoscale (#10557)

Remove redundant symbols (#10556)

Fix issue #10520 (#10554)

Signed-off-by: William Zhang <warmchang@outlook.com>

Update api-concepts.md (#10534)

Revert "Fix command issue (#10515)"

This reverts commit c02a7fb9f9.

Update memory-constraint-namespace.md (#10530)

update memory request to 100MiB corresponding the yaml content

Blog: Introducing Volume Snapshot Alpha for Kubernetes (#10562)

* blog post for azure vmss

* snapshot blog post

Resolve merge conflicts in OWNERS*

Minor typo fix (#10567)

Not sure what's supposed to be here, proposing removing it.

* Feedback from gochist

Tweaks to feedback

* Feedback from ClaudiaJKang
2018-10-12 14:25:01 -07:00

5.0 KiB
Raw 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配置文件中设置了一个命令与两个入参:

{{< code file="commands.yaml" >}}

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

    kubectl create -f https://k8s.io/docs/tasks/inject-data-application/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中对应的字段名称。

Description Docker field name Kubernetes field name
The command run by the container Entrypoint command
The arguments passed to the command Cmd args

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

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

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

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

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

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

Image Entrypoint Image Cmd Container command Container args Command run
[/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 %}}