Container Command and Args

Note, below 2 args formats are tested and worked for shell(sh/bash), other init process may not fit, for example tini, you have to use array format and one token a line.

The format of command and args syntax in kubernetes yaml really makes me crazy, when the entrypoint has long or multiple arguments, can be wrote in multiple lines instead of one line for better view:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
apiVersion: v1
kind: Pod
metadata:
...
spec:
...
container:
- name: busybox
command: ["/bin/sh", "-c", "--"]
args:
- echo "start";
while true; do
echo $(hostname) $(date) >> /html/index.html;
sleep 10;
done;
echo "done!";

Another format:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
apiVersion: v1
kind: Pod
metadata:
...
spec:
containers:
- name: myapp-container
image: busybox
command: ["/bin/sh", "-c", "--"]
args:
["echo \"check something...\";
if [[ ! -f /root/.link ]]; then
echo \"file does not exist!\";
echo \"no!\";
else
echo \"file is there!\";
fi;
echo \"done!\""]

The format is error-prone, every command should end with ;, for example the while and if above. I separate them into several lines for good looking, but they actually are one line command.

For one command with multiple options or parameters, can be described as below, no args field:

1
2
3
4
5
6
7
8
9
containers:
- command:
- /usr/local/bin/etcd
- --data-dir=/var/etcd/data
- --name=example-etcd-cluster-65zvs2mt8b
- --initial-advertise-peer-urls=http://example-etcd-cluster-65zvs2mt8b.example-etcd-cluster.default.svc:2380
- --listen-peer-urls=http://0.0.0.0:2380
- --listen-client-urls=http://0.0.0.0:2379
...
0%