【问题标题】:How to include script and run it into kubernetes yaml?如何包含脚本并将其运行到 kubernetes yaml 中?
【发布时间】:2017-05-31 03:45:59
【问题描述】:

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

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

在 Kubernetes 中我可以这样部署:

$ kubectl create -f helloworld.yaml

假设我有一个像这样的批处理脚本(script.sh):

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

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

...
image: "ubuntu:14.04"
command: ["/bin/bash", "./script.sh"]
...

【问题讨论】:

    标签: openshift kubernetes openshift-origin


    【解决方案1】:

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

    我建议阅读this getting started guide

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

    image: "my-custom-image:latest"
    

    要记住的另一件事是,如果脚本是您只想执行一次的临时进程(即不是 Web 服务),您可以考虑使用 kubernetes job

    【讨论】:

      【解决方案2】:

      我在 OpenShift 中使用了这种方法,因此它应该也适用于 Kubernetes。

      尝试将您的脚本放入配置映射键/值中,将此配置映射挂载为卷并从该卷运行脚本。

      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: {}
      

      【讨论】:

      • 我只想强调restartPolicy: Never 的重要性,否则你可能会得到CrashLoopBackOffs。 See here.
      • 嘿@JBSnorro 感谢您的评论。我怀疑如果我们设置restartPolicy: Never 并且我们的脚本由于某些错误而失败会发生什么?
      • 我们可以在 pod 中对 shell 进行 bash
      • 是否需要使用列表对象?我能够使用多个脚本并按照答案中指定的方式使用部署而不使用列表来执行它们。但我无法弄清楚为什么在答案中使用了列表。有人可以帮忙吗
      • 如果我们不定义restartPolicy,为什么会得到CrashLoopBackOff
      【解决方案3】:

      正如here 解释的那样,您也可以使用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
      

      可以进入容器shell,执行脚本/test-script/test.sh

      【讨论】:

      • 很好的例子; defaultModefsGroup 需要具有运行脚本所需的权限
      • 你拯救了我的一天!!!
      猜你喜欢
      • 2022-01-05
      • 2019-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-14
      • 2011-09-21
      • 2020-10-13
      • 1970-01-01
      相关资源
      最近更新 更多