一、名词解释及作用
ENTRYPOINT 指令用于设置容器的入口点(entrypoint),它接受一个命令(可以是可执行文件或脚本),并且可以与 CMD 指令一起使用。ENTRYPOINT 的参数将始终被执行,并且可以通过 CMD 提供额外的参数。
在Kubernetes 的 Pod 配置中,可以使用 command 和 args 字段来定义容器的启动命令。command 字段用于指定要执行的命令,它接受一个字符串数组,表示要执行的命令及其参数。args 字段用于向命令传递额外的参数,它也是一个字符串数组。
二、参数对比
情况1:如果command和args都没有写,那么用dockerfile的ENTRYPONT&CMD配置
测试:
#编写dockerfile
vim dockerfile
FROM centos:latest
ENTRYPOINT ["echo"]
cmd ["hello"]
#构建镜像,镜像名字叫centos版本号为v1
docker build -t=centos:v1 .
[+] Building 0.1s (5/5) FINISHED docker:default
=> [internal] load build definition from dockerfile 0.0s
=> => transferring dockerfile: 90B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:latest 0.0s
=> [1/1] FROM docker.io/library/centos:latest 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:4228d8b132ae98234d0cba1a6f0623dfa557d06e57345dbd783e411e99541135 0.0s
=> => naming to docker.io/library/centos:v1
#导出镜像
docker save -o centosv1.tar centos:v1
#导入到K8S的容器运行时(我的k8s容器运行时为containerd)
ctr -n=k8s.io images import centosv1.tar
#编写v1.yaml
apiVersion: v1
kind: Pod
metadata:
name: v1
labels:
app: centos
spec:
containers:
- name: centos
image: docker.io/library/centos:v1
imagePullPolicy: IfNotPresent
#更新资源清单
kubectl apply -f v1.yaml
#查看日志
kubectl logs v1
结果:hello
情况2:如果 command 写了,但 args 没有写,那么 Dockerfile 默认的配置会被忽略,执行输入的
command
vim v2.yaml
apiVersion: v1
kind: Pod
metadata:
name: v2
labels:
app: centos
spec:
containers:
- name: v2
image: centos:v1
imagePullPolicy: IfNotPresent
command: ["echo","hello world"]
#更新资源清单
kubectl apply -f v2.yaml
#查看日志
kubectl logs v2
结果:hello world
情况3:如果 command 没写,但 args 写了,那么 Dockerfile 中配置的 ENTRYPOINT 的命令行会被执 行,并且将 args 中填写的参数追加到 ENTRYPOINT 中
vim v3.yaml
apiVersion: v1
kind: Pod
metadata:
name: v3
labels:
app: centos
spec:
containers:
- name: v3
image: centos:v1
imagePullPolicy: IfNotPresent
args: ["hello wolrd"]
#更新资源清单
kubectl apply -f v3.yaml
#查看日志
kubectl logs v3
hello wolrd
情况4:如果 command 和 args 都写了,那么 Dockerfile 的配置被忽略,执行 command 并追加上
args 参数。
vim v4.yaml
apiVersion: v1
kind: Pod
metadata:
name: v4
labels:
app: centos
spec:
containers:
- name: v4
image: centos:v1
imagePullPolicy: IfNotPresent
command: ["ping"]
args: ["www.baidu.com"]
#更新资源清单
kubectl apply -f v4.yaml
#查看日志
kubectl logs v4
PING www.a.shifen.com (182.61.200.6) 56(84) bytes of data.
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=1 ttl=127 time=5.82 ms
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=2 ttl=127 time=4.07 ms
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=3 ttl=127 time=4.38 ms
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=4 ttl=127 time=15.2 ms
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=5 ttl=127 time=4.59 ms
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=6 ttl=127 time=4.49 ms
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=7 ttl=127 time=4.13 ms
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=8 ttl=127 time=4.75 ms