|
| 1 | +--- |
| 2 | +title: 为容器设置启动时要执行的命令及其入参 |
| 3 | +--- |
| 4 | + |
| 5 | +{% capture overview %} |
| 6 | + |
| 7 | +本页将展示如何为Kubernetes Pod下的容器设置启动时要执行的命令及其入参。 |
| 8 | + |
| 9 | +{% endcapture %} |
| 10 | + |
| 11 | + |
| 12 | +{% capture prerequisites %} |
| 13 | + |
| 14 | +{% include task-tutorial-prereqs.md %} |
| 15 | + |
| 16 | +{% endcapture %} |
| 17 | + |
| 18 | + |
| 19 | +{% capture steps %} |
| 20 | + |
| 21 | +## 创建Pod时为其下的容器设置启动时要执行的命令及其入参 |
| 22 | + |
| 23 | +创建Pod时,可以为其下的容器设置启动时要执行的命令及其入参。如果要设置命令,就 |
| 24 | +填写在配置文件的`command`字段下,如果要设置命令的入参,就填写在配置文件的`args |
| 25 | +`字段下。一旦Pod创建完成,该命令及其入参就无法再进行更改了。 |
| 26 | + |
| 27 | +如果在配置文件中设置了容器启动时要执行的命令及其入参,那么容器镜像中自带的命令 |
| 28 | +与入参将会被覆盖而不再执行。如果配置文件中只是设置了入参,却没有设置其对应的命 |
| 29 | +令,那么容器镜像中自带的命令会使用该新入参作为其执行时的入参。 |
| 30 | + |
| 31 | +本示例中,将创建一个只包含单个容器的Pod。在Pod配置文件中设置了一个命令与两个入参: |
| 32 | + |
| 33 | +{% include code.html language="yaml" file="commands.yaml" ghlink="/cn/docs/tasks/inject-data-application/commands.yaml" %} |
| 34 | + |
| 35 | +1. 基于YAML文件创建一个Pod: |
| 36 | + |
| 37 | + kubectl create -f https://k8s.io/docs/tasks/inject-data-application/commands.yaml |
| 38 | + |
| 39 | +1. 获取一下当前正在运行的Pods信息: |
| 40 | + |
| 41 | + kubectl get pods |
| 42 | + |
| 43 | + 查询结果显示在command-demo这个Pod下运行的容器已经启动完成 |
| 44 | + |
| 45 | +1. 如果要获取容器启动时执行命令的输出结果,可以通过Pod的日志进行查看 |
| 46 | + |
| 47 | + kubectl logs command-demo |
| 48 | + |
| 49 | + 日志中显示了HOSTNAME 与KUBERNETES_PORT 这两个环境变量的值: |
| 50 | + |
| 51 | + command-demo |
| 52 | + tcp://10.3.240.1:443 |
| 53 | + |
| 54 | +## 使用环境变量来设置入参 |
| 55 | + |
| 56 | +在上面的示例中,我们直接将一串字符作为命令的入参。除此之外,我们还可以 |
| 57 | +将环境变量作为命令的入参。 |
| 58 | + |
| 59 | + env: |
| 60 | + - name: MESSAGE |
| 61 | + value: "hello world" |
| 62 | + command: ["/bin/echo"] |
| 63 | + args: ["$(MESSAGE)"] |
| 64 | + |
| 65 | +这样一来,我们就可以将那些用来设置环境变量的方法应用于设置命令的入参,其 |
| 66 | +中包括了[ConfigMaps](/docs/tasks/configure-pod-container/configmap/) |
| 67 | +与 |
| 68 | +[Secrets](/docs/concepts/configuration/secret/). |
| 69 | + |
| 70 | +**注意:** 环境变量需要加上括号,类似于`"$(VAR)"`。这是在`command` |
| 71 | +或 `args`字段使用变量的格式要求。 |
| 72 | +{: .note} |
| 73 | + |
| 74 | +## 通过shell来执行命令 |
| 75 | + |
| 76 | +有时候,需要通过shell来执行命令。 例如,命令可能由多个命令组合而成,抑或包含 |
| 77 | +在一个shell脚本中。这时,就可以通过如下方式在shell中执行命令: |
| 78 | + |
| 79 | + command: ["/bin/sh"] |
| 80 | + args: ["-c", "while true; do echo hello; sleep 10;done"] |
| 81 | + |
| 82 | +## 注意 |
| 83 | + |
| 84 | +下表给出了Docker 与 Kubernetes中对应的字段名称。 |
| 85 | + |
| 86 | +| Description | Docker field name | Kubernetes field name | |
| 87 | +|----------------------------------------|------------------------|-----------------------| |
| 88 | +| The command run by the container | Entrypoint | command | |
| 89 | +| The arguments passed to the command | Cmd | args | |
| 90 | + |
| 91 | +如果要覆盖默认的Entrypoint 与 Cmd,需要遵循如下规则: |
| 92 | + |
| 93 | +* 如果在容器配置中没有设置`command` 或者 `args`,那么将使用Docker镜像自带的命 |
| 94 | +令及其入参。 |
| 95 | + |
| 96 | +* 如果在容器配置中只设置了`command`但是没有设置`args`,那么容器启动时只会执行该 |
| 97 | +命令,Docker镜像中自带的命令及其入参会被忽略。 |
| 98 | + |
| 99 | +* 如果在容器配置中只设置了`args`,那么Docker镜像中自带的命令会使用该新入参作为 |
| 100 | +其执行时的入参。 |
| 101 | + |
| 102 | +* 如果在容器配置中同时设置了`command` 与 `args`,那么Docker镜像中自带的命令及 |
| 103 | +其入参会被忽略。容器启动时只会执行配置中设置的命令,并使用配置中设置的入参作为 |
| 104 | +命令的入参。 |
| 105 | + |
| 106 | +下表涵盖了各类设置场景: |
| 107 | + |
| 108 | +| Image Entrypoint | Image Cmd | Container command | Container args | Command run | |
| 109 | +|--------------------|------------------|---------------------|--------------------|------------------| |
| 110 | +| `[/ep-1]` | `[foo bar]` | <not set> | <not set> | `[ep-1 foo bar]` | |
| 111 | +| `[/ep-1]` | `[foo bar]` | `[/ep-2]` | <not set> | `[ep-2]` | |
| 112 | +| `[/ep-1]` | `[foo bar]` | <not set> | `[zoo boo]` | `[ep-1 zoo boo]` | |
| 113 | +| `[/ep-1]` | `[foo bar]` | `[/ep-2]` | `[zoo boo]` | `[ep-2 zoo boo]` | |
| 114 | + |
| 115 | + |
| 116 | +{% endcapture %} |
| 117 | + |
| 118 | +{% capture whatsnext %} |
| 119 | + |
| 120 | +* 获取更多资讯可参考 [containers and commands](/docs/user-guide/containers/). |
| 121 | +* 获取更多资讯可参考 [configuring pods and containers](/docs/tasks/). |
| 122 | +* 获取更多资讯可参考 [running commands in a container](/docs/tasks/debug-application-cluster/get-shell-running-container/). |
| 123 | +* 参考 [Container](/docs/api-reference/{{page.version}}/#container-v1-core). |
| 124 | + |
| 125 | +{% endcapture %} |
| 126 | + |
| 127 | + |
| 128 | +{% include templates/task.md %} |
0 commit comments