本文介绍如何使用Argo Workflow来周期运行一个脚本,包括shell脚本和python脚本。
定义CronWorkflow配置文件
python脚本定时任务yaml模版
apiVersion: argoproj.io/v1alpha1
kind: CronWorkflow
metadata:
name: cron-python
spec:
schedule: "* * * * *"
timezone: "Asia/Shanghai" # Default to local machine timezone
startingDeadlineSeconds: 0
concurrencyPolicy: "Replace" # Default to "Allow"
successfulJobsHistoryLimit: 4 # Default 3
failedJobsHistoryLimit: 4 # Default 1
suspend: false # Set to "true" to suspend scheduling
workflowSpec:
entrypoint: whalesay
templates:
- name: whalesay
script:
image: python:alpine3.6
command: [python]
source: |
import random
i = random.randint(1, 100)
print(i)
print("hello everybody!")
shell脚本定时任务yaml模版
apiVersion: argoproj.io/v1alpha1
kind: CronWorkflow
metadata:
name: cron-shell
spec:
schedule: "* * * * *"
timezone: "Asia/Shanghai" # Default to local machine timezone
startingDeadlineSeconds: 0
concurrencyPolicy: "Replace" # Default to "Allow"
successfulJobsHistoryLimit: 4 # Default 3
failedJobsHistoryLimit: 4 # Default 1
suspend: false # Set to "true" to suspend scheduling
workflowSpec:
entrypoint: whalesay
templates:
- name: whalesay
script:
image: docker/whalesay:latest
command:
- sh
source: |
echo "hello everybody!"
主要修改:
| metadata.name | 你自己的任务名称 |
|---|---|
| schedule | Cron 表达式 --- 定时任务周期可以使用工具tool.lu/crontab/ 测试cron表达式 |
| source | 拷贝你自己的脚本内容 |
将python脚本内容放到容器镜像中
脚本目录添加Dockerfile
FROM python:3.7
WORKDIR /usr/src/app
COPY . .
RUN pip install -r requirements.txt
CMD ["onCallJob.py"]
ENTRYPOINT ["python3"]
docker build 得到镜像
docker build -t xxxxxx/oncallJob:v0.0.1 .
docker push xxxxxx/oncallJob:v0.0.1
编写CronWorkflow
apiVersion: argoproj.io/v1alpha1
kind: CronWorkflow
metadata:
name: oncalljob
spec:
schedule: "0 9 * * 1"
timezone: "Asia/Shanghai" # Default to local machine timezone
startingDeadlineSeconds: 0
concurrencyPolicy: "Replace" # Default to "Allow"
successfulJobsHistoryLimit: 4 # Default 3
failedJobsHistoryLimit: 4 # Default 1
suspend: false # Set to "true" to suspend scheduling
workflowSpec:
entrypoint: main
templates:
- name: main
container:
image: xxxxxx/oncallJob:v0.0.1
imagePullPolicy: Always
container.image配置上面构建出的镜像