Openshift 如何包含脚本并将其运行到kubernetes yaml中?

Openshift 如何包含脚本并将其运行到kubernetes yaml中?,openshift,kubernetes,openshift-origin,Openshift,Kubernetes,Openshift Origin,这是如何在kubernetes yaml(helloworld.yaml)中运行简单批处理: 在Kubernetes中,我可以这样部署: $ kubectl create -f helloworld.yaml ... image: "ubuntu:14.04" command: ["/bin/bash", "./script.sh"] ... 假设我有这样一个批处理脚本(script.sh): 是否有方法将script.sh包含到kubectl create-f中,以便它可以运行脚本。假设现

这是如何在kubernetes yaml(helloworld.yaml)中运行简单批处理:

在Kubernetes中,我可以这样部署:

$ kubectl create -f helloworld.yaml
...
image: "ubuntu:14.04"
command: ["/bin/bash", "./script.sh"]
...
假设我有这样一个批处理脚本(script.sh):

是否有方法将script.sh包含到
kubectl create-f
中,以便它可以运行脚本。假设现在helloworld.yaml编辑如下:

$ kubectl create -f helloworld.yaml
...
image: "ubuntu:14.04"
command: ["/bin/bash", "./script.sh"]
...

是的,这是可能的,但是您需要构建一个包含脚本的docker容器

我建议读一读

在本地获得一个正常工作的docker映像后,将其推送到docker存储库(例如hub.docker.com),然后用我们自己的映像替换yaml文件中的“映像”定义,例如:

image: "my-custom-image:latest"

另一个要记住的是,如果脚本是一个ad-hoc进程(即不是Web服务),你只想执行一次,你可以考虑使用

我在OpenSHIFT中使用这个方法,所以它也应该适用于Kubernetes。 尝试将脚本放入configmap键/值中,将此configmap装载为卷并从卷运行脚本

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: {}
如前所述,您还可以使用
defaultMode:0777
属性,例如:

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

您可以进入容器外壳并执行脚本
/test script/test.sh

,我只想强调
重启策略的重要性:永远不要
,否则您可能会遇到
崩溃回退。嘿@jbsnoro谢谢你的评论。我的疑问是,如果我们设置了
restartPolicy:Never
,并且我们的脚本由于一些错误而失败,会发生什么呢?很好的例子
defaultMode
fsGroup
必须具有运行脚本所需的权限