首页 > 解决方案 > 定义命令和参数 - Pod yaml

问题描述

根据提到的语法,下面是 Pod yaml 使用args

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 中)?

标签: linuxkubernetesdockerfilekubernetes-pod

解决方案


“默认命令”引用容器映像中设置的命令。如果是您的图像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"
]

因此,通过显式设置 a 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

在这里阅读更多。


推荐阅读