如何包含脚本并将其运行到 kubernetes yaml 中? [英] How to include script and run it into kubernetes yaml?

查看:21
本文介绍了如何包含脚本并将其运行到 kubernetes yaml 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

In Kubernetes i can deploy that like this:

$ kubectl create -f helloworld.yaml

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

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

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

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

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"]
...

推荐答案

我在 OpenShift 中使用这种方法,所以它应该也适用于 Kubernetes.

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

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

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

这篇关于如何包含脚本并将其运行到 kubernetes yaml 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆