Argo Workflows 是容器原生的工作流引擎,用于编排K8S上的并行作业。 工作流引擎以K8s CRD (Custom Resource Definition) 的形式实现。
(1)常见命令
子命令 | 说明 |
---|---|
submit | 提交工作流 |
list | 列举工作流 |
get | 获取工作流的详细信息 |
logs | 获取工作流的日志 |
suspend | 暂停 |
resume | 恢复 |
delete | 删除 |
retry | 重试 |
terminate | 终止 |
wait | 等待 |
resubmit | 重新提交 |
… | … |
(2)快速示例
argo submit -n argo --watch https://raw.githubusercontent.com/argoproj/argo/master/examples/hello-world.yaml
argo list
1 apiVersion: argoproj.io/v1alpha1
2 kind: Workflow
3 metadata:
4 generateName: hello-world-
5 spec:
6 entrypoint: whalesay
7 templates:
8 - name: whalesay
9 container:
10 image: docker/whalesay
11 command: [cowsay]
12 args: ["hello world"]
13 resources:
14 limits:
15 memory: 32Mi
16 cpu: 100m
1 apiVersion: argoproj.io/v1alpha1
2 kind: Workflow
3 metadata:
generateName: hello-world-parameters-
5 spec:
6 entrypoint: whalesay
arguments:
parameters:
- name: message
value: hello world
7 templates:
8 - name: whalesay
inputs:
parameters:
- name: message
9 container:
10 image: docker/whalesay
11 command: [cowsay]
args: ["{{inputs.parameters.message}}"]
# 提交时指定参数值
argo submit above.yaml -p message="goodbye world"
定义多个步骤,并指定执行顺序(串行或并行)。
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: steps-
spec:
entrypoint: hello-hello-hello
templates:
- name: hello-hello-hello # 1/2: define steps
steps:
- - name: hello1 ## step1
template: whalesay
arguments:
parameters:
- name: message
value: "hello1"
- - name: hello2a ## step2a
template: whalesay
arguments:
parameters:
- name: message
value: "hello2a"
- name: hello2b ## step2b
template: whalesay
arguments:
parameters:
- name: message
value: "hello2b"
- name: whalesay # 2/2: exec steps
inputs:
parameters:
- name: message
container:
image: docker/whalesay
command: [cowsay]
args: ["{{inputs.parameters.message}}"]
定义多个任务,并以DAG声明的任务间依赖关系作为执行顺序。
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: dag-diamond-
spec:
entrypoint: diamond
templates:
- name: echo
inputs:
parameters:
- name: message
container:
image: alpine:3.7
command: [echo, "{{inputs.parameters.message}}"]
- name: diamond
dag: ########################################
tasks:
- name: A
template: echo
arguments:
parameters: [{name: message, value: A}]
- name: B
dependencies: [A]
template: echo
arguments:
parameters: [{name: message, value: B}]
- name: C
dependencies: [A]
template: echo
arguments:
parameters: [{name: message, value: C}]
- name: D
dependencies: [B, C]
template: echo
arguments:
parameters: [{name: message, value: D}]
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: volumes-pvc-
spec:
entrypoint: volumes-pvc-example
volumeClaimTemplates: ############ claim PVC
- metadata:
name: workdir
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
templates:
- name: volumes-pvc-example
steps:
- - name: generate
template: whalesay
- - name: print
template: print-message
- name: whalesay
container:
image: docker/whalesay:latest
command: [sh, -c]
args: ["echo generating message in volume; cowsay hello world | tee /mnt/vol/hello_world.txt"]
volumeMounts: ### mount
- name: workdir
mountPath: /mnt/vol
- name: print-message
container:
image: alpine:latest
command: [sh, -c]
args: ["echo getting message from volume; find /mnt/vol; cat /mnt/vol/hello_world.txt"]
volumeMounts: ### mount
- name: workdir
mountPath: /mnt/vol
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: scripts-bash-
spec:
entrypoint: bash-script-example
templates:
- name: bash-script-example
steps:
- - name: generate
template: gen-random-int-bash
- - name: print
template: print-message
arguments:
parameters:
- name: message
value: "{{steps.generate.outputs.result}}"
- name: gen-random-int-bash ###### 1
script:
image: debian:9.4
command: [bash]
source: # shell scripts
cat /dev/urandom | od -N2 -An -i | awk -v f=1 -v r=100 '{printf "%i\n", f + r * $1 / 65536}'
- name: gen-random-int-python ###### 2
script:
image: python:alpine3.6
command: [python]
source: | # python scripts
import random
i = random.randint(1, 100)
print(i)
- name: print-message ######
inputs:
parameters:
- name: message
container:
image: alpine:latest
command: [sh, -c]
args: ["echo result was: {{inputs.parameters.message}}"]
Exit handler是工作流最后必需执行的模板。
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: exit-handlers-
spec:
entrypoint: intentional-fail
onExit: exit-handler ############ claim exit handler
templates:
- name: intentional-fail
container:
image: alpine:latest
command: [sh, -c]
args: ["echo intentional failure; exit 1"]
- name: exit-handler ############ exit handler
steps:
- - name: notify
template: send-email
- name: celebrate
template: celebrate
when: "{{workflow.status}} == Succeeded"
- name: cry
template: cry
when: "{{workflow.status}} != Succeeded"
- name: send-email
container:
image: alpine:latest
command: [sh, -c]
args: ["echo send e-mail: {{workflow.name}} {{workflow.status}}"]
- name: celebrate
container:
image: alpine:latest
command: [sh, -c]
args: ["echo hooray!"]
- name: cry
container:
image: alpine:latest
command: [sh, -c]
args: ["echo boohoo!"]