How to include script and run it into kubernetes yaml?

49,003

Solution 1

I'm using this approach in OpenShift, so it should be applicable in Kubernetes as well.

Try to put your script into a configmap key/value, mount this configmap as a volume and run the script from the volume.

apiVersion: batch/v1
kind: Job
metadata:
  name: hello-world-job
spec:
  parallelism: 1    
  completions: 1    
  template:         
    metadata:
      name: hello-world-job
    spec:
      volumes:
      - name: hello-world-scripts-volume
        configMap:
          name: hello-world-scripts
      containers:
      - name: hello-world-job
        image: alpine
        volumeMounts:
          - mountPath: /hello-world-scripts
            name: hello-world-scripts-volume
        env:
          - name: HOME
            value: /tmp
        command:
        - /bin/sh
        - -c
        - |
          echo "scripts in /hello-world-scripts"
          ls -lh /hello-world-scripts
          echo "copy scripts to /tmp"
          cp /hello-world-scripts/*.sh /tmp
          echo "apply 'chmod +x' to /tmp/*.sh"
          chmod +x /tmp/*.sh
          echo "execute script-one.sh now"
          /tmp/script-one.sh
      restartPolicy: Never
---
apiVersion: v1
items:
- apiVersion: v1
  data:
    script-one.sh: |
      echo "script-one.sh"
      date
      sleep 1
      echo "run /tmp/script-2.sh now"
      /tmp/script-2.sh
    script-2.sh: |
      echo "script-2.sh"
      sleep 1
      date
  kind: ConfigMap
  metadata:
    creationTimestamp: null
    name:  hello-world-scripts
kind: List
metadata: {}

Solution 2

As explained here, you could use the defaultMode: 0777 property as well, an example:

apiVersion: v1
kind: ConfigMap
metadata:
  name: test-script
data:
  test.sh: |
    echo "test1"
    ls
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
spec:
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
    spec:
      volumes:
      - name: test-script
        configMap:
          name: test-script
          defaultMode: 0777
      containers:
      - command:
        - sleep
        - infinity
        image: ubuntu
        name: locust
        volumeMounts:
          - mountPath: /test-script
            name: test-script

You can enter into the container shell and execute the script /test-script/test.sh

Share:
49,003

Related videos on Youtube

smftr
Author by

smftr

Updated on January 29, 2022

Comments

  • smftr
    smftr over 2 years

    It is how to run simple batch in kubernetes yaml (helloworld.yaml):

    ...
    image: "ubuntu:14.04"
    command: ["/bin/echo", "hello", "world"]
    ...
    

    In Kubernetes i can deploy that like this:

    $ kubectl create -f helloworld.yaml
    

    Suppose i have a batch script like this (script.sh):

    #!/bin/bash
    echo "Please wait....";
    sleep 5
    

    Is there way to include the script.sh into kubectl create -f so it can run the script. Suppose now helloworld.yaml edited like this:

    ...
    image: "ubuntu:14.04"
    command: ["/bin/bash", "./script.sh"]
    ...
    
  • JBSnorro
    JBSnorro about 4 years
    I just want to stress the importance of restartPolicy: Never, because otherwise you might get CrashLoopBackOffs. See here.
  • aru_sha4
    aru_sha4 over 3 years
    Hey @JBSnorro thanks for the comment. My doubt is what happens if we set the restartPolicy: Never and our script fails due to some error?
  • kheraud
    kheraud about 3 years
    Nice example; defaultMode or fsGroup is required to have the required permissions to run the script
  • prince yadav
    prince yadav over 2 years
    can we bash over shell in pod
  • Shaiju Janardhanan
    Shaiju Janardhanan over 2 years
    Is there a need to use the list object? I was able to use multiple scripts and execute them as specified in the answer using a deployment without using list. But i am not able to figure out why list was used in the answer. Can anyone help please
  • User12547645
    User12547645 over 2 years
    Why do we get the CrashLoopBackOff if we do not define the restartPolicy?
  • John Jang
    John Jang over 2 years
    You save my day!!!
  • Dale C. Anderson
    Dale C. Anderson about 2 years
    So building a well-architected solution gets downvotes? I don't get it. This seems like the least hack-ish of all the answers so far. It might be a little more work to set up, but would be the easiest to maintain in the long term. If I know anything about bash scripts, it's that they never stay small and simple for very long.