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ǔ)鏡像中默認(rèn)的啟動命令,也就是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ǔ)鏡像中默認(rèn)命令的參數(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默認(rèn)的配置會被忽略,執(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)文章
關(guān)于k8s?使用?Service?控制器對外暴露服務(wù)的問題
這篇文章主要介紹了k8s使用Service控制器對外暴露服務(wù),包括部署deploy,部署?service及查看?service?和?pod?的關(guān)系,本文給大家介紹的非常詳細,需要的朋友可以參考下2022-03-03
kubernetes部署dashboard及應(yīng)用小結(jié)
Dashboard?是基于網(wǎng)頁的?Kubernetes?用戶界面,可以對?Deployment?實現(xiàn)彈性伸縮、發(fā)起滾動升級、重啟?Pod?或者使用向?qū)?chuàng)建新的應(yīng)用,這篇文章主要介紹了kubernetes部署dashboard,需要的朋友可以參考下2024-06-06

