Kubernetes - 将多个命令传递给容器

Dim*_*thu 25 kubernetes

我想在commandkubernetes配置文件的标签中向Docker容器发送多个入口点命令.

apiVersion: v1
kind: Pod
metadata:
  name: hello-world
spec:  # specification of the pod’s contents
  restartPolicy: Never
  containers:
  - name: hello
    image: "ubuntu:14.04"
    command: ["command1 arg1 arg2 && command2 arg3 && command3 arg 4"]
Run Code Online (Sandbox Code Playgroud)

但似乎它不起作用.在命令标记中发送多个命令的正确格式是什么?

Jor*_*itt 42

容器中只能有一个入口点...如果要运行多个这样的命令,请将bash作为入口点,并使所有其他命令成为bash运行的参数:

command: ["/bin/bash","-c","touch /foo && echo 'here' && ls /"]


Vic*_*ong 29

乔丹的回答是正确的。

但为了提高可读性,我更喜欢:

apiVersion: v1
kind: Pod
metadata:
  name: hello-world
spec:  # specification of the pod’s contents
  restartPolicy: Never
  containers:
  - name: hello
    image: "ubuntu:14.04"
    command: ["/bin/sh"]
    args:
      - -c
      - >-
          command1 arg1 arg2 &&
          command2 arg3 &&
          command3 arg4
Run Code Online (Sandbox Code Playgroud)

阅读本文以了解 YAML 块标量(上述>-格式)。

  • 谢谢。这确实是一种更具可读性的方法 (2认同)

小智 10

使用此命令

command: ["/bin/sh","-c"]
args: ["command one; command two && command three"]
Run Code Online (Sandbox Code Playgroud)


Zst*_*ack 9

您可以像通常处理 yaml 数组/列表一样简单地列出命令。看看这个关于 yaml 数组语法的问题。

下面是如何将参数列表传递给命令的示例。请注意命令末尾的分号,否则会出现错误。

  containers:
  - name: my-container
    image: my-image:latest
    imagePullPolicy: Always
    ports:
    - containerPort: 80
    command: [ "/bin/bash", "-c" ]
    args:
     - 
        echo "check if my service is running and run commands";
        while true; do
            service my-service status > /dev/null || service my-service start;
            if condition; then
                    echo "run commands";
            else
                    echo "run another command";
            fi;
        done
        echo "command completed, proceed ....";
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

21605 次

最近记录:

7 年,1 月 前