OCP CronJob 需要哪些 API

What APIs are required along with OCP CronJob

我有 ConfigMap、ImageStream、BuildConfig、DeploymentConfig API 成功部署了我的应用程序并按要求启动了 pods 个。但是我现在想用CronJob。

是否完全替换 DeploymentConfig?因为这个想法是根据传递到 CronJob API.

的玉米表达式来启动一个新的 pod

是的,为什么不呢,您可以重复使用 DeploymentConfig 的模板部分。例如:

kind: "DeploymentConfig"
apiVersion: "v1"
metadata:
  name: "frontend"
spec:
  template: 
    metadata:
      labels:
        name: "frontend"
    spec:
      containers:
        - name: "helloworld"
          image: "openshift/origin-ruby-sample"
          ports:
            - containerPort: 8080
              protocol: "TCP"
  replicas: 5 
  triggers:
    - type: "ConfigChange" 
    - type: "ImageChange" 
      imageChangeParams:
        automatic: true
        containerNames:
          - "helloworld"
        from:
          kind: "ImageStreamTag"
          name: "origin-ruby-sample:latest"
  strategy: 
    type: "Rolling"
  paused: false 
  revisionHistoryLimit: 2 
  minReadySeconds: 0

会变成这样:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: frontend
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            name: "frontend"
        spec:
          containers:
            - name: "helloworld"
              image: "openshift/origin-ruby-sample"
              ports:
                - containerPort: 8080
                  protocol: "TCP"
          restartPolicy: OnFailure

✌️

Do I replace the DeploymentConfig completely? Because the idea is to launch a new pod according to a corn expression that is passed into the CronJob API.

我不这么认为。基本上,“DeploymentConfig”用于 运行ning “Pod”,“CronJob”用于 运行ning 基于“Job”的一次性“Pod”。所以他们的用例彼此不同。

例如,“DeploymentConfig”具有通过“ImageStream”根据图像变化触发的功能,这要求目标 pod 应该 运行,而不是一次性的。它不适用于“CronJob”。

但是如果你只是想使用“CronJob”而不是没有图像触发功能的“DeploymentConfig”进行pod部署,你还应该考虑如何在“CronJob”上引用“ImageStream”。因为“CronJob”是原生的Kubernetes资源,所以“CronJob”不能直接使用“ImageStream”。 为此,将“alpha.image.policy.openshift.io/resolve-names: '*'”注释添加到“CronJob”,如下所示。请参阅 Using Image Streams with Kubernetes Resources 了解更多详情。

例如>

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: pi
spec:
  schedule: "*/1 * * * *"  
  jobTemplate:             
    spec:
      template:
        metadata:
          annotations:
            alpha.image.policy.openshift.io/resolve-names: '*'  <-- You need this for using ImageStream
          labels:          
            parent: "cronjobpi"
        spec:
          containers:
          - name: pi
            image: "<ImageStream name>"
            command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
          restartPolicy: OnFailure

但是如果您不喜欢使用 ImageStream,您可以像 Rico 提到的那样在“DeploymentConfig”和“CronJob”之间为 pod 的容器部署相同的模板。希望对你有帮助。 :)