Hope all is well. I am stuck with this Pod executing a shell script, using the BusyBox image. The one below works,
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: loop
name: busybox-loop
spec:
containers:
- args:
- /bin/sh
- -c
- |-
for i in 1 2 3 4 5 6 7 8 9 10; \
do echo "Welcome $i times"; done
image: busybox
name: loop
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
But this one doesn't works as I am using "- >" as the operator,
apiVersion: v1
kind: Pod
metadata:
labels:
run: busybox-loop
name: busybox-loop
spec:
containers:
- image: busybox
name: busybox-loop
args:
- /bin/sh
- -c
- >
- for i in {1..10};
- do
- echo ("Welcome $i times");
- done
restartPolicy: Never
Is it because the for syntax "for i in {1..10};" will not work on sh shell. As we know we don't have any other shells in Busybox or the "- >" operator is incorrect, I don't think so because it works for other shell scripts.
Also when can use "- |" multiline operator(I hope the term is correct) and "- >" operator. I know this syntax below is easy to use, but the problem is when we use double quotes in the script, the escape sequence confuses and never works.
args: ["-c", "while true; do echo hello; sleep 10;done"]
Appreciate your support.
CodePudding user response:
...But this one doesn't works as I am using "- >" as the operator...
You don't need '-' after '>' in this case, try:
apiVersion: v1
kind: Pod
metadata:
name: busybox
labels:
run: busybox
spec:
containers:
- name: busybox
image: busybox
args:
- ash
- -c
- >
for i in 1 2 3 4 5 6 7 8 9 10;
do
echo "hello";
done
kubectl logs pod busybox will print hello 10 times.
CodePudding user response:
I think you can use this command to create the pod:
$ kubectl run busybox --image=busybox --dry-run=client -o yaml --command -- /bin/sh -c "for i in {1..10}; do echo 'Welcome $i times'; done" | kubectl apply -f -
pod/busybox created
$ kubectl logs busybox
Welcome 10 times
Note that you can take a look into YAML created by the dry run command as well.
apiVersion: v1
kind: Pod
metadata:
labels:
run: busybox
name: busybox
spec:
containers:
- image: busybox
command:
- /bin/sh
- -c
- for i in {1..10}; do echo 'Welcome 10 times'; done
