当前位置: 首页 > 工具软件 > argo-events > 使用案例 >

Argo_notes

梁丘佑运
2023-12-01

Argo学习笔记
argo


Argo包含Workflows,CD,Events和Rollouts四个组件。

1. Argo Workflows

Argo Workflows 是容器原生的工作流引擎,用于编排K8S上的并行作业。 工作流引擎以K8s CRD (Custom Resource Definition) 的形式实现。

1.1 argo: CLI of Argo

(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.2 Argo模板

a. 基础示例

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

b. 参数化

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"

c1. 多步骤

定义多个步骤,并指定执行顺序(串行或并行)。

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}}"]

c2. DAG

定义多个任务,并以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}]

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

e. 脚本

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}}"]

f. 退出

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!"]

g. 循环


h. 分支


i. 递归


2. Argo CD

 类似资料:

相关阅读

相关文章

相关问答