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


   
[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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值