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

问题描述 投票:0回答:3

这是如何在 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
3个回答
47
投票

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

15
投票

正如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


0
投票

我最近遇到了同样的问题,想要在本地以及 kubernetes 上运行脚本,并认为应该有解决方案来创建一个 shebang + 注释行,我可以将其放在本地脚本前面,以使其在 k8s 上运行默认。 好吧,我将其作为一个宠物项目进行跟踪,最终得到了一个公认的相当复杂且丑陋的解决方案 - 但是嘿 - 它对我有用 - 也许你可以找到它的用途..

#!/usr/bin/awk BEGIN{exit system("bash -euo pipefail -c 'f=\"" ARGV[1] "\";n=$(awk \"/^#!\\/bin\\/bash$/ {print NR}\" \"$f\" ); t=$(sed -e \"s/^# //\" -ne \"2,$((n-1))p\" \"$f\"); s=$(tail +$n \"$f\") ;  eval \"echo \\\"$t\\\" \" | bash -eu'")}
# timout=300
# name="adhoc-\$HOSTNAME-\$USER-$(basename $f| sed "s/[^[:alnum:]]/-/g")-\$timout"
# kubectl get job \$name &> /dev/null && kubectl delete job \$name > /dev/null
# kubectl apply -f - <<EOF > /dev/null
# apiVersion: batch/v1
# kind: Job
# metadata:
#   name: \$name
# spec:
#   template:
#     spec:
#       containers:
#       - name: \${name}-cont
#         image: centos
#         command:
#         - sh
#         - "-c"
#         - |
#           /bin/sh <<LOL
# $(echo "$s"| sed 's/^/          /')
#           LOL
#       restartPolicy: Never
# EOF
# if [[ "\$?" != "0" ]]; then
#    exit 1
# fi
# podname=\$(kubectl get pods --sort-by=.metadata.creationTimestamp --selector=job-name=\$name -o jsonpath="{.items[0].metadata.name}")
# sleep 1
# retval=1
# for i in {1..200}; do 
#   status=\$(kubectl get pod \$podname -o jsonpath='{.status.phase}') || break
#   [[ "\$status" == "Failed" ]] && break
#   [[ "\$status" == "Failed" ]] && retval=0 && break
#   sleep 1
# done
#
# kubectl logs -f \$podname
# 
# exit \$retval
#!/bin/bash

echo hello world from k8s(maybe)
© www.soinside.com 2019 - 2024. All rights reserved.