如何使用Kubernetes在一个yaml文件中设置多个命令?
在本官方文件中,它可以在yaml配置文件中运行命令: 如果我想运行多个命令,怎么办如何使用Kubernetes在一个yaml文件中设置多个命令?,yaml,kubernetes,Yaml,Kubernetes,在本官方文件中,它可以在yaml配置文件中运行命令: 如果我想运行多个命令,怎么办 command: ["/bin/sh","-c"] args: ["command one; command two && command three"] 解释:命令[“/bin/sh”、“-c”]说“运行shell,并执行以下指令”。然后将参数作为命令传递给shell。在shell脚本中,分号分隔命令,如果第一个命令成功,则有条件地运行以下命令。在上面的示例中,它总是先运行命令一,然后运行命
command: ["/bin/sh","-c"]
args: ["command one; command two && command three"]
解释:命令[“/bin/sh”、“-c”]说“运行shell,并执行以下指令”。然后将参数作为命令传递给shell。在shell脚本中,分号分隔命令,如果第一个命令成功,则有条件地运行以下命令。在上面的示例中,它总是先运行命令一
,然后运行命令二
,并且只有在命令二
成功时才运行命令三
备选方案:在许多情况下,您想要运行的某些命令可能正在设置要运行的最终命令。在这种情况下,建立自己的是一条路要走。请特别查看该指令。如果您愿意使用卷和ConfigMap,可以将其作为脚本,然后运行该脚本:
---
apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
data:
entrypoint.sh: |-
#!/bin/bash
echo "Do this"
echo "Do that"
---
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: "ubuntu:14.04"
command:
- /bin/entrypoint.sh
volumeMounts:
- name: configmap-volume
mountPath: /bin/entrypoint.sh
readOnly: true
subPath: entrypoint.sh
volumes:
- name: configmap-volume
configMap:
defaultMode: 0700
name: my-configmap
这会稍微清理一下pod规范,并允许编写更复杂的脚本
$ kubectl logs my-pod
Do this
Do that
我的首选是多行参数,这是最简单和最容易阅读的。此外,可以在不影响图像的情况下更改脚本,只需重新启动pod。例如,对于mysql转储,容器规范可以如下所示:
containers:
- name: mysqldump
image: mysql
command: ["/bin/sh", "-c"]
args:
- echo starting;
ls -la /backups;
mysqldump --host=... -r /backups/file.sql db_name;
ls -la /backups;
echo done;
volumeMounts:
- ...
这样做的原因是yaml实际上将“-”后面的所有行连接成一行,而sh运行一个长字符串“echo start;ls…;echo done;” 如果要避免使用
将所有命令连接到单个命令中
或&&
您还可以使用herdoc获得真正的多行脚本:
命令:
-嘘
-“-c”
- |
/bin/bash为了提供另一种可能的选择,可以使用机密,因为它们以卷的形式呈现给pod:
秘密例子:
apiVersion: v1
kind: Secret
metadata:
name: secret-script
type: Opaque
data:
script_text: <<your script in b64>>
我知道很多人会说这不是秘密的用途,但它是一种选择。我认为最好的选择是使用YAML的母语。特别是在本例中,折叠样式块
通过调用sh-c
您可以将参数作为命令传递给容器,但是如果您想用换行符将它们优雅地分开,那么您需要使用折叠样式块,以便YAML能够将换行符转换为空格,从而有效地连接命令
apiVersion: batch/v1
kind: Job
metadata:
name: multiline
spec:
template:
spec:
containers:
- command:
- /bin/bash
- -exc
- |
set +x
echo "running below scripts"
if [[ -f "if-condition.sh" ]]; then
echo "Running if success"
else
echo "Running if failed"
fi
name: ubuntu
image: ubuntu
restartPolicy: Never
backoffLimit: 1
一个完整的工作示例:
apiVersion: v1
kind: Pod
metadata:
name: myapp
labels:
app: myapp
spec:
containers:
- name: busy
image: busybox:1.28
command: ["/bin/sh", "-c"]
args:
- >
command_1 &&
command_2 &&
...
command_n
这是我成功的跑步记录
我不确定这个问题是否仍然有效,但由于我在上述答案中没有找到解决方案,我决定将其写下来
我采用以下方法:
readinessProbe:
exec:
command:
- sh
- -c
- |
command1
command2 && command3
我知道我的示例与readinessProbe、livenessProbe等相关,但怀疑容器命令也是如此。这提供了灵活性,因为它反映了在Bash中编写的标准脚本。这里是运行多行命令的另一种方法
apiVersion: batch/v1
kind: Job
metadata:
name: multiline
spec:
template:
spec:
containers:
- command:
- /bin/bash
- -exc
- |
set +x
echo "running below scripts"
if [[ -f "if-condition.sh" ]]; then
echo "Running if success"
else
echo "Running if failed"
fi
name: ubuntu
image: ubuntu
restartPolicy: Never
backoffLimit: 1
apiVersion:batch/v1
种类:工作
元数据:
名称:多行
规格:
模板:
规格:
容器:
-命令:
-/bin/bash
--exc
- |
集合+x
echo“在脚本下运行”
if[-f“if条件.sh”];然后
回显“如果成功则运行”
其他的
回显“失败时运行”
fi
名称:ubuntu
图片:ubuntu
餐馆政策:永不
退避限制:1
是的,非常有效,但是,我认为也有很好的用例可以扩展命令
,因为它覆盖Dockerfile的入口点
;)您知道如何使用容器生命周期实现这一点吗?它没有args@aclokay您只需将参数指定为其他命令字符串。容器中命令和参数之间的分隔只是为了更容易地重写参数。它们在功能上是等效的,-c在这里做什么?@Abdul这意味着运行作为参数提供的脚本,而不是启动交互式shell或从文件加载脚本。非常酷,但我认为将脚本内联更简单,只需使用多行语法。我在一个单独的答案中说明了这一点。当我需要传递双引号时,该怎么办。例如,想象一下这个命令:printf'%s@%s\n'$(echo'user')”“$(echo'host')”很好,但是当您请求使用kubectl进行编辑时,它将再次出现在一行中。:)@塞克雷特:哦,不(这非常好用-关键是每行上的分号。当命令很多并且与上面的解决方案是多行的时候,这是一个特别好的解决方案。使git diff成为一个breezeThis这就是我想要的。使用环境变量作为参数与此解决方案很好用。+1漂亮,加上多行命令工作正常:命令:['/bin/bash','-c']
参数:
-exec&>/path/to/redirected/program.output;
`python/program.py`--key1=val1`--key2=val2`--key3=val3`
readinessProbe:
exec:
command:
- sh
- -c
- |
command1
command2 && command3
apiVersion: batch/v1
kind: Job
metadata:
name: multiline
spec:
template:
spec:
containers:
- command:
- /bin/bash
- -exc
- |
set +x
echo "running below scripts"
if [[ -f "if-condition.sh" ]]; then
echo "Running if success"
else
echo "Running if failed"
fi
name: ubuntu
image: ubuntu
restartPolicy: Never
backoffLimit: 1