Argo Workflows
是一个开源容器原生工作流引擎,用于在 Kubernetes 上编排并行作业。Argo 工作流作为 Kubernetes CRD(自定义资源定义)实现WorkflowTemplate
、Workflow
、template
,这些资源的命名有些相似,注意分辨。kubectl create ns argo
kubectl apply -n argo -f https://raw.githubusercontent.com/argoproj/argo-workflows/master/manifests/quick-start-postgres.yaml
# kubectl get po -n argo
NAME READY STATUS RESTARTS AGE
argo-server-574ddc66b-62rjc 1/1 Running 4 4h25m
minio 1/1 Running 0 4h25m
postgres-56fd897cf4-k8fwd 1/1 Running 0 4h25m
workflow-controller-77658c77cc-p25ll 1/1 Running 4 4h25m
##
argo-server是argo服务端
mino是进行制品仓库
postgres是数据库
workflow-controller是流程控制器
# Download the binary
curl -sLO https://github.com/argoproj/argo/releases/download/v3.0.0-rc4/argo-linux-amd64.gz
# Unzip
gunzip argo-linux-amd64.gz
# Make binary executable
chmod +x argo-linux-amd64
# Move binary to path
mv ./argo-linux-amd64 /usr/local/bin/argo
# version
argo version
# 主要命令
list 列出工作流
logs 查看工作流的日志
submit 创建工作流
watch 实时监听工作流
get 现实详细信息
delete 删除工作流
stop 停止工作流
# workflows配置清单
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: hello-world-
labels:
workflows.argoproj.io/archive-strategy: "false"
spec:
entrypoint: whalesay
templates:
- name: whalesay
container:
image: docker/whalesay:latest
command: [cowsay]
args: ["hello world"]
# 创建并观察
argo submit -n argo helloworld.yaml --watch
Name: hello-world-9pw7v
Namespace: argo
ServiceAccount: default
Status: Succeeded
Conditions:
Completed True
Created: Mon Mar 08 14:51:35 +0800 (10 seconds ago)
Started: Mon Mar 08 14:51:35 +0800 (10 seconds ago)
Finished: Mon Mar 08 14:51:45 +0800 (now)
Duration: 10 seconds
Progress: 1/1
ResourcesDuration: 4s*(1 cpu),4s*(100Mi memory)
STEP TEMPLATE PODNAME DURATION MESSAGE
✔ hello-world-9pw7v whalesay hello-world-9pw7v 5s
# 查看状态
# argo list -n argo
NAME STATUS AGE DURATION PRIORITY
hello-world-9pw7v Succeeded 1m 10s 0
# 查看日志
argo logs -n argo hello-world-9pw7v
Workflow是Argo中最重要的资源,其主要有两个重要功能:
要执行的工作流定义在Workflow.spec
字段中,其主要包括templates
和entrypoint
在一个 Workflow 中,其 spec 中有一个名为 templates 的字段,在其中至少需要一个 template 作为其组成的任务
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: hello-world- # Workflow的配置名称
spec:
entrypoint: whalesay # 解析whalesay templates
templates:
- name: whalesay # 定义whalesay templates,和entrypoint保持一致
container: # 定义一个容器,输出"helloworld"
image: docker/whalesay
command: [cowsay]
args: ["hello world"]
- name: whalesay
container:
image: docker/whalesay
command: [cowsay]
args: ["hello world"]
source
字段用于自定义脚本 - name: gen-random-int
script:
image: python:alpine3.6
command: [python]
source: |
import random
i = random.randint(1, 100)
print(i)
脚本的输出结果会根据调用方式自动导出到{{tasks.<NAME>.outputs.result}}或{{steps.<NAME>.outputs.result}}中
get, create, apply, delete, replace, patch
集群资源。如下在集群中创建一个ConfigMap类型资源: - name: k8s-owner-reference
resource:
action: create
manifest: |
apiVersion: v1
kind: ConfigMap
metadata:
generateName: owned-eg-
data:
some: value
argo resume
进行恢复。定义格式如下: - name: delay
suspend:
duration: "20s"
在 Steps 中,--
代表顺序执行,-
代表并行执行
Steps主要是通过定义一系列步骤来定义任务,其结构是"list of lists",外部列表将顺序执行,内部列表将并行执行。如下:
- name: hello-hello-hello
steps:
- - name: step1
template: prepare-data
- - name: step2a
template: run-data-first-half
- name: step2b
template: run-data-second-half
其中step1和step2a是顺序执行,而step2a和step2b是并行执行
- name: diamond
dag:
tasks:
- name: A
template: echo
- name: B
dependencies: [A]
template: echo
- name: C
dependencies: [A]
template: echo
- name: D
dependencies: [B, C]
template: echo
其中A会立即执行,B和C会依赖A,D依赖B和C
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: dag-diamond-
spec:
entrypoint: diamond
templates:
- 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}]
- name: echo
inputs:
parameters:
- name: message
container:
image: alpine:3.7
command: [echo, "{{inputs.parameters.message}}"]
when
用于判断apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: coinflip-
spec:
entrypoint: coinflip
templates:
- name: coinflip
steps:
- - name: flip-coin
template: flip-coin
- - name: heads
template: heads
when: "{{steps.flip-coin.outputs.result}} == heads"
- name: tails
template: tails
when: "{{steps.flip-coin.outputs.result}} == tails"
- name: flip-coin
script:
image: python:alpine3.6
command: [python]
source: |
import random
result = "heads" if random.randint(0,1) == 0 else "tails"
print(result)
- name: heads
container:
image: alpine:3.6
command: [sh, -c]
args: ["echo \"it was heads\""]
- name: tails
container:
image: alpine:3.6
command: [sh, -c]
args: ["echo \"it was tails\""]
withItems
用于循环apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: loops-
spec:
entrypoint: loop-example
templates:
- name: loop-example
steps:
- - name: print-message
template: whalesay
arguments:
parameters:
- name: message
value: "{{item}}"
withItems:
- hello world
- goodbye world
- name: whalesay
inputs:
parameters:
- name: message
container:
image: docker/whalesay:latest
command: [cowsay]
args: ["{{inputs.parameters.message}}"]
generate-parameter
通过whalesay
输出hello world
到/tmp/hello_world.txt
上并作为outputs输出。而print-message直接读取了generate-parameter
outputs作为参数当作inputstemplates:
- name: output-parameter
steps:
- - name: generate-parameter
template: whalesay
- - name: consume-parameter
template: print-message
arguments:
parameters:
- name: message
value: "{{steps.generate-parameter.outputs.parameters.hello-param}}"
- name: whalesay
container:
image: docker/whalesay:latest
command: [sh, -c]
args: ["echo -n hello world > /tmp/hello_world.txt"]
outputs:
parameters:
- name: hello-param
valueFrom:
path: /tmp/hello_world.txt
- name: print-message
inputs:
parameters:
- name: message
container:
image: docker/whalesay:latest
command: [cowsay]
args: ["{{inputs.parameters.message}}"]
generate-artifact
任务完成后output输出一个名为hello-art
的制品,这个制品会把/tmp/hello_world.txt
这个文件打包后上传到制品库中,默认制品库可以通过configmap配置,通常是放在S3上apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: artifact-example-
spec:
entrypoint: main
templates:
- name: main
steps:
- - name: generate-artifact
template: whalesay
- - name: consume-artifact
template: print-message
arguments:
artifacts:
- name: message
from: "{{steps.generate-artifact.outputs.artifacts.hello-art}}"
- name: whalesay
container:
image: docker/whalesay:latest
command: [sh, -c]
args: ["sleep 1; cowsay hello world | tee /tmp/hello_world.txt"]
outputs:
artifacts:
- name: hello-art
path: /tmp/hello_world.txt
- name: print-message
inputs:
artifacts:
- name: message
path: /tmp/message
container:
image: alpine:latest
command: [sh, -c]
args: ["cat /tmp/message"]
spec
字段定义arguments
,定义变量名message
,其值是hello world
,然后在templates
字段中需要先定义一个inputs
字段,用于templates
的输入参数,然后在使用"{{}}"
形式引用变量apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: hello-world-parameters-
spec:
entrypoint: whalesay
arguments:
parameters:
- name: message
value: hello world
templates:
- name: whalesay
inputs:
parameters:
- name: message
container:
image: docker/whalesay
command: [ cowsay ]
args: [ "{{inputs.parameters.message}}" ]
filter([1, 2], { # > 1})
asInt(inputs.parameters["my-int-param"])
asFloat(inputs.parameters["my-float-param"])
string(1)
toJson([1, 2])
WorkflowTemplate
相当于 Workflow
的模板库,和 Workflow
一样,也由template
组成。用户在创建完WorkflowTemplate
后,可以通过直接提交它们来执行 Workflow
WorkflowTemplate
和template
的区别:
在2.7版本后,WorkflowTemplate
的定义和Workflow
的定义一样,可以简单的将kind:Workflow
改成kind:WorkflowTemplate
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
name: workflow-template-1
spec:
entrypoint: whalesay-template
arguments:
parameters:
- name: message
value: hello world
templates:
- name: whalesay-template
inputs:
parameters:
- name: message
container:
image: docker/whalesay
command: [cowsay]
args: ["{{inputs.parameters.message}}"]
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: workflow-template-hello-world-
spec:
entrypoint: whalesay
templates:
- name: whalesay
steps: # 引用模板必须在steps/dag/template下
- - name: call-whalesay-template
templateRef: # 应用模板字段
name: workflow-template-1 # 引用的WorkflowTemplate名
template: whalesay-template # WorkflowTemplate中具体的template名
arguments: # 参数
parameters:
- name: message
value: "hello world"
ClusterWorkflowTemplate
创建的是一个集群范围内的WorkflowTemplate
,其他workflow
可以引用它,其他和WorkflowTemplate
是一样的apiVersion: argoproj.io/v1alpha1
kind: ClusterWorkflowTemplate
metadata:
name: cluster-workflow-template-whalesay-template
spec:
templates:
- name: whalesay-template
inputs:
parameters:
- name: message
container:
image: docker/whalesay
command: [cowsay]
args: ["{{inputs.parameters.message}}"]
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: workflow-template-hello-world-
spec:
entrypoint: whalesay
templates:
- name: whalesay
steps:
- - name: call-whalesay-template
templateRef: # 引用模板
name: cluster-workflow-template-whalesay-template # ClusterWorkflow名
template: whalesay-template # 具体的模板名
clusterScope: true # 表示是ClusterWorkflow
arguments: # 参数
parameters:
- name: message
value: "hello world"
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
annotations:
workflows.argoproj.io/description: |
Checkout out from Git, build and deploy application.
workflows.argoproj.io/maintainer: '@joker'
workflows.argoproj.io/tags: java, git
workflows.argoproj.io/version: '>= 2.9.0'
name: devops-java
spec:
entrypoint: main
arguments:
parameters:
- name: repo
value: gitlab-test.coolops.cn:32080/root/springboot-helloworld.git
- name: branch
value: master
- name: image
value: registry.cn-hangzhou.aliyuncs.com/rookieops/myapp:202103101613
- name: cache-image
value: registry.cn-hangzhou.aliyuncs.com/rookieops/myapp
- name: dockerfile
value: Dockerfile
- name: devops-cd-repo
value: gitlab-test.coolops.cn:32080/root/devops-cd.git
- name: gitlabUsername
value: devops-argo
- name: gitlabPassword
value: devops123456
templates:
- name: main
steps:
- - name: Checkout
template: Checkout
- - name: Build
template: Build
- - name: BuildImage
template: BuildImage
- - name: Deploy
template: Deploy
# 拉取代码
- name: Checkout
script:
image: registry.cn-hangzhou.aliyuncs.com/rookieops/maven:3.5.0-alpine
workingDir: /work
command:
- sh
source: |
git clone --branch {{workflow.parameters.branch}} http://{{workflow.parameters.gitlabUsername}}:{{workflow.parameters.gitlabPassword}}@{{workflow.parameters.repo}} .
volumeMounts:
- mountPath: /work
name: work
# 编译打包
- name: Build
script:
image: registry.cn-hangzhou.aliyuncs.com/rookieops/maven:3.5.0-alpine
workingDir: /work
command:
- sh
source: mvn -B clean package -Dmaven.test.skip=true -Dautoconfig.skip
volumeMounts:
- mountPath: /work
name: work
# 构建镜像
- name: BuildImage
volumes:
- name: docker-config
secret:
secretName: docker-config
container:
image: registry.cn-hangzhou.aliyuncs.com/rookieops/kaniko-executor:v1.5.0
workingDir: /work
args:
- --context=.
- --dockerfile={{workflow.parameters.dockerfile}}
- --destination={{workflow.parameters.image}}
- --skip-tls-verify
- --reproducible
- --cache=true
- --cache-repo={{workflow.parameters.cache-image}}
volumeMounts:
- mountPath: /work
name: work
- name: docker-config
mountPath: /kaniko/.docker/
# 部署
- name: Deploy
script:
image: registry.cn-hangzhou.aliyuncs.com/rookieops/kustomize:v3.8.1
workingDir: /work
command:
- sh
source: |
git remote set-url origin http://{{workflow.parameters.gitlabUsername}}:{{workflow.parameters.gitlabPassword}}@{{workflow.parameters.devops-cd-repo}}
git config --global user.name "Administrator"
git config --global user.email "coolops@163.com"
git clone http://{{workflow.parameters.gitlabUsername}}:{{workflow.parameters.gitlabPassword}}@{{workflow.parameters.devops-cd-repo}} /work/devops-cd
cd /work/devops-cd
git pull
cd /work/devops-cd/devops-simple-java
kustomize edit set image {{workflow.parameters.image}}
git commit -am 'image update'
git push origin master
volumeMounts:
- mountPath: /work
name: work
volumeClaimTemplates:
- name: work
metadata:
name: work
spec:
storageClassName: nfs-client-storageclass
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi