Kubernetes-向容器传递多个命令

Kubernetes-向容器传递多个命令,kubernetes,Kubernetes,我想将多个入口点命令发送到kubernetes配置文件的command标记中的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

我想将多个入口点命令发送到kubernetes配置文件的
command
标记中的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"]

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

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

命令:[“/bin/bash”、“-c”、“touch/foo&&echo'here'&&ls/”]
使用此命令

command: ["/bin/sh","-c"]
args: ["command one; command two && command three"]

乔丹的回答是正确的

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

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

阅读以理解YAML块标量(上述
-
格式)。

您可以像通常处理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 ....";

另一个例子是为busybox映像使用多个bash命令。 这将以while循环持续运行,否则通常busybox映像将使用简单的脚本完成任务,pod将在此之后关闭。 该yaml将持续运行pod

apiVersion: v1
kind: Pod
metadata:
labels:
  run: busybox
  name: busybox
spec:
  containers:
  - command:
  - /bin/sh
  - -c
  - |
    echo "running below scripts"
    i=0; 
    while true; 
    do 
      echo "$i: $(date)"; 
      i=$((i+1)); 
      sleep 1; 
    done
  name: busybox
  image: busybox

可能重复这一点背后的逻辑是什么?我自己也不明白。@dmigo你可以在这里看到Bash文档中的操作符(“;”、“&”、“&&&”、“| |”)来创建“命令列表”,这并不是很好,因为
args
是dockerfile中
cmd
的k8s等价物。而
命令
是与
入口点
相当的k8s。