Skip to content
gqlxj1987's Blog
Go back

Kubernetes Running background task

Edit page

原文链接

run a batch-job in a few different ways: one-time, sequential, and parallel.

apiVersion: batch/v1
kind: Job
metadata:
  name: single-job
spec:
  backoffLimit: 6 # number of retries before throwing error
  activeDeadlineSeconds: 10 # time to allow job to run
  template:
    metadata:
      labels:
        app: kubernetes-series
        tier: job
    spec:
      restartPolicy: OnFailure
      containers:
        - name: job
          image: gcr.io/PROJECT_NAME/batchjob-container-job:latest
          # environment variables for the Pod
          env:
          - name: GCLOUD_PROJECT
            value: PROJECT_NAME
          - name: MESSAGE
            value: I am a single run job
          - name: FOREIGN_SERVICE
            value: http://endpoints.default.svc.cluster.local/single
          - name: NODE_ENV
            value: production
          ports:
          - containerPort: 80

one-time job

View you will see the new single-job Pod in your Workloads and 0/1 Pods active. That is because the single-job ran and terminated the Pod when complete

增加completions参数,batch-job to run 3 times, one after the other

apiVersion: batch/v1
kind: Job
metadata:
  name: sequential-job
spec:
  completions: 3 # number of times to run
  backoffLimit: 6 # number of retries before throwing error
  activeDeadlineSeconds: 10 # time to allow job to run
  template:
    metadata:
      labels:
        app: kubernetes-series
        tier: job
    spec:
      restartPolicy: OnFailure
      containers:
        - name: job
          image: gcr.io/PROJECT_NAME/batchjob-container-job:latest
          # environment variables for the Pod
          env:
          - name: GCLOUD_PROJECT
            value: PROJECT_NAME
          - name: MESSAGE
            value: I am a sequential run job
          - name: FOREIGN_SERVICE
            value: http://endpoints.default.svc.cluster.local/sequential
          - name: NODE_ENV
            value: production
          ports:
          - containerPort: 80

增加parallelism参数,pod同时运行数

  parallelism: 2 # number of pods that can run in parallel

Edit page
Share this post on:

Previous Post
Debugging simple memory leak in go
Next Post
kubeflow pipline deploy