K8s Pod容器中的command和args指令詳解
概述
command和args是containers下的兩個(gè)指令,類似Dockerfile中的ENTRYPONIT和CMD指令。
官方文檔地址:https://kubernetes.io/zh-cn/docs/tasks/inject-data-application/define-command-argument-container/
command
command功能同Dockerfile中的ENTRYPONIT指令,用于指定容器啟動(dòng)時(shí)要執(zhí)行的命令。如果不設(shè)置command,容器將使用基礎(chǔ)鏡像中默認(rèn)的啟動(dòng)命令,也就是ENTRYPONIT指定的啟動(dòng)命令。
可以通過(guò)kubectl explain pod.spec.containers.command查看對(duì)應(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沒(méi)有指定,則args中的參數(shù)將作為基礎(chǔ)鏡像中默認(rèn)命令的參數(shù),也就是ENTRYPONIT指令的參數(shù)。
可以通過(guò)kubectl explain pod.spec.containers.args查看對(duì)應(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使用注意事項(xiàng)
如果command和args均沒(méi)有寫(xiě),那么用Dockerfile的配置。
如果command寫(xiě)了,但args沒(méi)有寫(xiě),那么Dockerfile默認(rèn)的配置會(huì)被忽略,執(zhí)行輸入的command
如果command沒(méi)寫(xiě),但args寫(xiě)了,那么Dockerfile中配置的ENTRYPOINT的命令會(huì)被執(zhí)行,使用當(dāng)前args的參數(shù)
如果command和args都寫(xiě)了,那么Dockerfile的配置被忽略,執(zhí)行command并追加上args參數(shù)
到此這篇關(guān)于K8s新手系列之Pod容器中的command和args指令的文章就介紹到這了,更多相關(guān)K8s Pod中command和args指令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- k8s強(qiáng)制刪除一個(gè)Pod的詳細(xì)步驟
- K8s學(xué)習(xí)之Pod的定義及詳細(xì)資源調(diào)用案例
- K8S如何利用Prometheus監(jiān)控pod的實(shí)時(shí)數(shù)據(jù)指標(biāo)
- K8S內(nèi)部pod之間相互調(diào)用案例以及詳解
- K8S中Pod重啟策略及重啟可能原因詳細(xì)講解
- k8s中如何實(shí)現(xiàn)pod自動(dòng)擴(kuò)縮容詳解
- K8s實(shí)戰(zhàn)教程之容器和?Pods資源分配問(wèn)題
- k8s查看pod日志的幾種實(shí)用方法匯總
- k8s監(jiān)控?cái)?shù)據(jù)組件Pod自動(dòng)化進(jìn)行擴(kuò)縮容HPA
相關(guān)文章
k8s中實(shí)現(xiàn)mysql主備過(guò)程詳解
文章講解了在K8s中使用StatefulSet部署MySQL主備架構(gòu),包含NFS安裝、storageClass配置、MySQL部署及同步檢查步驟,確保主備數(shù)據(jù)一致性與高可用性2025-09-09
教你在k8s上部署HADOOP-3.2.2(HDFS)的方法
這篇文章主要介紹了k8s-部署HADOOP-3.2.2(HDFS)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
php redis擴(kuò)展支持scan命令實(shí)現(xiàn)方法
這篇文章主要介紹了php redis擴(kuò)展支持scan命令實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2016-10-10
k8s?pod和service網(wǎng)絡(luò)暴露詳解
這篇文章主要介紹了借助iptables的路由轉(zhuǎn)發(fā)功能,打通k8s集群內(nèi)的pod和service網(wǎng)絡(luò),與外部網(wǎng)絡(luò)聯(lián)通,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
云原生時(shí)代的前端部署最佳實(shí)踐(含詳細(xì)代碼)
云原生安全架構(gòu)應(yīng)運(yùn)而生,它通過(guò)零信任、自動(dòng)化防護(hù)和全生命周期管理等理念,為企業(yè)提供從基礎(chǔ)設(shè)施到應(yīng)用層的全方位保護(hù),這篇文章主要介紹了云原生時(shí)代的前端部署最佳實(shí)踐,需要的朋友可以參考下2026-04-04
K8S Pod定向部署到指定節(jié)點(diǎn)的實(shí)現(xiàn)全過(guò)程
K8S Pod定向部署通過(guò)節(jié)點(diǎn)標(biāo)簽、親和性和污點(diǎn)三種機(jī)制實(shí)現(xiàn)資源適配、業(yè)務(wù)隔離與節(jié)點(diǎn)專屬化,適用于不同場(chǎng)景,選型建議為標(biāo)簽用于基礎(chǔ)、親和性用于彈性、污點(diǎn)用于資源保護(hù)2025-08-08

