Execute while + for loop through command + args in Pod

Summary

If you want to execute the while loop + for loop of the shell in the pod of kubernetes , the configuration is recorded as follows

while finite loop

---
kind: Pod
apiVersion: v1
metadata:
  name: pod-diy-command
spec:
  terminationGracePeriodSeconds: 0
  containers:
  - name: busybox-diy
    image: busybox
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh"]
    args:
    - -c
    - |
      i=0
      while [ $i -le 10 ] ;
      do
        ls -l
        sleep 30
        echo "hello world"  >> ./hello.txt
        cat ./hello.txt
        let i++
        pwd
      done
  restartPolicy: Always

Effect:

# 创建
kubectl apply -f 3-pod-diy.yml
# 强制创建替换
kubectl replace --force -f 3-pod-diy.yml
# 查看记录
kubectl logs -f pod-diy-command

while infinite loop

---
kind: Pod
apiVersion: v1
metadata:
  name: pod-diy-command
spec:
  terminationGracePeriodSeconds: 0
  containers:
  - name: busybox-diy
    image: busybox
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh"]
    args:
    - -c
    - |
      while true ;
      do
        ls -l
        sleep 3
        echo "hello world"  >> ./hello.txt
        cat ./hello.txt
        pwd
      done
  restartPolicy: Always

Effect:

for loop

The command + args in the pod may not be able to traverse the for loop in the way of writing for i in {1..100} like the Linux operating system.
Can only be executed by specifying traversal
---
kind: Pod
apiVersion: v1
metadata:
  name: pod-diy-command
spec:
  terminationGracePeriodSeconds: 0
  containers:
  - name: busybox-diy
    image: busybox
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh"]
    args:
    - -c
    - |
      for i in 01 02 03 04 05
      do
        ls -l
        sleep 3
        echo $i  >> ./hello.txt
        cat ./hello.txt
        pwd
      done
  restartPolicy: Always

Guess you like

Origin blog.csdn.net/m0_59267075/article/details/129541616