羡渥蛛 发表于 2025-6-3 10:43:12

K8s新手系列之Pod容器中的command和args指令

概述

command和args是containers下的两个指令,类似Dockerfile中的ENTRYPONIT和CMD指令。
官方文档地址:https://kubernetes.io/zh-cn/docs/tasks/inject-data-application/define-command-argument-container/
command

command功能同Dockerfile中的ENTRYPONIT指令,用于指定容器启动时要执行的命令。如果不设置command,容器将使用基础镜像中默认的启动命令,也就是ENTRYPONIT指定的启动命令。
可以通过kubectl explain pod.spec.containers.command查看对应的资源信息
示例:
# 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-shellargs

args功能同Dockerfile中的CMD指令,用于为command指定的命令提供参数。如果command没有指定,则args中的参数将作为基础镜像中默认命令的参数,也就是ENTRYPONIT指令的参数。
可以通过kubectl explain pod.spec.containers.args查看对应的资源信息
示例:
# 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示例

# 定义资源清单
# 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使用注意事项


[*]如果command和args均没有写,那么用Dockerfile的配置。
[*]如果command写了,但args没有写,那么Dockerfile默认的配置会被忽略,执行输入的command
[*]如果command没写,但args写了,那么Dockerfile中配置的ENTRYPOINT的命令会被执行,使用当前args的参数
[*]如果command和args都写了,那么Dockerfile的配置被忽略,执行command并追加上args参数

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: K8s新手系列之Pod容器中的command和args指令