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指令,用于指定容器啟動時要執(zhí)行的命令。如果不設(shè)置command,容器將使用基礎(chǔ)鏡像中默認的啟動命令,也就是ENTRYPONIT指定的啟動命令。
可以通過kubectl explain pod.spec.containers.command查看對應(yīng)的資源信息
示例:
[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-shellargs
args功能同Dockerfile中的CMD指令,用于為command指定的命令提供參數(shù)。如果command沒有指定,則args中的參數(shù)將作為基礎(chǔ)鏡像中默認命令的參數(shù),也就是ENTRYPONIT指令的參數(shù)。
可以通過kubectl explain pod.spec.containers.args查看對應(yīng)的資源信息
示例:
[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
# 創(chuàng)建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默認的配置會被忽略,執(zhí)行輸入的command
如果command沒寫,但args寫了,那么Dockerfile中配置的ENTRYPOINT的命令會被執(zhí)行,使用當(dāng)前args的參數(shù)
如果command和args都寫了,那么Dockerfile的配置被忽略,執(zhí)行command并追加上args參數(shù)
到此這篇關(guān)于K8s新手系列之Pod容器中的command和args指令的文章就介紹到這了,更多相關(guān)K8s Pod中command和args指令內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
教你在k8s上部署HADOOP-3.2.2(HDFS)的方法
這篇文章主要介紹了k8s-部署HADOOP-3.2.2(HDFS)的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04
k8s?pod和service網(wǎng)絡(luò)暴露詳解
這篇文章主要介紹了借助iptables的路由轉(zhuǎn)發(fā)功能,打通k8s集群內(nèi)的pod和service網(wǎng)絡(luò),與外部網(wǎng)絡(luò)聯(lián)通,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
K8S Pod定向部署到指定節(jié)點的實現(xiàn)全過程
K8S Pod定向部署通過節(jié)點標(biāo)簽、親和性和污點三種機制實現(xiàn)資源適配、業(yè)務(wù)隔離與節(jié)點專屬化,適用于不同場景,選型建議為標(biāo)簽用于基礎(chǔ)、親和性用于彈性、污點用于資源保護2025-08-08

