如何将参数传递到kubernetes yaml文件?

watbbzwu  于 2023-10-17  发布在  Kubernetes
关注(0)|答案(1)|浏览(102)

如何在多个作业中使用同一个yaml?下面是yaml文件片段:

apiVersion: batch/v1
kind: Job
metadata:
  annotations:
    version: 0.0.1
  name: batch
  labels:
    app: batch
    io.service: batch
  namespace: batchnamespace
spec:
  template:
    spec:
      containers:
      - name: batch-1
        image: batch:001
        args: ["progname","arg1","arg2"]
      restartPolicy: Never

如何实现像“kubectl apply -f job.yaml programname arg 1 arg 2”这样的东西,即,使用相同的yaml文件运行不同的作业?

hc2pp10m

hc2pp10m1#

你的意思是用同一个容器运行不同的pod?在这种情况下,您需要创建一个bash文件,然后使用sed命令更改pod的名称。我使用@multi-pod,所以它可以在bash sed命令中检测到。假设我的yaml文件是:

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: @multi-pod
  name: @multi-pod
spec:
  containers:
  - image: machlovi/tf_threading:latest
    name: "p1"
    env:
     - name: type
       value: demo1

然后创建一个bash文件

#!/bin/bash
  
x=1
# here i have set the num pods 5 which can you change as per need
while [ $x -le 5 ]
do
        # $1 argument is yaml file like tf.yaml 
        #sed command look for @multi-pod and then replace it with p1, p2 
        # till p5
        template=`cat "$1" | sed "s/@multi-pod/p$x/g"`

       
        x=$(( $x + 1 ))

        echo "$template" | kubectl apply -f -
   
done

运行bash文件如下:bash test.sh tf.yaml

相关问题