基于ACK One和ACR 构建应用全自动化GitOps交付的实践

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: 本文介绍如何在ACK One实例上构建应用GitOps全自动化交付流水线。

背景信息

您可以使用任意第三方CI系统完成如下CI流程:

image.png

当CI流程成功将应用镜像推送到ACR镜像仓库时,就可以自动化触发CD流程的运行并更新应用容器镜像。本文示例主要演示ACK One GitOps自动监听ACR镜像仓库变化,若有符合过滤条件的容器镜像tags更新,则触发以下流程:

image.png

使用限制

  • 仅适用于通过ACK One GitOps系统创建的应用。
  • 仅适用于通过Kustomize或Helm编排渲染托管在Git系统中的应用编排。
  • 仅对同步策略设置为auto-sync的应用生效。
  • 私有镜像拉取凭证必须与GitOps系统部署在同一集群中,不支持跨集群读取私有镜像拉取凭证。


前置条件


快速开始


本文示例应用的GitHub地址为:https://github.com/AliyunContainerService/gitops-demo.git

由于本示例中涉及将应用变更回写到Git仓库中,所以首先需要您fork以上Git仓库到自己的账号下。例如本次演示将使用fork的仓库地址: https://github.com/haoshuwei/gitops-demo.git


gitops-demo应用的目录结构为:

.
├── manifests
│   └── helm
│       ├── Chart.yaml
│       ├── templates
│       │   ├── deployment.yaml
│       │   ├── _helpers.tpl
│       │   ├── hpa.yaml
│       │   ├── ingress.yaml
│       │   ├── NOTES.txt
│       │   ├── serviceaccount.yaml
│       │   ├── service.yaml
│       │   └── tests
│       │       └── test-connection.yaml
│       ├── values-oversea.yaml
│       └── values.yaml
└── README.md

其中values.yaml文件中,通过image.tag动态渲染应用锁使用的镜像tag:

image:
  repository: registry.cn-hangzhou.aliyuncs.com/haoshuwei24/echo-server
  pullPolicy: IfNotPresent
  # Overrides the image tag whose default is the chart appVersion.
  tag: "v1.0"

步骤一 在GitOps系统中创建应用

连接和访问GitOps系统,可参考 https://help.aliyun.com/document_detail/464255.html#section-q3r-rjg-t9k

Git源仓库添加,可参考 https://help.aliyun.com/document_detail/464259.html

应用创建参考: https://help.aliyun.com/document_detail/464576.html


(1)CLI创建:创建应用gitops-demo,参数详情如下:

argocd app create gitops-demo --repo https://github.com/AliyunContainerService/gitops-demo.git --path  manifests/helm --dest-namespace gitops --dest-server https://xx.xx.XX.XX:6443 --sync-policy none
argocd app sync gitops-demo

(2)UI创建:创建应用gitops-demo,参数详情如下:

image.png

image.png

查看应用运行情况:

image.png


步骤二 配置应用自动更新

当您推送了一个新版本的应用镜像到ACR镜像仓库时,希望GitOps系统可以自动检测到该新版镜像并自动将其更新到实际环境中。在配置应用自动更新之前,您需要先知晓以下内容:

  • GitOps系统中基于ACR镜像仓库变化触发应用自动更新的功能,由argocd-image-updater组件负责,该组件目前处于alpha阶段,建议只在开发测试环境中使用,如需在生产环境中使用,请您仔细评估,风险自担。
  • GitOps系统创建的应用会在argocd命名空间下生成一个Application CR,我们将通过为该Application CR添加annotations来配置应用自动更新。
  • GitOps系统需要您手动配置ACR镜像仓库的登录用户名密码用于监听镜像仓库变化。


(1) 查看gitops-demo应用的Application CR

$ kubectl -n argocd get application gitops-demo -oyaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: gitops-demo
  namespace: argocd
spec:
  destination:
    namespace: gitops
    server: https://x.xxx.xxx.x:6443
  project: default
  source:
    helm:
      valueFiles:
      - values.yaml
    path: manifests/helm
    repoURL: https://github.com/haoshuwei/gitops-demo.git
    targetRevision: HEAD
  syncPolicy:
    automated: {}
    syncOptions:
    - CreateNamespace=true

(2) 为gitops-demo添加annotations

生成imageUpdate.patch:

$ cat <<EOF > imageUpdate.patch
metadata:
  annotations:
    argocd-image-updater.argoproj.io/image-list: echoserver=registry.cn-hangzhou.aliyuncs.com/haoshuwei24/echo-server
    argocd-image-updater.argoproj.io/echoserver.helm.image-tag: image.tag
    argocd-image-updater.argoproj.io/echoserver.update-strategy: semver
    argocd-image-updater.argoproj.io/echoserver.allow-tags: regexp:^v[1-9].*
    argocd-image-updater.argoproj.io/write-back-method: git:secret:argocd/git-creds
EOF

为gitops-demo添加annotations:

$ kubectl -n argocd patch Application gitops-demo --type=merge -p "$(cat imageUpdate.patch)"

(3) 配置访问ACR和Git仓库的凭证

配置访问ACR的凭证:

$ kubectl -n argocd apply -f - <<EOF
apiVersion: v1
kind: Secret
metadata:
  name: acr
type: Opaque
stringData:
  acr: <your_username>:<your_password>  # 将<your_username>:<your_password>更换为您自己的容器镜像仓库访问凭证。
EOF

配置访问Git仓库的凭证:

$ kubectl -n argocd create secret generic git-creds \
--from-literal=username=<your_username> \
--from-literal=password=<your_password>

步骤三 测试应用的自动更新

(1) push新的镜像tag到ACR镜像仓库

$ docker pull registry.cn-hangzhou.aliyuncs.com/haoshuwei24/echo-server:v1.0
$ docker tag registry.cn-hangzhou.aliyuncs.com/haoshuwei24/echo-server:v1.0 registry.cn-hangzhou.aliyuncs.com/haoshuwei24/echo-server:v2.0
$ docker push registry.cn-hangzhou.aliyuncs.com/haoshuwei24/echo-server:v2.0

(2) 执行以下命令,查看argocd命名空间下argocd-image-updater容器的日志。

$ kubectl -n argocd logs -f argocd-server-xxxxxxxxxx-xxxxx -c argocd-image-updater

预期输出:

time="2022-12-29T09:58:13Z" level=info msg="Successfully updated the live application spec" application=gitops-demo
time="2022-12-29T09:58:13Z" level=info msg="Processing results: applications=1 images_considered=1 images_skipped=0 images_updated=1 errors=0"

查看目标集群中的应用镜像是否已自动更新。

(3) GitHub上查看Git仓库中是否自动生成manifests/helm/.argocd-source-gitops-demo.yaml文件(表示回写Git成功)。

image.png

配置应用镜像的自动更新

1. 配置更新指定的容器镜像

您可以通过设置AnnotationGitOps系统中创建的应用标记1个或者多个容器镜像被自动更新,格式如下所示:

argocd-image-updater.argoproj.io/image-list: <image_spec_list>

可以是单个容器镜像,也可以用半角逗号(,)分割的一组容器镜像列表,每个容器镜像描述的格式如下所示:

[<alias_name>=]<image_path>[:<version_constraint>]

是应用的容器镜像别名,以便在做其它配置时引用。别名只允许字母字符串,且只允许在image-listAnnotation中使用。例如本文示例中,指定需要自动更新的镜像为registry.cn-hangzhou.aliyuncs.com/haoshuwei24/echo-server,为其设置的别名为echoserver:

argocd-image-updater.argoproj.io/image-list: echoserver=registry.cn-hangzhou.aliyuncs.com/haoshuwei24/echo-server


2. 镜像tags的条件过滤

registry.cn-hangzhou.aliyuncs.com/haoshuwei24/echo-server镜像为例,您可以设置过滤条件,当且仅当ACR镜像仓库中echo-server镜像有符合过滤规则的新tags推送时,才出发应用的自动更新。

通过正则表达式过滤允许的tags,规范如下所示:

argocd-image-updater.argoproj.io/<image_name>.allow-tags: <match_func>

其中,的格式为regexp:即为标准的正则表达式。

例如本文中的示例为:

argocd-image-updater.argoproj.io/echoserver.allow-tags: regexp:^v[1-9].*

3. 设置容器镜像更新策略

容器镜像的更新策略有以下几种,默认镜像更新策略为semver

更新策略

描述

semver

按语义化版本号排序并更新到最新的镜像tag

latest

按创建时间排序并更新到最新的镜像tag(注意:并非镜像推送到仓库的时间)

name

按字母排序并更新到最新的镜像tag

digest

更新到最新推送的可变标签版本。

Annotation的格式如下所示:

argocd-image-updater.argoproj.io/<image_name>.update-strategy: <strategy>

本示例中使用的镜像更新策略为semver,如下所示:

argocd-image-updater.argoproj.io/echoserver.update-strategy: semver

4. Helm/Kustomize编排的应用参数设置

  • Helm编排的应用参数设置

应用可能包含多个容器镜像的描述,比如gitops-demo示例应用的values.yaml文件中有image.repository image.tag配置,那么Annotations的配置如下所示:

annotations:
  argocd-image-updater.argoproj.io/image-list: echoserver=registry.cn-hangzhou.aliyuncs.com/haoshuwei24/echo-server
  argocd-image-updater.argoproj.io/echoserver.helm.image-name: image.repository
  argocd-image-updater.argoproj.io/echoserver.helm.image-tag: image.tag
  • Kustomize编排的应用参数设置

Kustomize编排类型的应用容器镜像的自动更新设置,需要先设置被更新的容器镜像的别名(可以包含tag),然后设置原始容器镜像地址(不包含tag),Annotation格式如下所示:

annotations:
  argocd-image-updater.argoproj.io/image-list: <image_alias>=<image_name>:<image_tag>
  argocd-image-updater.argoproj.io/<image_alias>.kustomize.image-name: <original_image_name>


配置访问ACR镜像仓库的凭证

您需配置如下访问凭证才能通过GitOps系统自动监听ACR镜像仓库的变化并更新应用。ACR镜像仓库相关的配置保存在argocd命名空间下名为argocd-image-updater-configConfigMap中。

查看默认配置如下所示:

$ kubectl -n argocd get cm argocd-image-updater-config -oyaml
apiVersion: v1
data:
  registries.conf: |
    registries:
    - name: AlibabaCloud Container Registry
      api_url: https://registry-vpc.<RegionID>.aliyuncs.com
      prefix: registry-vpc.<RegionID>.aliyuncs.com
      insecure: no
      credentials: secret:argocd/acr#acr
kind: ConfigMap
metadata:
  labels:
    app.kubernetes.io/name: argocd-image-updater-config
    app.kubernetes.io/part-of: argocd-image-updater
  name: argocd-image-updater-config
  namespace: argocd

参数

说明

name

容器镜像仓库配置的名称。

api_url

容器镜像仓库API地址,<RegionID>会根据当前地域动态渲染自动生成。

prefix

容器镜像仓库的查询前缀,该前缀中<RegionID>会根据当前地域动态渲染自动生成。

credentials

容器镜像仓库的查询前缀,该前缀在安装GitOps时根据当前地域动态渲染自动生成。

如果您使用的是ACREE镜像仓库,请替换对应的api_url和prefix字段的值。

根据 credentials: secret:argocd/acr#acr 配置,我们需要再argocd命名空间下创建名为acr的secret保存访问ACR镜像仓库的凭证:

kubectl -n argocd apply -f - <<EOF
apiVersion: v1
kind: Secret
metadata:
  name: acr
type: Opaque
stringData:
  # 将<your_username>:<your_password>更换为您自己的容器镜像仓库访问凭证。
  acr: <your_username>:<your_password>  
EOF

配置访问Git仓库的凭证

若您在GitOps系统中添加Git Repository时,配置了用户名和密码,则使用该Git Repository的应用默认拥有回写应用容器镜像变更信息到Git系统的权限,可以通过添加以下Annotations配置使用该Git凭证:

annotations:
  argocd-image-updater.argoproj.io/write-back-method: git

若您需要自定义配置该Git凭证,则可以通过添加以下Annotations配置使用指定Git凭证:

annotations:
  argocd-image-updater.argoproj.io/write-back-method: git:secret:argocd/git-creds

并在argocd命名空间下创建名为git-creds的secret:

$ kubectl -n argocd create secret generic git-creds \
--from-literal=username=<your_username> \
--from-literal=password=<your_password>


更多image-updater的用法请参考: https://argocd-image-updater.readthedocs.io/en/stable/

相关实践学习
通过容器镜像仓库与容器服务快速部署spring-hello应用
本教程主要讲述如何将本地Java代码程序上传并在云端以容器化的构建、传输和运行。
Kubernetes极速入门
Kubernetes(K8S)是Google在2014年发布的一个开源项目,用于自动化容器化应用程序的部署、扩展和管理。Kubernetes通常结合docker容器工作,并且整合多个运行着docker容器的主机集群。 本课程从Kubernetes的简介、功能、架构,集群的概念、工具及部署等各个方面进行了详细的讲解及展示,通过对本课程的学习,可以对Kubernetes有一个较为全面的认识,并初步掌握Kubernetes相关的安装部署及使用技巧。本课程由黑马程序员提供。 &nbsp; 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情:&nbsp;https://www.aliyun.com/product/kubernetes
目录
相关文章
|
7天前
|
人工智能 分布式计算 调度
打破资源边界、告别资源浪费:ACK One 多集群Spark和AI作业调度
ACK One多集群Spark作业调度,可以帮助您在不影响集群中正在运行的在线业务的前提下,打破资源边界,根据各集群实际剩余资源来进行调度,最大化您多集群中闲置资源的利用率。
|
20天前
|
Cloud Native Serverless 数据中心
阿里云ACK One:注册集群支持ACS算力——云原生时代的计算新引擎
阿里云ACK One:注册集群支持ACS算力——云原生时代的计算新引擎
25 10
|
20天前
|
存储 Kubernetes 对象存储
部署DeepSeek但GPU不足,ACK One注册集群助力解决IDC GPU资源不足
部署DeepSeek但GPU不足,ACK One注册集群助力解决IDC GPU资源不足
|
20天前
|
Kubernetes 持续交付 开发工具
阿里云协同万兴科技落地ACK One GitOps方案,全球多机房应用自动化发布,效率提升50%
阿里云协同万兴科技落地ACK One GitOps方案,全球多机房应用自动化发布,效率提升50%
|
1月前
|
Kubernetes 持续交付 开发工具
阿里云协同万兴科技落地ACK One GitOps方案,全球多机房应用自动化发布,效率提升50%
阿里云协同万兴科技落地ACK One GitOps方案,全球多机房应用自动化发布,效率提升50%
|
20天前
|
存储 Kubernetes 对象存储
部署 DeepSeek 但 GPU 不足,ACK One 注册集群助力解决 IDC GPU 资源不足
部署 DeepSeek 但 GPU 不足,ACK One 注册集群助力解决 IDC GPU 资源不足
|
22天前
|
运维 分布式计算 Kubernetes
ACK One多集群Service帮助大批量应用跨集群无缝迁移
ACK One多集群Service可以帮助您,在无需关注服务间的依赖,和最小化迁移风险的前提下,完成跨集群无缝迁移大批量应用。
|
18天前
|
Prometheus Kubernetes 监控
OpenAI故障复盘丨如何保障大规模K8s集群稳定性
OpenAI故障复盘丨如何保障大规模K8s集群稳定性
|
2月前
|
缓存 容灾 网络协议
ACK One多集群网关:实现高效容灾方案
ACK One多集群网关可以帮助您快速构建同城跨AZ多活容灾系统、混合云同城跨AZ多活容灾系统,以及异地容灾系统。
|
3月前
|
Kubernetes Ubuntu 网络安全
ubuntu使用kubeadm搭建k8s集群
通过以上步骤,您可以在 Ubuntu 系统上使用 kubeadm 成功搭建一个 Kubernetes 集群。本文详细介绍了从环境准备、安装 Kubernetes 组件、初始化集群到管理和使用集群的完整过程,希望对您有所帮助。在实际应用中,您可以根据具体需求调整配置,进一步优化集群性能和安全性。
193 12

相关产品

  • 容器镜像服务
  • 容器服务Kubernetes版