文档

使用ACK One分布式工作流集群构建容器镜像CI

更新时间:

本文介绍如何使用ACK One分布式工作流集群构建容器镜像CI。

前提条件

使用限制

暂不支持通过修改代码来自动触发CI流程。

ACK One分布式工作流集群功能优势

展开查看ACK One分布式工作流集群功能优势

工作流集群基于开源Argo Workflow项目构建,完全符合开源工作流标准,如果您已在ACK集群或者其他Kubernetes集群运行Argo工作流,无需修改现有工作流,可以无缝迁移至工作流集群

通过工作流集群,您可以轻松编排工作流,每个工作流步骤使用容器运行,可以在短时间内轻松运行大规模机器学习或数据处理的计算密集型作业,可以快速运行CI/CD流水线。

  • 基于开源Argo Workflow,无需修改现有Argo工作流可无缝迁移。

  • 无运维成本,专注工作流运行。

  • 极致弹性,自动扩展,优化计算成本。

  • 可靠性高,多可用区负载均衡,调度可靠性高。

  • 增强控制面,性能、效率、稳定性、可观测性大幅提升。

构建容器镜像CI

本文在ACK One分布式工作流集群基础上,使用BuildKit实现容器镜像的构建和推送,本实践使用的镜像为docker hub https://hub.docker.com/r/moby/buildkitmoby/buildkit:v0.12.1-rootless

步骤一:工作流集群中创建Secret保存ACR EE密码

  1. 为ACR企业版实例(ACR EE)配置访问凭证。具体操作,请参见配置访问凭证

  2. 执行以下命令,在工作流集群中创建Secret保存ACR EE密码,供BuildKit使用。

    替换以下代码中的${workflow_kubeconfig}$username:$password为您的实际配置信息。

    # repositoryDomain: 例如demo-test-registry.cn-hangzhou.cr.aliyuncs.com
    kubectl --kubeconfig ${workflow_kubeconfig} create secret generic docker-config --from-literal="config.json={\"auths\": {\"$repositoryDomain\": {\"auth\": \"$(echo -n $username:$password|base64)\"}}}"

步骤二:在工作流集群中创建WorkflowTemplate

WorkflowTemplate主要用于定义Git Clone代码、构建和推送镜像。

  • Clone代码:本文采用Public仓库,如果是私有仓库,可以使用token来拉取,如git clone https://[username]:[token]@gitlab.com/demo3624733/echo-server.git

  • 构建和推送镜像:本档使用BuildKit来构建镜像。

本文通过不使用共享存储和使用NAS共享存储两种方式介绍如何创建WorkflowTemplate。

不使用共享存储

通过此方式可以快速构建CI流水线,无需关注共享存储的使用。此处使用initContainer用于将代码Clone到emptyDir,之后进入相应的临时目录构建镜像。

  1. 使用以下YAML内容,创建worktemplate-1.yaml文件。

    您可以在以下WorkflowTemplate的parameters字段中指定代码仓库、分支及镜像等,并将您的username和Token替换到以下YAML文件中。

    展开查看worktemplate-1.yaml文件

    apiVersion: argoproj.io/v1alpha1
    kind: WorkflowTemplate
    metadata:
      name: build-echo-server-2
    spec:
      arguments:
        parameters:
          - name: repo
            value: https://github.com/ivan-cai/echo-server.git
          - name: branch
            value: main
          - name: path
            value: echo-server
          - name: image
            value: demo-test-registry.cn-hangzhou.cr.aliyuncs.com/cidemo/echo-server:v2-argo-08231710
      entrypoint: main
      volumes:
        - name: work
          emptyDir: {}
      templates:
        - name: main
          inputs:
            parameters:
              - name: repo
              - name: branch
              - name: path
              - name: image
          # Mount the configuration so we can push the image.
          # This should create the /.docker/config.json file.
          volumes:
            - name: docker-config
              secret:
                secretName: docker-config
          initContainers:
          - name: git-clone
            image: alpine/git:v2.26.2
            volumeMounts:
              - mountPath: /work
                name: work
            workingDir: /work/
            command:
              - git
            args:
              - clone
              - --depth
              - "1"
              - --branch
              - "{{inputs.parameters.branch}}"
              - --single-branch
              - "{{inputs.parameters.repo}}"
          container:
            readinessProbe:
              exec:
                command: [ sh, -c, "buildctl debug workers" ]
            image: moby/buildkit:v0.12.1-rootless
            volumeMounts:
              - name: work
                mountPath: /work
              - name: docker-config
                mountPath: /.docker
            workingDir: /work/{{inputs.parameters.path}}
            env:
              - name: BUILDKITD_FLAGS
                value: --oci-worker-no-process-sandbox
              - name: DOCKER_CONFIG
                value: /.docker
            command:
              - buildctl-daemonless.sh
            args:
              - build
              - --frontend
              - dockerfile.v0
              - --local
              - context=.
              - --local
              - dockerfile=.
              - --output
              - type=image,name={{inputs.parameters.image}},push=true
              - build-arg:GOPROXY=http://goproxy.cn,direct
  2. 执行以下命令,在工作流集群中创建WorkflowTemplate。

    kubectl --kubeconfig ${ackone_argo_kubeconfig} apply -f worktemplate-1.yaml

使用NAS共享存储

此场景中,Git Clone和构建镜像是两个单独的Pod,所以需要共享存储。关于如何在工作流集群使用NAS存储卷的操作,请参见使用存储卷

  1. 使用以下YAML内容,创建worktemplate-2.yaml文件。

    您可以在以下WorkflowTemplate的parameters字段中指定代码仓库、分支及镜像等,并将您的username和Token替换到以下YAML文件中。

    展开查看worktemplate-2.yaml文件

    apiVersion: argoproj.io/v1alpha1
    kind: WorkflowTemplate
    metadata:
      name: build-echo-server
    spec:
      arguments:
        parameters:
          - name: repo
            value: https://github.com/ivan-cai/echo-server.git
          - name: branch
            value: main
          - name: path
            value: echo-server
          - name: image
            value: demo-test-registry.cn-hangzhou.cr.aliyuncs.com/cidemo/echo-server:v2-argo
      entrypoint: main
      volumes:
        - name: work
          persistentVolumeClaim:
            claimName: pvc-nas
      templates:
        - name: main
          dag:
            tasks:
              - name: clone
                template: clone
                arguments:
                  parameters:
                    - name: repo
                      value: "{{workflow.parameters.repo}}"
                    - name: branch
                      value: "{{workflow.parameters.branch}}"
              - name: image
                template: image
                arguments:
                  parameters:
                    - name: path
                      value: "{{workflow.parameters.path}}"
                    - name: image
                      value: "{{workflow.parameters.image}}"
                depends: "clone"
        - name: clone
          inputs:
            parameters:
              - name: repo
              - name: branch
          container:
            volumeMounts:
              - mountPath: /work
                name: work
            image: alpine/git:v2.26.2
            workingDir: /work/
            # Do a shallow clone, which is the fastest way to clone, by using the
            # --depth, --branch, and --single-branch options
            command:
              - sh
              - -c
              - |
                if [ -d "{{workflow.parameters.path}}" ];then
                  rm -rf {{workflow.parameters.path}}
                fi
                git clone --depth 1 --branch {{inputs.parameters.branch}} --single-branch {{inputs.parameters.repo}}
        - name: image
          inputs:
            parameters:
              - name: path
              - name: image
          # Mount the configuration so we can push the image.
          # This should create the /.docker/config.json file.
          volumes:
            - name: docker-config
              secret:
                secretName: docker-config
          container:
            readinessProbe:
              exec:
                command: [ sh, -c, "buildctl debug workers" ]
            image: moby/buildkit:v0.9.3-rootless
            volumeMounts:
              - name: work
                mountPath: /work
              - name: docker-config
                mountPath: /.docker
            workingDir: /work/{{inputs.parameters.path}}
            env:
              - name: BUILDKITD_FLAGS
                value: --oci-worker-no-process-sandbox
              - name: DOCKER_CONFIG
                value: /.docker
            command:
              - buildctl-daemonless.sh
            args:
              - build
              - --frontend
              - dockerfile.v0
              - --local
              - context=.
              - --local
              - dockerfile=.
              - --output
              - type=image,name={{inputs.parameters.image}},push=true
              - build-arg:GOPROXY=http://goproxy.cn,direct
  2. 执行以下命令,在工作流集群中创建WorkflowTemplate。

    kubectl --kubeconfig ${ackone_argo_kubeconfig} apply -f worktemplate-2.yaml

步骤三:创建WorkFlow

  1. 使用以下YAML内容,创建workflow.yaml文件。

    apiVersion: argoproj.io/v1alpha1
    kind: Workflow
    metadata:
      annotations:
        workflows.argoproj.io/pod-name-format: v1
      generateName: echo-server-
    spec:
      workflowTemplateRef:
        name: echo-server
  2. 执行以下命令,在工作流集群中创建WorkFlow。

    kubectl --kubeconfig ${ackone_argo_kubeconfig} create -f workflow.yaml

步骤四:通过ACK One控制台查看工作流

公网访问服务开通后,您可以通过ACK One控制台更快捷地访问Agro控制台。

  1. 登录ACK One控制台,在左侧导航栏选择工作流集群

  2. 基础信息页签下,找到常用操作区域,单击工作流控制台(Argo)

  3. 在工作流控制台页面左侧,选择NAMESPACEdefault,查看工作流列表。

  • 本页导读 (1)
文档反馈