在Kubernetes docker-compose中创建CRON作业之间的依赖关系

sdnqo3pr  于 2023-11-17  发布在  Docker
关注(0)|答案(1)|浏览(105)

在我目前的项目中,Kubernetes中计划了多个Cron作业。每个Cron作业都有一个特定的开始时间。但是根据一个新的要求,我们需要在作业之间创建依赖关系,这样作业就不会等到计划的时间,而是应该在前一个作业完成时立即开始。
例如,假设我们有4个CRON作业(a1,a2,a3,a4)计划在下午1点,下午2点,下午3点和下午4点开始。但是假设a2在10分钟内完成(下午2点开始,下午2点10分结束)。a3不是等到下午3点,它可以在下午2点10分触发吗?如果可以,我们如何实现这一点?
我当前的docker-compose.kubernetes.yml文件看起来像这样-

version: "3.9"

services:
  purelit-run-migrations:
    image: artifactory.castecosys.com:8443/docker-python-debian:1.1.0
    env_file:
      - standard-model-environment
    labels:
      service: 'purelit'
      type: 'cron'
      kubernetes.schedule: "30 9 1 1 *"
    deploy:
      resources:
        limits:
          memory: "300M"
          cpus: '0.4'
        reservations:
          cpus: '0.4'
          memory: "300M"
    command: ["/app/purelit/bin/python" ,"/app/utils/init_schema.py", "--script_start_index","105", "--script_end_index","105"]

  a1-purelit-copy-sftp-files:
    image: artifactory.castecosys.com:8443/docker-python-debian:1.1.0
    # restart: on-failure
    env_file:
      - standard-model-environment
    labels:
      service: 'purelit'
      type: 'cron'
      kubernetes.schedule: "30 8 * * *"
    deploy:
      resources:
        limits:
          memory: "4000M"
          cpus: '1.0'
        reservations:
          cpus: '1.0'
          memory: "4000M"
    command: ["/app/purelit/bin/python" ,"/app/utils/copy_files.py"]

  a2-purelit-supply-loader:
    image: artifactory.castecosys.com:8443/docker-python-debian:1.1.0
    # restart: on-failure
    env_file:
      - standard-model-environment
    labels:
      service: 'purelit'
      type: 'cron'
      kubernetes.schedule: "30 9 * * *"
    deploy:
      resources:
        limits:
          memory: "4000M"
          cpus: '1.0'
        reservations:
          cpus: '1.0'
          memory: "4000M"
    command: ["/app/purelit/bin/python" ,"/app/loader/supply_data/supply_data_pipeline_runner.py"]

  a3-purelit-supply-delta-export:
    image: artifactory.castecosys.com:8443/docker-python-debian:1.1.0
    # restart: on-failure
    env_file:
      - standard-model-environment
    labels:
      service: 'purelit'
      type: 'cron'
      kubernetes.schedule: "30 10 * * *"
    deploy:
      resources:
        limits:
          memory: "4000M"
          cpus: '1.2'
        reservations:
          cpus: '1.2'
          memory: "4000M"
    command: ["/app/purelit/bin/python" ,"/app/delta/export/etl_supply_to_app_data_sync_pipeline_runner.py", "--source","all"]

字符串
在上面的代码中,我们可以做什么配置来实现CRON作业依赖?
如果没有CRON工作,那么它如何能够实现其他。

2guxujil

2guxujil1#

这可以通过CronJob使用RBAC启动/创建其他作业来实现:
CronJob:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cron-job
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: job-service-account
          containers:
          - name: cron-container
            image: your-cron-image:tag
            command: ["sh", "-c", "your-cron-command && kubectl create job --from=cronjob/cron-job some-job"]

字符串
其他工作(或职位):

apiVersion: batch/v1
kind: Job
metadata:
  name: some-job
spec:
  template:
    spec:
      serviceAccountName: job-service-account
      containers:
      - name: some-container
        image: your-image:tag
        command: ["your-command"]


RBAC(需要允许cronjob创建其他作业):

apiVersion: v1
kind: ServiceAccount
metadata:
  name: job-service-account

---

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: job-role
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list", "create"]

---

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: job-role-binding
subjects:
- kind: ServiceAccount
  name: job-service-account
  namespace: default
roleRef:
  kind: Role
  name: job-role
  apiGroup: rbac.authorization.k8s.io

相关问题