k8s中的command和args可以覆盖docker镜像中的Entrypoint和Cmd。其中,k8s的command可以覆盖docker镜像中的Entrypoint,k8s的args可以覆盖docker镜像中的Cmd。参考 Difference between Docker ENTRYPOINT and Kubernetes container spec COMMAND?

DescriptionDocker field nameKubernetes field name
The command run by the containerEntrypointcommand
The arguments passed to the commandCmdargs

When you override the default Entrypoint and Cmd, these rules apply:

  • If you do not supply command or args for a Container, the defaults defined in the Docker image are used.
  • If you supply a command but no args for a Container, only the supplied command is used. The default EntryPoint and the default Cmd defined in the Docker image are ignored.
  • If you supply only args for a Container, the default Entrypoint defined in the Docker image is run with the args that you supplied.
  • If you supply a command and args, the default Entrypoint and the default Cmd defined in the Docker image are ignored. Your command is run with your args.

了解一下entrypoint的意义:ENTRYPOINT 入口点

很多官方镜像多会使用entrypoint,看看redis:https://github.com/docker-library/redis/blob/master/Dockerfile.template#L116C2-L117C36

===============================================

关于pod yaml中的数组缩进,k8s的官方pod yaml中不用缩进(有短横线的地方表示这是数组中的一个元素,短横线不需要缩进。):

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx  // 这里没有缩进
    image: nginx:1.14.2
    ports:
    - containerPort: 80  // 这里没有缩进

在k8s pod yaml中,对于command和args字段缩进与不缩进的区别:不缩进加不加引号都行;缩进的话需要加上引号才行,否则会报错。

不缩进
缩进

注意上面command中的init.sh必须写绝对路径,而且是可执行文件。

chmod a+x init.sh
chmod 755 init.sh

===============================================

k8s pod yaml找不到本地镜像?看看这个:Pull a local image to run a pod in Kubernetes

执行一下

eval $(minikube docker-env)

重新构建镜像。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐