网站首页 全球最实用的IT互联网站!

人工智能P2P分享Wind搜索发布信息网站地图标签大全

当前位置:诺佳网 > 软件工程 > 其他技术区 > Kubernetes >

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

时间:2025-05-02 13:41

人气:

作者:admin

标签:

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

概述

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查看对应的资源信息
示例:

[root@master01 ~]# 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

args

args功能同Dockerfile中的CMD指令,用于为command指定的命令提供参数。如果command没有指定,则args中的参数将作为基础镜像中默认命令的参数,也就是ENTRYPONIT指令的参数。

可以通过kubectl explain pod.spec.containers.args查看对应的资源信息
示例:

[root@master01 ~]# 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

示例

# 定义资源清单
[root@master01 ~/pod]# 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
[root@master01 ~/pod]# kubectl apply -f command-pod.yaml
pod/command-demo created

# 查看Pod日志打印信息
[root@master01 ~/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参数

本文来自博客园,作者:huangSir-devops,转载请注明原文链接:https://www.cnblogs.com/huangSir-devops/p/18856547,微信Vac6666666,欢迎交流

温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信