快速开始

简要概述

工作流编排。

均基于 “argo workflows/v3.4.17” 版本分析。

安装部署

请先完成 k8s 集群部署,参考 K8S 集群部署 / 快速开始

登录任一控制节点:

kubectl apply -k /etc/kubernetes/addons/argo-workflows/

验证部署详情:

kubectl get pods -n argo -w

工作流类型

Workflow

  1. 它定义了要执行的工作流程;
  2. 它存储工作流的状态。

见数据结构 Workflow,一个最简单的示例:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: hello-world-
spec:
  entrypoint: main
  templates:
  - name: main
    container:
      image: ccr.ccs.tencentyun.com/opsaid/minideb:bullseye
      command: [ echo ]
      args: ["hello world"]

其中用户定义的流程在 “spec.templates” 集合内,“spec.entrypoint” 表示该工作流执行的入口,类似函数的 “main” 功能。

创建语句:

argo submit hello-world.yaml

或者

kubectl create -f hello-world.yaml

WorkflowTemplate

是 “Workflow” 工作流的模版,一般用于创建常用流程,然后根据参数在生成具体的工作流执行任务。

apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: hello-world
  namespace: default
spec:
  entrypoint: main

  arguments:
    parameters:
    - name: name
    - name: price
    - name: from
      value: create by workflow template

  templates:
  - name: main
    inputs:
      parameters:
      - name: name
      - name: price
      - name: from
    container:
      image: ccr.ccs.tencentyun.com/opsaid/minideb:bullseye
      command: [ sh, -c ]
      args:
      - "echo "
      - "hello: {{ inputs.parameters.name }}"
      - "age: {{ inputs.parameters.price }}"
      - "from: {{ inputs.parameters.from }}"
    outputs:
      parameters:
      - name: name
        value: "{{ inputs.parameters.name }}"
      - name: price
        value: "{{ inputs.parameters.price }}"
      - name: from
        value: "{{ inputs.parameters.from }}"

创建语句:

kubectl apply -f hello-world.yaml

ClusterWorkflowTemplate

同 “WorkflowTemplate”,但该资源是 k8s 集群范围内可用,没有用户空间隔离。

apiVersion: argoproj.io/v1alpha1
kind: ClusterWorkflowTemplate
metadata:
  name: hello-world
spec:
  ...

CronWorkflow

按照一定的计划任务去运行工作流。

---
apiVersion: argoproj.io/v1alpha1
kind: CronWorkflow
metadata:
  name: my-test-9
spec:
  schedule: "* * * * *"
  concurrencyPolicy: "Replace"
  startingDeadlineSeconds: 0
  workflowSpec:
    entrypoint: main
    templates:
    - name: main
      container:
        image: ccr.ccs.tencentyun.com/opsaid/minideb:bullseye
        command: [sh, -c]
        args: ["date; sleep 10"]

创建语句:

argo cron create hello-world.yaml

或者

kubectl create -f hello-world.yaml

配置参数与数据结构说明参考 CronWorkflow

任务模版类型

容器类型

通过属性 “spec.templates[_].container” 定义:

...
spec:
  templates:
  - name: tmpl-container
    container:
      image: ccr.ccs.tencentyun.com/opsaid/minideb:bullseye
      command: [ echo ]
      args: ["hello world"]

是最常用的模板类型,它将启动一个容器用于作业执行。

脚本类型

通过属性 “spec.templates[_].script” 定义:

...
spec:
  templates:
  - name: tmpl-script 
    script:
      image: ccr.ccs.tencentyun.com/opsaid/python:alpine3.6
      command:
      - python
      source: |
        import random
        i = random.randint(1, 100)
        print(i)        

启动容器并执行 “spec.templates[_].script.source” 脚本内容,最终把结果写入变量 {{tasks.<NAME>.outputs.result}} 或者 {{steps.<NAME>.outputs.result}} 以供后续流程使用。

资源类型

...
spec:
  templates:
  - name: tmpl-resource
    resource:
      action: create
      manifest: |
        apiVersion: v1
        kind: ConfigMap
        metadata:
          #generateName: owned-eg-
          name: tmpl-resource
        data:
          some: value        

通过属性 “spec.templates[_].resource” 定义。

这里是指 k8s 集群内的资源。

挂起类型

通过属性 “spec.templates[_].suspend” 定义:

...
spec:
  templates:
  - name: tmpl-suspend
    suspend:
      duration: "20s"

对于流水线执行到此步骤进行等待一段时间或者人工审批确认后在执行后续步骤。

步骤类型

通过属性 “spec.templates[_].steps” 定义:

...
spec:
  templates:
  - name: tmpl-steps
    steps:
    - - name: step1
        template: tmpl-container
    - - name: step2
        template: tmpl-script
      - name: step2-1
        template: tmpl-script

把大任务分解为一系列子步骤进行执行,外部列表将顺序运行(两个中横行,既 “- - name:” 下任务),内部列表将并行运行(一个中横行,既 “- name:” 任务,属于某个 “- -” 下的子任务)。如果需要逐一运行内部任务,需使用同步功能。

也可以设置多种选项来控制执行,如 “when:” 子句有条件地执行步骤。

有向无环图

通过属性 “spec.templates[_].dag” 定义:

...
spec:
  templates:
  - name: tmpl-steps
    dag:
      tasks:
      - name: A
        template: tmpl-container
      - name: B
        dependencies: [A]
        template: tmpl-container
      - name: C
        dependencies: [A]
        template: tmpl-container
      - name: D
        dependencies: [B, C]
        template: tmpl-container

可以定义每个任务之间的依赖关系,并设置在特定任务开始之前必须完成哪些其他任务。

HTTP 类型

通过属性 “spec.templates[_].http” 定义:

...
spec:
  entrypoint: main
  templates:
  - name: main
    steps:
    - - name: get-baidu
        template: http
        arguments:
          parameters:
          - name: url
            value: www.baidu.com

  - name: http
    inputs:
      parameters:
      - name: url

    http:
      method: GET
      url: "{{ inputs.parameters.url }}"
      insecureSkipVerify: true
      successCondition: "response.statusCode == 200"

实际上也是以镜像 “quay.io/argoproj/argoexec:v3.4.7” 启动容器,然后执行 http 动作。

容器集合类型

通过属性 “spec.templates[_].containerSet” 定义:

...
spec:
  entrypoint: main
  templates:
  - name: main
    volumes:
    - name: workspace
      emptyDir: {}

    containerSet:
      volumeMounts:
      - mountPath: /workspace
        name: workspace

      containers:
      - name: task-a
        image: ccr.ccs.tencentyun.com/opsaid/minideb:bullseye
        command: [ sh, -c ]
        args: ["echo task-a >> /workspace/hello.txt"]
      - name: task-b
        image: ccr.ccs.tencentyun.com/opsaid/minideb:bullseye
        command: [ sh, -c ]
        args: ["echo task-b >> /workspace/hello.txt"]
      - name: task-c
        image: ccr.ccs.tencentyun.com/opsaid/minideb:bullseye
        command: [ sh, -c ]
        args: ["echo task-c >> /workspace/hello.txt"]
        dependencies: [ task-a, task-b ]
      - name: task-d
        image: ccr.ccs.tencentyun.com/opsaid/minideb:bullseye
        command: [ sh, -c ]
        args: ["cat /workspace/hello.txt"]
        dependencies: [ task-b ]
      - name: main
        image: ccr.ccs.tencentyun.com/opsaid/minideb:bullseye
        command: [ sh, -c ]
        args: ["echo main >> /workspace/hello.txt"]
        dependencies: [task-c, task-d]

    outputs:
      parameters:
      - name: hello
        valueFrom:
          path: /workspace/hello.txt

与以上容器类型区别在于可在单个 Pod 里运行多个容器,这些容器均运行同一个宿主上,所以可以共享存储卷等比较灵活。同时流水线入口步骤必须命名为 “main”。