

 **協助改進此頁面** 

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

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

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

# 建立應用程式
<a name="argocd-create-application"></a>

應用程式代表目標叢集中的部署。每個應用程式都會定義來源 (Git 儲存庫） 和目的地 （叢集和命名空間）。套用時，Argo CD 會將 Git 儲存庫中資訊清單指定的資源建立到叢集中的命名空間。應用程式通常會指定工作負載部署，但可以管理目的地叢集中可用的任何 Kubernetes 資源。

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

## 建立基本應用程式
<a name="_create_a_basic_application"></a>

定義從 Git 儲存庫部署的應用程式：

```
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: guestbook
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/argoproj/argocd-example-apps
    targetRevision: HEAD
    path: guestbook
  destination:
    name: in-cluster
    namespace: default
```

**注意**  
`destination.name` 使用 與您在註冊叢集時使用的叢集名稱 （例如`in-cluster`本機叢集的 )。`destination.server` 欄位也適用於 EKS 叢集 ARNs，但建議使用叢集名稱以提高可讀性。

套用應用程式：

```
kubectl apply -f application.yaml
```

檢視應用程式狀態：

```
kubectl get application guestbook -n argocd
```

## 來源組態
<a name="_source_configuration"></a>

 **Git 儲存庫**：

```
spec:
  source:
    repoURL: https://github.com/example/my-app
    targetRevision: main
    path: kubernetes/manifests
```

 **特定 Git 標籤或遞交**：

```
spec:
  source:
    targetRevision: v1.2.0  # or commit SHA
```

 **Helm Chart**：

```
spec:
  source:
    repoURL: https://github.com/example/helm-charts
    targetRevision: main
    path: charts/my-app
    helm:
      valueFiles:
      - values.yaml
      parameters:
      - name: image.tag
        value: v1.2.0
```

 **Helm Chart 具有來自外部 Git 儲存庫的值** （多來源模式）：

```
spec:
  sources:
  - repoURL: https://github.com/example/helm-charts
    targetRevision: main
    path: charts/my-app
    helm:
      valueFiles:
      - $values/environments/production/values.yaml
  - repoURL: https://github.com/example/config-repo
    targetRevision: main
    ref: values
```

如需詳細資訊，請參閱 Argo CD 文件中的[來自外部 Git 儲存庫的 Helm 值檔案](https://argo-cd.readthedocs.io/en/stable/user-guide/multiple_sources/#helm-value-files-from-external-git-repository)。

 **ECR 的 Helm Chart**：

```
spec:
  source:
    repoURL: oci://account-id.dkr.ecr.region.amazonaws.com/repository-name
    targetRevision: chart-version
    chart: chart-name
```

如果功能角色具有必要的 ECR 許可，則直接使用儲存庫，不需要儲存庫組態。如需詳細資訊，請參閱 [設定儲存庫存取](argocd-configure-repositories.md)。

 **CodeCommit 的 Git 儲存庫**：

```
spec:
  source:
    repoURL: https://git-codecommit.region.amazonaws.com/v1/repos/repository-name
    targetRevision: main
    path: kubernetes/manifests
```

如果功能角色具有必要的 CodeCommit 許可，則直接使用儲存庫，不需要儲存庫組態。如需詳細資訊，請參閱 [設定儲存庫存取](argocd-configure-repositories.md)。

 **CodeConnections 中的 Git 儲存庫**：

```
spec:
  source:
    repoURL: https://codeconnections.region.amazonaws.com/git-http/account-id/region/connection-id/owner/repository.git
    targetRevision: main
    path: kubernetes/manifests
```

儲存庫 URL 格式衍生自 CodeConnections 連線 ARN。如果功能角色具有必要的 CodeConnections 許可，且已設定連線，則會直接使用儲存庫，而且不需要儲存庫組態。如需詳細資訊，請參閱 [設定儲存庫存取](argocd-configure-repositories.md)。

 **Kustomize**：

```
spec:
  source:
    repoURL: https://github.com/example/kustomize-app
    targetRevision: main
    path: overlays/production
    kustomize:
      namePrefix: prod-
```

## 同步政策
<a name="_sync_policies"></a>

控制 Argo CD 同步應用程式的方式。

 **手動同步 （預設）**：

應用程式需要手動核准才能同步：

```
spec:
  syncPolicy: {}  # No automated sync
```

手動觸發同步：

```
kubectl patch application guestbook -n argocd \
  --type merge \
  --patch '{"operation": {"initiatedBy": {"username": "admin"}, "sync": {}}}'
```

 **自動同步**：

偵測到 Git 變更時，應用程式會自動同步：

```
spec:
  syncPolicy:
    automated: {}
```

 **自我修復**：

自動將手動變更還原至叢集：

```
spec:
  syncPolicy:
    automated:
      selfHeal: true
```

啟用後，Argo CD 會還原直接對叢集所做的任何手動變更，確保 Git 仍然是事實來源。

 **剔除**：

自動刪除從 Git 移除的資源：

```
spec:
  syncPolicy:
    automated:
      prune: true
```

**警告**  
刪除 會從叢集刪除資源。在生產環境中謹慎使用 。

 **合併自動同步**：

```
spec:
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
    - CreateNamespace=true
```

 **重試組態**：

設定失敗同步的重試行為：

```
spec:
  syncPolicy:
    retry:
      limit: 5  # Number of failed sync attempts; unlimited if less than 0
      backoff:
        duration: 5s  # Amount to back off (default unit: seconds, also supports "2m", "1h")
        factor: 2  # Factor to multiply the base duration after each failed retry
        maxDuration: 3m  # Maximum amount of time allowed for the backoff strategy
```

這對於依賴於 CRDs的資源特別有用，而 CRD 可能無法立即使用。

## 同步選項
<a name="_sync_options"></a>

其他同步組態：

 **如果命名空間不存在，請建立命名空間**：

```
spec:
  syncPolicy:
    syncOptions:
    - CreateNamespace=true
```

 **針對遺失的資源略過試轉**：

套用依賴尚不存在CRDs 的資源時很有用 （例如 kro 執行個體）：

```
spec:
  syncPolicy:
    syncOptions:
    - SkipDryRunOnMissingResource=true
```

這也可以使用資源本身上的標籤套用至特定資源。

 **在套用之前驗證資源**：

```
spec:
  syncPolicy:
    syncOptions:
    - Validate=true
```

 **僅套用不同步**：

```
spec:
  syncPolicy:
    syncOptions:
    - ApplyOutOfSyncOnly=true
```

## 進階同步功能
<a name="_advanced_sync_features"></a>

Argo CD 支援複雜部署的進階同步功能：
+  **同步波** – 使用`argocd.argoproj.io/sync-wave`註釋控制資源建立順序
+  **同步掛鉤** - 使用`argocd.argoproj.io/hook`註釋同步之前或之後執行任務 (PreSync、PostSync、SyncFail)
+  **資源運作狀態評估** - 自訂應用程式特定資源的運作狀態檢查

如需詳細資訊，請參閱 Argo CD 文件中的[同步波](https://argo-cd.readthedocs.io/en/stable/user-guide/sync-waves/)和資源[勾點](https://argo-cd.readthedocs.io/en/stable/user-guide/resource_hooks/)。

## 忽略差異
<a name="_ignore_differences"></a>

防止 Argo CD 同步由其他控制器管理的特定欄位 （例如 HPA 管理複本）：

```
spec:
  ignoreDifferences:
  - group: apps
    kind: Deployment
    jsonPointers:
    - /spec/replicas
```

如需忽略模式和欄位排除的詳細資訊，請參閱 Argo CD 文件中的[差異自訂](https://argo-cd.readthedocs.io/en/stable/user-guide/diffing/)。

## 多環境部署
<a name="_multi_environment_deployment"></a>

將相同的應用程式部署到多個環境：

 **開發**：

```
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: my-app-dev
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/example/my-app
    targetRevision: develop
    path: overlays/development
  destination:
    name: dev-cluster
    namespace: my-app
```

 **生產**：

```
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: my-app-prod
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/example/my-app
    targetRevision: main
    path: overlays/production
  destination:
    name: prod-cluster
    namespace: my-app
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
```

## 監控和管理應用程式
<a name="_monitor_and_manage_applications"></a>

 **檢視應用程式狀態**：

```
kubectl get application my-app -n argocd
```

 **存取 Argo CD UI**：

透過 EKS 主控台開啟 Argo CD UI，以檢視應用程式拓撲、同步狀態、資源運作狀態和部署歷史記錄。如需 UI 存取指示[使用 Argo CD](working-with-argocd.md)，請參閱 。

 **轉返應用程式**：

使用 Argo CD UI、Argo CD CLI 或將應用程式規格`targetRevision`中的 更新為先前的 Git 遞交或標籤，轉返至先前的修訂。

使用 Argo CD CLI：

```
argocd app rollback argocd/my-app <revision-id>
```

**注意**  
搭配 受管功能使用 Argo CD CLI 時，請指定命名空間字首為 的應用程式`namespace/appname`。

如需詳細資訊，請參閱 Argo CD 文件中的 [argocd 應用程式轉返](https://argo-cd.readthedocs.io/en/stable/user-guide/commands/argocd_app_rollback/)。

## 其他資源
<a name="_additional_resources"></a>
+  [使用 Argo CD 專案](argocd-projects.md) - 使用適用於多租戶環境的專案組織應用程式
+  [使用 ApplicationSets](argocd-applicationsets.md) - 使用 範本部署到多個叢集
+  [應用程式規格](https://argo-cd.readthedocs.io/en/stable/user-guide/application-specification/) - 完整應用程式 API 參考
+  [同步選項](https://argo-cd.readthedocs.io/en/stable/user-guide/sync-options/) - 進階同步組態