Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

docker - 如何从CLI清理在牧场主中终止的作业(How to cleanup jobs that are terminated in rancher from CLI)

I am running thousands of job in rancher using rancher CLI.

(我正在使用牧场主CLI在牧场主中运行数千个工作。)

I want to delete the job as soon as the task is finished.

(我想在任务完成后立即删除作业。)

THe problem is When the job finishes the job remain "Active" there.

(问题是,当作业完成时,作业在此处保持“活动”状态。)

I tried suggestion from How to automatically remove completed Kubernetes Jobs created by a CronJob?

(我尝试了如何自动删除由CronJob创建的完整Kubernetes作业的建议?)

but its not working for me.

(但它对我不起作用。)

As i can see the underlying docker container gets into "Terminated" state but the deployed job remains active.

(如我所见,底层docker容器进入“已终止”状态,但已部署的作业仍处于活动状态。)

Right now i have to synchronously wait for job to finish and then i am firing "kubectl delete" command to delete job.

(现在,我必须同步等待作业完成,然后触发“ kubectl delete”命令来删除作业。)

But i want to do that asynchronously(delete all job that are "done").

(但是我想异步地做到这一点(删除“完成”的所有工作)。)

Any idea?

(任何的想法?)

Update

(更新资料)

Here is the yaml file for my job

(这是我工作的yaml文件)

apiVersion: batch/v1
kind: Job
metadata:
  name: runtest
  namespace: default
spec:
  ttlSecondsAfterFinished: 60
  template:
    metadata:
      labels:
        job-name: runtest
    spec:
      restartPolicy: Never
      containers:
      - args:
        - sh
        - /code/rancher_test/run_9.sh
        image: x11vnc/docker-desktop
        name: runtest
        stdin: true
        tty: true
        securityContext:
          runAsUser: 53197
        volumeMounts:
        - mountPath: /code
          name: code
      volumes:
      - hostPath:
          path: /code
          type: ""
        name: code
  ask by Krishnom translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can enable TTL Controller (for k8s 1.12 and above), if it's not enabled already and, add ttlSecondsAfterFinished: N parameter, in which case the pods, that created the job, and the job itself will be deleted after N seconds.

(如果尚未启用TTL控制器 (适用于k8s 1.12及更高版本),则可以启用它,并添加ttlSecondsAfterFinished: N参数,在这种情况下,创建作业的ttlSecondsAfterFinished: N和作业本身将在N秒后删除。)

For example:

(例如:)

apiVersion: batch/v1
kind: Job
metadata:
  name: pi-with-ttl
spec:
  ttlSecondsAfterFinished: 0
  template:
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never

This typical kubernetes pi job will clean itself up immediatly after getting to completed.

(完成后,此典型的kubernetes pi工作将立即清理自身。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...