在 Kubernetes 容器中找不到从 ConfigMap 挂载的脚本

Script mounted from ConfigMap not found inside Kubernetes container

我有一个 ConfigMap 为我保存了一个 shell 脚本:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Chart.Name }}-script
  labels:
    app: {{ .Chart.Name }}
data:
  backup.sh: |
    #!/bin/bash

    rolling_backup() {
      echo "Found at least 3 backups. Starting rolling backup."
    ...
      rolling_backup
    fi

然后我尝试将其安装到执行此脚本的 CronJob

      ...
      containers:
      - name: {{ .Chart.Name }}-cronjob
        image: busybox
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        command: ['sh', '-c', 'ls -la /etc/scripts/ && /etc/scripts/backup.sh inp-mongo-main-volume']
        volumeMounts:
        - name: {{ .Chart.Name }}-script
          mountPath: "/etc/scripts/"
      volumes:
      - name: {{ .Chart.Name }}-script
        configMap:
          name: {{ .Chart.Name }}-script
          defaultMode: 0777
      ...

每当我尝试 运行 这份工作时,我得到的只是 'not found',当 ls 命令之前脚本执行显示的内容与实际存在的脚本完全不同。

user@computer scripts % kubectl logs -n <namespace> mongo-cron-46svm 
total 12
drwxr-xr-x    2 root     root          4096 Aug  5 10:31 .
drwxr-xr-x    1 root     root          4096 Aug  5 10:31 ..
-rwxrwxrwx    1 root     root          1377 Aug  5 10:31 backup.sh
/bin/sh: /etc/scripts/backup.sh: not found

这可能是什么问题?

Docker Hub busybox 图像是一个极小的图像,仅包含 BusyBox, a single-static-binary application that contains a minimum set of standard Unix tools. It contains an implementation of the Bourne shell /bin/sh that conforms to the POSIX specification,但不包含 GNU bash。

实际上,您应该能够重写大多数 shell 脚本以符合 POSIX 语法。 (使用 . 而不是 source;不要以 function 开始 shell 函数声明;您可能需要使用外部工具而不是复杂的模式扩展。) shell 你展示的片段应该没问题。您只需要将第一行更改为

#!/bin/sh

(因为这是 代码 而不是配置,也可以考虑将其放入自定义 Docker 映像中;您的 CI 系统需要发布它到某些图像注册表以便能够 运行 它,但随后它将遵循与集群中 运行 的所有其他自定义代码相同的生命周期。)