如何使用Kubernetes在一个Yaml文件中设置多个命令? [英] How to set multiple commands in one yaml file with Kubernetes?
问题描述
在此正式文档中,它可以在yaml配置文件中运行命令:
In this official document, it can run command in a yaml config file:
apiVersion: v1
kind: Pod
metadata:
name: hello-world
spec: # specification of the pod’s contents
restartPolicy: Never
containers:
- name: hello
image: "ubuntu:14.04"
env:
- name: MESSAGE
value: "hello world"
command: ["/bin/sh","-c"]
args: ["/bin/echo \"${MESSAGE}\""]
如果我要运行多个命令,该怎么办?
If I want to run more than one command, how to do?
推荐答案
command: ["/bin/sh","-c"]
args: ["command one; command two && command three"]
说明: command ["/bin/sh", "-c"]
表示运行外壳程序,并执行以下说明".然后将args作为命令传递到外壳.在shell脚本中,用分号分隔命令,如果第一个成功,则&&
有条件地运行以下命令.在上面的示例中,它总是先运行command one
,然后再运行command two
,并且仅在command two
成功后才运行command three
.
Explanation: The command ["/bin/sh", "-c"]
says "run a shell, and execute the following instructions". The args are then passed as commands to the shell. In shell scripting a semicolon separates commands, and &&
conditionally runs the following command if the first succeed. In the above example, it always runs command one
followed by command two
, and only runs command three
if command two
succeeded.
替代::在许多情况下,您要运行的某些命令可能设置了要运行的最终命令.在这种情况下,构建自己的 Dockerfile 是可行的方法.尤其要查看 RUN 指令.
Alternative: In many cases, some of the commands you want to run are probably setting up the final command to run. In this case, building your own Dockerfile is the way to go. Look at the RUN directive in particular.
这篇关于如何使用Kubernetes在一个Yaml文件中设置多个命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!