

 **協助改進此頁面** 

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

若要為本使用者指南貢獻內容，請點選每個頁面右側面板中的**在 GitHub 上編輯此頁面**連結。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

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

ApplicationSets 會從 範本產生多個應用程式，讓您能夠使用單一資源定義，在多個叢集、環境或命名空間之間部署相同的應用程式。

## 先決條件
<a name="_prerequisites"></a>
+ 建立 Argo CD 功能的 EKS 叢集
+ 已設定儲存庫存取 （請參閱 [設定儲存庫存取](argocd-configure-repositories.md))
+  `kubectl` 設定為與您的叢集通訊

**注意**  
ApplicationSets 不需要多個目標叢集。您可以使用叢集產生器以外的產生器 （例如清單、git 或矩陣產生器），在沒有遠端叢集的情況下部署應用程式。

## 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 叢集 ARNs。

這會建立三個應用程式：`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 產生器](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 文件中的[矩陣產生器](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 規格`true`中將 `.syncPolicy.preserveResourcesOnDeletion`設定為 。如需詳細資訊，請參閱 Argo CD 文件中的[應用程式刪除和資源刪除](https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/Application-Deletion/)。

**重要**  
Argo CD 的 ApplicationSets 功能具有您在使用 ApplicationSets 之前應注意的安全考量。如需詳細資訊，請參閱 Argo CD 文件中的 [ApplicationSet Security](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 文件](https://argo-cd.readthedocs.io/en/stable/user-guide/application-set/) - 完整的產生器參考和模式
+  [產生器參考](https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/Generators/) - 詳細的產生器規格