在已啟用 Azure Arc 的 Kubernetes 叢集上使用 GitOps 部署 Helm 圖表

重要

本文適用於具有 Flux v1 的 GitOps。 已啟用 Azure Arc 的 Kubernetes 和 Azure Kubernetes Service (AKS) 叢集現在可以使用具有 Flux v2 的 GitOps;移至具有 Flux v2 的 GitOps 教學課程。 建議您儘快移轉至 Flux v2

在 2024 年 1 月 1 日之前建立的 Flux v1 型叢集設定資源支援,將於 2025 年 5 月 24 日結束。 從 2024 年 1 月 1 日起,您將無法建立新的 Flux v1 型叢集設定資源。 Helm 是開放原始碼的封裝工具,可協助您安裝和管理 Kubernetes 應用程式的生命週期。 Helm 類似於 APT 和 Yum 等 Linux 套件管理員,可用於管理 Kubernetes 圖表,即預先設定的 Kubernetes 資源套件。

本文示範如何透過已啟用 Azure Arc 的 Kubernetes 來設定及使用 Helm。

必要條件

已啟用 Azure Arc 的 Kubernetes 搭配使用 GitOps 與 Helm 總覽

Helm 運算子提供 Flux 的延伸模組,可將 Helm 圖表版本自動化。 Helm 圖表版本是透過名為 HelmRelease 的 Kubernetes 自訂資源進行描述。 Flux 會將這些資源從 Git 同步處理到叢集,而 Helm 運算子可確保如資源中所指定來釋放 Helm 圖表。

本文中使用的範例存放庫的結構如下:

├── charts
│   └── azure-arc-sample
│       ├── Chart.yaml
│       ├── templates
│       │   ├── NOTES.txt
│       │   ├── deployment.yaml
│       │   └── service.yaml
│       └── values.yaml
└── releases
    └── app.yaml

在 Git 存放庫中,我們有兩個目錄,一個包含 Helm 圖表,一個包含版本設定。在 releases 目錄中,app.yaml 包含如下所示的 HelmRelease 組態:

apiVersion: helm.fluxcd.io/v1
kind: HelmRelease
metadata:
  name: azure-arc-sample
  namespace: arc-k8s-demo
spec:
  releaseName: arc-k8s-demo
  chart:
    git: https://github.com/Azure/arc-helm-demo
    ref: master
    path: charts/azure-arc-sample
  values:
    serviceName: arc-k8s-demo

Helm 版本設定包含下列欄位:

欄位 描述
metadata.name 必要欄位。 必須遵循 Kubernetes 命名慣例。
metadata.namespace 選擇性欄位。 決定建立版本的位置。
spec.releaseName 選擇性欄位。 如果未提供,版本名稱將會是 $namespace-$name
spec.chart.path 包含圖表的目錄 (相對於儲存機制根路徑)。
spec.values 來自圖表本身的使用者自訂預設參數值。

在 HelmRelease spec.values 中指定的選項將會覆寫圖表來源的 values.yaml 中所指定選項。

您可以在官方 Helm Operator 文件中深入了解 HelmRelease。

建立設定

使用適用於 k8s-configuration 的 Azure CLI 延伸模組,即可將已連線的叢集連結至範例 Git 存放庫。 將此設定命名為 azure-arc-sample,並在 arc-k8s-demo 命名空間中部署 Flux 運算子。

az k8s-configuration create --name azure-arc-sample --cluster-name AzureArcTest1 --resource-group AzureArcTest --operator-instance-name flux --operator-namespace arc-k8s-demo --operator-params='--git-readonly --git-path=releases' --enable-helm-operator --helm-operator-chart-version='1.2.0' --helm-operator-params='--set helm.versions=v3' --repository-url https://github.com/Azure/arc-helm-demo.git --scope namespace --cluster-type connectedClusters

設定參數

若要自訂設定的建立,請深入了解其他參數

驗證設定

使用 Azure CLI 驗證是否已成功建立設定。

az k8s-configuration show --name azure-arc-sample --cluster-name AzureArcTest1 --resource-group AzureArcTest --cluster-type connectedClusters

設定資源將使用合規性狀態、訊息和偵錯資訊進行更新。

{
  "complianceStatus": {
    "complianceState": "Installed",
    "lastConfigApplied": "2019-12-05T05:34:41.481000",
    "message": "{\"OperatorMessage\":null,\"ClusterState\":null}",
    "messageLevel": "3"
  },
  "enableHelmOperator": "True",
  "helmOperatorProperties": {
    "chartValues": "--set helm.versions=v3",
    "chartVersion": "1.2.0"
  },
  "id": "/subscriptions/57ac26cf-a9f0-4908-b300-9a4e9a0fb205/resourceGroups/AzureArcTest/providers/Microsoft.Kubernetes/connectedClusters/AzureArcTest1/providers/Microsoft.KubernetesConfiguration/sourceControlConfigurations/azure-arc-sample",
  "name": "azure-arc-sample",
  "operatorInstanceName": "flux",
  "operatorNamespace": "arc-k8s-demo",
  "operatorParams": "--git-readonly --git-path=releases",
  "operatorScope": "namespace",
  "operatorType": "Flux",
  "provisioningState": "Succeeded",
  "repositoryPublicKey": "",
  "repositoryUrl": "https://github.com/Azure/arc-helm-demo.git",
  "resourceGroup": "AzureArcTest",
  "type": "Microsoft.KubernetesConfiguration/sourceControlConfigurations"
}

驗證應用程式

執行下列命令,並瀏覽至瀏覽器上的 localhost:8080,以確認應用程式正在執行。

kubectl port-forward -n arc-k8s-demo svc/arc-k8s-demo 8080:8080

下一步

使用 Azure 原則大規模套用叢集設定。