Kubernetes - 将多个命令传递给容器
Kubernetes - Passing multiple commands to the container
我想将多个入口点命令发送到 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"]
不过好像不行。在command标签中发送多个命令的正确格式是什么?
容器中只能有一个入口点...如果您想 运行 多个这样的命令,请将 bash 作为入口点,并将所有其他命令设为bash 到 运行 的参数:
command: ["/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
阅读this了解YAML块标量(上述>-
格式)。
您可以像通常处理 yaml 一样简单地列出命令 arrays/lists。查看 this question on 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
扩展
的正确答案
command: ["/bin/bash","-c","touch /foo && echo 'here' && ls /"]
Docker 需要容器中的单个入口点,并且该入口点应启动一个前台进程。这就是为什么我们需要使用 /bin/bash -c.
/bin/bash -c 此处将为新的 shell 启动一个新进程并执行其余命令。这样容器运行时就可以监视单个进程并报告容器是否正常完成。
我想将多个入口点命令发送到 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"]
不过好像不行。在command标签中发送多个命令的正确格式是什么?
容器中只能有一个入口点...如果您想 运行 多个这样的命令,请将 bash 作为入口点,并将所有其他命令设为bash 到 运行 的参数:
command: ["/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
阅读this了解YAML块标量(上述>-
格式)。
您可以像通常处理 yaml 一样简单地列出命令 arrays/lists。查看 this question on 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
扩展
command: ["/bin/bash","-c","touch /foo && echo 'here' && ls /"]
Docker 需要容器中的单个入口点,并且该入口点应启动一个前台进程。这就是为什么我们需要使用 /bin/bash -c.
/bin/bash -c 此处将为新的 shell 启动一个新进程并执行其余命令。这样容器运行时就可以监视单个进程并报告容器是否正常完成。