

 **帮助改进此页面** 

要帮助改进本用户指南，请选择位于每个页面右侧窗格中的**在 GitHub 上编辑此页面**链接。

# 使用 ApplicationSets
<a name="argocd-applicationsets"></a>

ApplicationSet 通过模板生成多个应用程序，使您能够通过单个资源定义，跨多个集群、环境或命名空间部署相同的应用程序。

## 先决条件
<a name="_prerequisites"></a>
+ 已创建具有 Argo CD 功能的 EKS 集群
+ 已配置存储库访问权限（请参阅[配置存储库访问权限](argocd-configure-repositories.md)）
+  已配置 `kubectl` 以与集群通信

**注意**  
ApplicationSets 不需要多个目标集群。您可以使用集群生成器以外的生成器（如 list、git 或 matrix 生成器），在没有远程集群的情况下部署应用程序。

## ApplicationSets 的工作原理
<a name="_how_applicationsets_work"></a>

ApplicationSets 使用生成器来生成参数，然后将这些参数应用于应用程序模板。每组生成的参数都会创建一个应用程序。

适用于 EKS 部署的常用生成器：
+  **列表生成器**：为每个环境明确定义集群和参数
+  **集群生成器**：自动部署到所有已注册的集群
+  **Git 生成器**：基于存储库结构生成应用程序
+  **矩阵生成器**：组合多个生成器以实现多维部署
+  **合并生成器**：合并来自多个生成器的参数

有关完整的生成器参考，请参阅 [ApplicationSet 文档](https://argo-cd.readthedocs.io/en/stable/user-guide/application-set/)。

## 列表生成器
<a name="_list_generator"></a>

通过明确配置部署到多个集群：

```
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: guestbook-all-clusters
  namespace: argocd
spec:
  generators:
  - list:
      elements:
      - environment: dev
        replicas: "2"
      - environment: staging
        replicas: "3"
      - environment: prod
        replicas: "5"
  template:
    metadata:
      name: 'guestbook-{{environment}}'
    spec:
      project: default
      source:
        repoURL: https://github.com/example/guestbook
        targetRevision: HEAD
        path: 'overlays/{{environment}}'
      destination:
        name: '{{environment}}-cluster'
        namespace: guestbook
      syncPolicy:
        automated:
          prune: true
          selfHeal: true
```

**注意**  
将 `destination.name` 与集群名称一起使用以提高可读性。如果需要，`destination.server` 字段还可以与 EKS 集群 ARN 配合使用。

这将创建三个应用程序：`guestbook-dev`、`guestbook-staging` 和 `guestbook-prod`。

## 集群生成器
<a name="_cluster_generator"></a>

自动部署到所有已注册的集群：

```
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: cluster-addons
  namespace: argocd
spec:
  generators:
  - clusters: {}
  template:
    metadata:
      name: '{{name}}-addons'
    spec:
      project: default
      source:
        repoURL: https://github.com/example/cluster-addons
        targetRevision: HEAD
        path: addons
      destination:
        server: '{{server}}'
        namespace: kube-system
      syncPolicy:
        automated:
          prune: true
          selfHeal: true
```

这将自动为每个已注册的集群创建应用程序。

 **筛选集群**：

使用 `matchLabels` 包含特定集群，或使用 `matchExpressions` 排除集群：

```
spec:
  generators:
  - clusters:
      selector:
        matchLabels:
          environment: production
        matchExpressions:
        - key: skip-appset
          operator: DoesNotExist
```

## Git 生成器
<a name="_git_generators"></a>

Git 生成器基于存储库结构创建应用程序：
+  **目录生成器**：将每个目录作为单独的应用程序部署（适用于微服务）
+  **文件生成器**：根据参数文件生成应用程序（适用于多租户部署）

 **示例：微服务部署** 

```
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: microservices
  namespace: argocd
spec:
  generators:
  - git:
      repoURL: https://github.com/example/microservices
      revision: HEAD
      directories:
      - path: services/*
  template:
    metadata:
      name: '{{path.basename}}'
    spec:
      project: default
      source:
        repoURL: https://github.com/example/microservices
        targetRevision: HEAD
        path: '{{path}}'
      destination:
        name: my-cluster
        namespace: '{{path.basename}}'
      syncPolicy:
        automated:
          prune: true
          selfHeal: true
        syncOptions:
        - CreateNamespace=true
```

有关 Git 生成器和基于文件的配置的详细信息，请参阅 Argo CD 文档中的 [Git Generator](https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/Generators-Git/)。

## 矩阵生成器
<a name="_matrix_generator"></a>

组合多个生成器，以实现跨多个维度（环境 × 集群）的部署：

```
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: multi-env-multi-cluster
  namespace: argocd
spec:
  generators:
  - matrix:
      generators:
      - list:
          elements:
          - environment: dev
          - environment: staging
          - environment: prod
      - clusters:
          selector:
            matchLabels:
              region: us-west-2
  template:
    metadata:
      name: 'app-{{environment}}-{{name}}'
    spec:
      project: default
      source:
        repoURL: https://github.com/example/app
        targetRevision: HEAD
        path: 'overlays/{{environment}}'
      destination:
        name: '{{name}}'
        namespace: 'app-{{environment}}'
```

有关组合生成器的详细信息，请参阅 Argo CD 文档中的 [Matrix Generator](https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/Generators-Matrix/)。

## 多区域部署
<a name="_multi_region_deployment"></a>

跨多个区域部署到集群：

```
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: global-app
  namespace: argocd
spec:
  generators:
  - list:
      elements:
      - clusterName: prod-us-west
        region: us-west-2
      - clusterName: prod-us-east
        region: us-east-1
      - clusterName: prod-eu-west
        region: eu-west-1
  template:
    metadata:
      name: 'app-{{region}}'
    spec:
      project: default
      source:
        repoURL: https://github.com/example/app
        targetRevision: HEAD
        path: kubernetes
        helm:
          parameters:
          - name: region
            value: '{{region}}'
      destination:
        name: '{{clusterName}}'
        namespace: app
      syncPolicy:
        automated:
          prune: true
          selfHeal: true
```

## 管理 ApplicationSets
<a name="_manage_applicationsets"></a>

 **查看 ApplicationSets 及生成的应用程序**：

```
kubectl get applicationsets -n argocd
kubectl get applications -n argocd -l argocd.argoproj.io/application-set-name=<applicationset-name>
```

 **更新 ApplicationSet**：

修改 ApplicationSet 规范并重新应用。Argo CD 会自动更新所有生成的应用程序：

```
kubectl apply -f applicationset.yaml
```

 **删除 ApplicationSet**：

```
kubectl delete applicationset <name> -n argocd
```

**警告**  
删除 ApplicationSet 会同时删除所有生成的应用程序。如果这些应用程序设置了 `prune: true`，其资源也将从目标集群中删除。  
要在删除 ApplicationSet 时保留已部署的资源，请在 ApplicationSet 规范中将 `.syncPolicy.preserveResourcesOnDeletion` 设置为 `true`。有关更多信息，请参阅 Argo CD 文档中的[应用程序修剪和资源删除](https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/Application-Deletion/)。

**重要**  
Argo CD 的 ApplicationSets 功能存在一些安全方面的考量事项，在使用 ApplicationSets 之前您应当予以了解。有关更多信息，请参阅 Argo CD 文档中的 [ApplicationSet 安全性](https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/Security/)。

## 其他资源
<a name="_additional_resources"></a>
+  [使用 Argo CD 项目](argocd-projects.md)：通过项目整理 ApplicationSets
+  [创建应用程序](argocd-create-application.md)：了解应用程序配置
+  [ApplicationSet Documentation](https://argo-cd.readthedocs.io/en/stable/user-guide/application-set/)：完整的生成器参考和模式
+  [Generator Reference](https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/Generators/)：详细的生成器规范