定义命令和参数 - Pod yaml

Defining command & argument - Pod yaml

根据提到的 syntax,下面是使用 args:

的 Pod yaml
apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-exec
spec:
  containers:
  - name: liveness
    image: k8s.gcr.io/busybox
    resources:
      limits:
        memory: "64Mi" #64 MB
        cpu: "50m" #50 millicpu (.05 cpu or 5% of the cpu)
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 5

文档说:If you define args, but do not define a command, the default command is used with your new arguments.

根据文档,参数的默认命令是什么(在上面的 yaml 中)?

“默认命令”引用容器映像中的命令集。对于您的图片 - k8s.gcr.io/busybox - 这似乎是 /bin/sh:

$ docker pull k8s.gcr.io/busybox
Using default tag: latest
latest: Pulling from busybox
a3ed95caeb02: Pull complete 
138cfc514ce4: Pull complete 
Digest: sha256:d8d3bc2c183ed2f9f10e7258f84971202325ee6011ba137112e01e30f206de67
Status: Downloaded newer image for k8s.gcr.io/busybox:latest
k8s.gcr.io/busybox:latest
$ docker image inspect k8s.gcr.io/busybox | jq '.[0] | .Config.Cmd'
[
  "/bin/sh"
]

因此,通过显式设置 pod.spec.containers.command,您实际上覆盖了该值。

另请参阅:

$ kubectl explain pod.spec.containers.command
KIND:     Pod
VERSION:  v1

FIELD:    command <[]string>

DESCRIPTION:
     Entrypoint array. Not executed within a shell. The docker 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. The
     $(VAR_NAME) syntax can be escaped with a double $$, ie: $$(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

阅读更多 here

要将其可视化,您可以运行以下命令:

kubectl  run busybox-default --image busybox
pod/busybox-default created
kubectl  run busybox-command --image busybox --command sleep 10000
pod/busybox-command created

检查 docker ps 的输出并查找 COMMAND 列。您可以使用 --no-trunc 标志来完成输出。

容器运行输出command选项:

docker ps |grep -E 'busybox-default'
CONTAINER ID        IMAGE                  COMMAND                  CREATED              STATUS              PORTS               NAMES
e01428c98071        k8s.gcr.io/pause:3.2   "/pause"                 3 minutes ago       Up 3 minutes                            k8s_POD_busybox-default_default_3449d2bc-a731-4441-9d78-648a7fa730dd_0

容器 运行 command 选项的输出:

docker ps |grep -E 'busybox-command'
CONTAINER ID        IMAGE                  COMMAND                  CREATED              STATUS              PORTS               NAMES
557578fc60ea        busybox                "sleep 10000"            5 minutes ago       Up 5 minutes                            k8s_busybox-comand_busybox-command_default_57f6d09c-2ed1-4b73-b3f9-c2b612c19a16_0
7c6f1240ab07        k8s.gcr.io/pause:3.2   "/pause"                 5 minutes ago       Up 5 minutes                            k8s_POD_busybox-comand_default_57f6d09c-2ed1-4b73-b3f9-c2b612c19a16_0