Kubernetes POD 命令和参数

Kubernetes POD Command and argument

我正在学习 kubernetes 并且有以下与 POD 的命令和参数语法相关的问题。

在 POD 的参数中编写 shell 脚本类代码是否需要遵循任何特定语法?例如

在下面的代码中,我怎么知道while true需要以分号结尾;为什么在do之后没有分号但是在If etc

之后
   while true;
  do
  echo $i;
  if [ $i -eq 5 ];
  then
    echo "Exiting out";
    break;
  fi;
      i=$((i+1));
      sleep "1";
  done

我们不会从分号的角度以类似的方式编写 shell 脚本,所以为什么我们必须在 POD 中这样做。

我也尝试了 /bin/bash 格式的命令

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: bash
  name: bash
spec:
  containers:
  - image: nginx
    name: nginx
    args:
    - /bin/bash
    - -c
    - >
       for i in 1 2 3 4 5
       do
         echo "Welcome $i times"
       done
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

新代码出错

/bin/bash: -c: line 2: syntax error near unexpected token `echo'
/bin/bash: -c: line 2: `  echo "Welcome $i times"'

Are there any specific syntax that we need to follow to write a shell script kind of code in the arguments of a POD?

不,shell 语法是相同的。

...how will I know that the while true need to end with a semicolon

使用 | 将您的文本块视为普通 shell 脚本:

...
args:
- /bin/bash
- -c
- |
  for i in 1 2 3 4 5
  do
    echo "Welcome $i times"
  done

当您使用 > 时,您的文本块将合并为一行,其中换行符被替换为白色 space。在这种情况下,您的命令无效。如果你希望你的命令是一行,那么就像在普通终端中一样用 ; 写它们。这是 shell 脚本标准,不是 K8s 特定的。

如果必须使用>,则需要添加空行或正确缩进下一行:

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: bash
  name: bash
spec:
  containers:
  - image: nginx
    name: nginx
    args:
    - /bin/bash
    - -c
    - >
      for i in 1 2 3 4 5
        do
          echo "Welcome $i times"
        done
  restartPolicy: Never

kubectl logs bash 查看 5 个回声,kubectl delete pod bash 到 clean-up。