I have the following Kubernetes job defined in job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: pi-$count
spec:
template:
spec:
containers:
- name: pi
image: perl:5.34.0
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
I apply this with the following bash script:
for i in {1..3}
do
kubectl apply -f job.yaml
done
Is there a way to create 3 jobs here and use an environment variable pi-$count
so pi-1
, pi-2
, pi-3
are created?
CodePudding user response:
You can use sed
to replace the $count
and create a manifest file for each job and apply it.
For example create a file called pi-job.yaml
(I'm taking your code as example)
apiVersion: batch/v1
kind: Job
metadata:
name: pi-$count
spec:
template:
spec:
containers:
- name: pi
image: perl:5.34.0
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
Create a folder called job to store the manifest files (this directory is completely optional)You can use something like this
mkdir job
for i in {1..3}
do
cat pi-job.yaml | sed "s/\$count/$i/" > ./jobs/job-$i.yaml;
kubectl apply -f ./jobs/job-$i.yaml
done
By executing this code 3 jobs will be created in the name of pi-1,pi-2 and pi-3. But the manifest files will reside in the job folder. You need to clean it up. Otherwise you can add one more line to the script to remove the files like this
mkdir job
for i in {1..3}
do
cat pi-job.yaml | sed "s/\$count/$i/" > ./jobs/job-$i.yaml;
kubectl apply -f ./jobs/job-$i.yaml
rm -rf ./jobs/job-$i.yaml
done
For more detailed information refer to the official k8's job document
CodePudding user response:
Use sed command to replace the job counter (COUNT) and create temporary files for each of the job instances. You can delete all the temporary job yamls after deployment. In this way you can keep your original job yaml file intact for reusing. Let's say I am using your kubernetes job yaml named 'job.yaml'.
apiVersion: batch/v1
kind: Job
metadata:
name: pi-COUNT
spec:
template:
spec:
containers:
- name: pi
image: perl:5.34.0
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
Modify your bash script like the following. I'm naming it jobrunner.sh
.
#!/usr/bin/bash
for i in {1..3}
do
sed 's|COUNT|'${i}'|g' job.yaml > job-$i.yaml
kubectl apply -f job-$i.yaml
done
rm job-*.yaml
Run this bash script and 3 jobs will be created in your Kubernetes named pi-1
, pi-2
& pi-3
. You can comment out the last part where the temporary job files are being deleted with rm job-*.yaml
command if you wish to keep those files.