How to include script and run it into kubernetes yaml?
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/v1kind: Jobmetadata: name: hello-world-jobspec: 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: v1items:- 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-scriptskind: Listmetadata: {}
As explained here, you could use the defaultMode: 0777
property as well, an example:
apiVersion: v1kind: ConfigMapmetadata: name: test-scriptdata: test.sh: | echo "test1" ls---apiVersion: apps/v1kind: Deploymentmetadata: name: testspec: 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
Yes, this is possible, but you'll need to build a docker container containing your script.
I suggest having a read of this getting started guide
Once you've got a working docker image locally, push it to a docker repository (such a hub.docker.com) and then replaced the "image" definition in your yaml file with our own image, eg:
image: "my-custom-image:latest"
One other thing to bear in mind, is if the script is an ad-hoc process (ie not a web service) which you just want to have executed once, you might consider using the kubernetes job