kuberneteskubectlkubernetes-jobs

Running bash script in a kubernetes pod


I am trying to run an external bash script using the below yaml file.

The script is inside the /scripts/run.sh folder. I have also given the defaultMode: 0777

This is the error I get.

sh: 0: Can't open /scripts/run.sh
apiVersion: v1
data:
  script.sh: |-
    echo "Hello world!"
    kubectl get pods
kind: ConfigMap
metadata:
  name: script-configmap
---
apiVersion: batch/v1
kind: Job
metadata:
  labels:
    app: script-job
  name: script-job
spec:
  backoffLimit: 2
  template:
    spec:
      containers:
        - command:
            - sh
            - /scripts/run.sh
          image: 'bitnami/kubectl:1.12'
          name: script
          volumeMounts:
            - name: script-configmap
              mountPath: /scripts
              subPath: run.sh              
              readOnly: false
      restartPolicy: Never
      volumes:
        - name: script-configmap
          configMap:
            name: script-configmap
            defaultMode: 0777

Solution

  • The file name is script.sh and not run.sh

    Try

    containers:
        - command:
            - sh
            - /scripts/script.sh