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

打印 上一主题 下一主题

主题 1444|帖子 1444|积分 4347

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
概述

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查看对应的资源信息
示例:
  1. [root@master01 ~]# kubectl explain pod.spec.containers.command
  2. KIND:     Pod
  3. VERSION:  v1
  4. FIELD:    command <[]string>
  5. DESCRIPTION:
  6.      Entrypoint array. Not executed within a shell. The container image's
  7.      ENTRYPOINT is used if this is not provided. Variable references $(VAR_NAME)
  8.      are expanded using the container's environment. If a variable cannot be
  9.      resolved, the reference in the input string will be unchanged. Double $$
  10.      are reduced to a single $, which allows for escaping the $(VAR_NAME)
  11.      syntax: i.e. "$$(VAR_NAME)" will produce the string literal "$(VAR_NAME)".
  12.      Escaped references will never be expanded, regardless of whether the
  13.      variable exists or not. Cannot be updated. More info:
  14.      https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell
复制代码
args

args功能同Dockerfile中的CMD指令,用于为command指定的命令提供参数。如果command没有指定,则args中的参数将作为基础镜像中默认命令的参数,也就是ENTRYPONIT指令的参数。
可以通过kubectl explain pod.spec.containers.args查看对应的资源信息
示例:
  1. [root@master01 ~]# kubectl explain pod.spec.containers.args
  2. KIND:     Pod
  3. VERSION:  v1
  4. FIELD:    args <[]string>
  5. DESCRIPTION:
  6.      Arguments to the entrypoint. The container image's CMD is used if this is
  7.      not provided. Variable references $(VAR_NAME) are expanded using the
  8.      container's environment. If a variable cannot be resolved, the reference in
  9.      the input string will be unchanged. Double $$ are reduced to a single $,
  10.      which allows for escaping the $(VAR_NAME) syntax: i.e. "$$(VAR_NAME)" will
  11.      produce the string literal "$(VAR_NAME)". Escaped references will never be
  12.      expanded, regardless of whether the variable exists or not. Cannot be
  13.      updated. More info:
  14.      https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell
复制代码
示例
  1. # 定义资源清单
  2. [root@master01 ~/pod]# cat command-pod.yaml
  3. apiVersion: v1
  4. kind: Pod
  5. metadata:
  6.   name: command-demo
  7.   labels:
  8.     purpose: demonstrate-command
  9. spec:
  10.   containers:
  11.   - name: command-demo-container
  12.     image: debian
  13.     command: ["printenv"]
  14.     args: ["HOSTNAME", "KUBERNETES_PORT"]
  15.   restartPolicy: OnFailure
  16. # 创建pod
  17. [root@master01 ~/pod]# kubectl apply -f command-pod.yaml
  18. pod/command-demo created
  19. # 查看Pod日志打印信息
  20. [root@master01 ~/pod]# kubectl logs command-demo
  21. command-demo
  22. tcp://10.96.0.1:443
复制代码
使用注意事项


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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

大连全瓷种植牙齿制作中心

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表