K8s新手系列之Pod容器中的command和args指令
<h2 id="概述">概述</h2><p>command和args是containers下的两个指令,类似Dockerfile中的ENTRYPONIT和CMD指令。</p>
<p>官方文档地址:https://kubernetes.io/zh-cn/docs/tasks/inject-data-application/define-command-argument-container/</p>
<h2 id="command">command</h2>
<p>command功能同Dockerfile中的ENTRYPONIT指令,用于指定容器启动时要执行的命令。如果不设置command,容器将使用基础镜像中默认的启动命令,也就是ENTRYPONIT指定的启动命令。</p>
<p>可以通过<code>kubectl explain pod.spec.containers.command</code>查看对应的资源信息<br>
示例:</p>
<pre><code># kubectl explain pod.spec.containers.command
KIND: Pod
VERSION:v1
FIELD: command <[]string>
DESCRIPTION:
Entrypoint array. Not executed within a shell. The container image's
ENTRYPOINT is used if this is not provided. Variable references $(VAR_NAME)
are expanded using the container's environment. If a variable cannot be
resolved, the reference in the input string will be unchanged. Double $$
are reduced to a single $, which allows for escaping the $(VAR_NAME)
syntax: i.e. "$$(VAR_NAME)" will produce the string literal "$(VAR_NAME)".
Escaped references will never be expanded, regardless of whether the
variable exists or not. Cannot be updated. More info:
https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell
</code></pre>
<h2 id="args">args</h2>
<p>args功能同Dockerfile中的CMD指令,用于为command指定的命令提供参数。如果command没有指定,则args中的参数将作为基础镜像中默认命令的参数,也就是ENTRYPONIT指令的参数。</p>
<p>可以通过<code>kubectl explain pod.spec.containers.args</code>查看对应的资源信息<br>
示例:</p>
<pre><code># kubectl explain pod.spec.containers.args
KIND: Pod
VERSION:v1
FIELD: args <[]string>
DESCRIPTION:
Arguments to the entrypoint. The container image's CMD is used if this is
not provided. Variable references $(VAR_NAME) are expanded using the
container's environment. If a variable cannot be resolved, the reference in
the input string will be unchanged. Double $$ are reduced to a single $,
which allows for escaping the $(VAR_NAME) syntax: i.e. "$$(VAR_NAME)" will
produce the string literal "$(VAR_NAME)". Escaped references will never be
expanded, regardless of whether the variable exists or not. Cannot be
updated. More info:
https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell
</code></pre>
<h2 id="示例">示例</h2>
<pre><code># 定义资源清单
# cat command-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: command-demo
labels:
purpose: demonstrate-command
spec:
containers:
- name: command-demo-container
image: debian
command: ["printenv"]
args: ["HOSTNAME", "KUBERNETES_PORT"]
restartPolicy: OnFailure
# 创建pod
# kubectl apply -f command-pod.yaml
pod/command-demo created
# 查看Pod日志打印信息
# kubectl logs command-demo
command-demo
tcp://10.96.0.1:443
</code></pre>
<h2 id="使用注意事项">使用注意事项</h2>
<ul>
<li>
<p>如果command和args均没有写,那么用Dockerfile的配置。</p>
</li>
<li>
<p>如果command写了,但args没有写,那么Dockerfile默认的配置会被忽略,执行输入的command</p>
</li>
<li>
<p>如果command没写,但args写了,那么Dockerfile中配置的ENTRYPOINT的命令会被执行,使用当前args的参数</p>
</li>
<li>
<p>如果command和args都写了,那么Dockerfile的配置被忽略,执行command并追加上args参数</p>
</li>
</ul>
</div>
<div id="MySignature" role="contentinfo">
<p>本文来自博客园,作者:huangSir-devops,转载请注明原文链接:https://www.cnblogs.com/huangSir-devops/p/18856547,微信Vac6666666,欢迎交流</p><br><br>
来源:https://www.cnblogs.com/huangSir-devops/p/18856547
頁:
[1]