教學課程:在已啟用 Azure Arc 的 Kubernetes 叢集上使用 GitOps 部署設定

重要

本教學課程適用於具有 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 型叢集設定資源。

在本教學課程中,您將在已啟用 Azure Arc 的 Kubernetes 叢集上使用 GitOps 部署設定。 您將了解如何:

  • 使用 Git 存放庫範例,在已啟用 Azure Arc 的 Kubernetes 叢集上建立設定。
  • 驗證已成功建立設定。
  • 套用來自私人 Git 存放庫的設定。
  • 驗證 Kubernetes 設定。

必要條件

建立設定

本文中使用的範例存放庫是以叢集操作員的角色為結構。 此存放庫中的資訊清單會佈建幾個命名空間、部署工作負載,並提供一些小組特定的設定。 使用此存放庫搭配 GitOps,會在您的叢集上建立下列資源:

  • 命名空間:cluster-configteam-ateam-b
  • 部署:arc-k8s-demo
  • ConfigMap:team-a/endpoints

config-agent 會輪詢 Azure 以取得新的或更新的設定。 此工作最多需要 5 分鐘的時間。

如果您要將私人存放庫與設定建立關聯,請確保您也會完成下列從私人 Git 存放庫套用設定中的步驟。

使用 Azure CLI

使用適用於 k8s-configuration 的 Azure CLI 延伸模組,將已連線的叢集連結至範例 Git 存放庫

  1. 將此設定命名為 cluster-config

  2. 指示代理程式在 cluster-config 命名空間中部署運算子。

  3. 授與操作員 cluster-admin 權限。

    az k8s-configuration create --name cluster-config --cluster-name AzureArcTest1 --resource-group AzureArcTest --operator-instance-name cluster-config --operator-namespace cluster-config --repository-url https://github.com/Azure/arc-k8s-demo --scope cluster --cluster-type connectedClusters
    
    {
      "complianceStatus": {
      "complianceState": "Pending",
      "lastConfigApplied": "0001-01-01T00:00:00",
      "message": "{\"OperatorMessage\":null,\"ClusterState\":null}",
      "messageLevel": "3"
      },
      "configurationProtectedSettings": {},
      "enableHelmOperator": false,
      "helmOperatorProperties": null,
      "id": "/subscriptions/<sub id>/resourceGroups/<group name>/providers/Microsoft.Kubernetes/connectedClusters/<cluster name>/providers/Microsoft.KubernetesConfiguration/sourceControlConfigurations/cluster-config",
      "name": "cluster-config",
      "operatorInstanceName": "cluster-config",
      "operatorNamespace": "cluster-config",
      "operatorParams": "--git-readonly",
      "operatorScope": "cluster",
      "operatorType": "Flux",
      "provisioningState": "Succeeded",
      "repositoryPublicKey": "",
      "repositoryUrl": "https://github.com/Azure/arc-k8s-demo",
      "resourceGroup": "MyRG",
      "sshKnownHostsContents": "",
      "systemData": {
        "createdAt": "2020-11-24T21:22:01.542801+00:00",
        "createdBy": null,
        "createdByType": null,
        "lastModifiedAt": "2020-11-24T21:22:01.542801+00:00",
        "lastModifiedBy": null,
        "lastModifiedByType": null
      },
      "type": "Microsoft.KubernetesConfiguration/sourceControlConfigurations"
    }
    

使用公用 Git 存放庫

參數 格式
--repository-url http[s]://server/repo[.git]

使用帶有 SSH 和 Flux 已建立金鑰的私人 Git 存放庫

將 Flux 產生的公開金鑰新增至 Git 服務提供者的使用者帳戶中。 如果將金鑰新增至存放庫而不是使用者帳戶,請使用 user@ 代替 URL 中的 git@

如需詳細資訊,請跳至從私人 Git 存放庫套用設定區段。

參數 格式 備註
--repository-url ssh://user@server/repo[.git] 或 user@server:repo[.git] git@ 可取代 user@

使用帶有 SSH 和使用者已提供金鑰的私人 Git 存放庫

直接或在檔案中提供您自己的私密金鑰。 金鑰必須為 PEM 格式,並以分行符號 (\n) 結尾。

將關聯的公開金鑰新增至 Git 服務提供者中的使用者帳戶。 如果將金鑰新增至存放庫而不是使用者帳戶,請使用 git@ 代替 user@

如需詳細資訊,請跳至從私人 Git 存放庫套用設定區段。

參數 格式 備註
--repository-url ssh://user@server/repo[.git] 或 user@server:repo[.git] git@ 可取代 user@
--ssh-private-key PEM 格式編碼的 base64 編碼金鑰 直接提供金鑰
--ssh-private-key-file 本機檔案的完整路徑 提供包含 PEM 格式金鑰的本機檔案完整路徑

使用具有 SSH 和使用者已提供已知主機的私人 Git 主機

Flux 操作員會在其已知主機檔案中維護常見 Git 主機清單,以便在建立 SSH 連線之前對 Git 存放庫進行驗證。 如果您使用的是不常見的 Git 存放庫或您自己的 Git 主機,則可以提供主機金鑰,以便 Flux 識別您的存放庫。

就像私密金鑰一樣,您可以直接或在檔案中提供您的 known_hosts 內容。 提供您自己的內容時,請使用 known_hosts 內容格式規格以及上述任一 SSH 金鑰案例。

參數 格式 備註
--repository-url ssh://user@server/repo[.git] 或 user@server:repo[.git] git@ 可取代 user@
--ssh-known-hosts Base64 編碼 直接提供已知的主機內容
--ssh-known-hosts-file 本機檔案的完整路徑 在本機檔案中提供已知的主機內容

搭配 HTTPS 使用私人 Git 存放庫

參數 格式 備註
--repository-url https://server/repo[.git] 具有基本身份驗證的 HTTPS
--https-user RAW 或 Base64 編碼字串 HTTPS 使用者名稱
--https-key RAW 或 Base64 編碼字串 HTTPS 個人存取權杖或密碼

注意

  • Helm 運算子圖表 1.2.0+ 版支援 HTTPS Helm 發行私人驗證。
  • AKS 受控叢集不支援 HTTPS Helm 版本。
  • 如果您需要 Flux 透過 Proxy 存取 Git 存放庫,您必須使用 Proxy 設定來更新 Azure Arc 代理程式。 如需詳細資訊,請參閱使用輸出 Proxy 伺服器連線

其他參數

使用下列選擇性參數自訂設定:

參數 描述
--enable-helm-operator 切換以啟用 Helm 圖表部署的支援。
--helm-operator-params 適用於 Helm 運算子 (如果已啟用) 的圖表值。 例如: --set helm.versions=v3
--helm-operator-chart-version 適用於 Helm 運算子 (如果已啟用) 的圖表版本。 使用 1.2.0+ 版。 預設值:'1.2.0'。
--operator-namespace 運算子命名空間的名稱。 預設值:'default'。 最大值:23 個字元。
--operator-params 運算子的參數。 必須在單引號中提供。 例如,--operator-params='--git-readonly --sync-garbage-collection --git-branch=main'

--operator-params 中支援的選項:

選項 描述
--git-branch 要用於 Kubernetes 資訊清單的 Git 存放庫分支。 預設值是 'master'。 較新的存放庫具有名為 main 的根分支,在此情況下,您必須設定 --git-branch=main
--git-path 適用於 Flux 之 Git 存放庫內的相對路徑,可尋找 Kubernetes 資訊清單。
--git-readonly Git 存放庫會被視為唯讀。 Flux 不會嘗試將其寫入。
--manifest-generation 啟用時,Flux 將會尋找 .flux.yaml,並執行 Kustomize 或其他資訊清單產生器。
--git-poll-interval 輪詢 Git 存放庫以取得新認可的期間。 預設值為 5m (5 分鐘)。
--sync-garbage-collection 啟用時,Flux 將刪除其已建立但不再出現於 Git 中的資源。
--git-label 追蹤同步進度的標籤。 用來標記 Git 分支。 預設值為 flux-sync
--git-user 適用於 Git 認可的使用者名稱。
--git-email 適用於 Git 認可的電子郵件。

如果您不想讓 Flux 寫入存放庫,且未設定 --git-user--git-email,則會自動設定 --git-readonly

如需詳細資訊,請參閱 Flux 文件

注意

Flux 預設會從 git 存放庫的 master 分支進行同步處理。 不過,較新的 Git 存放庫具有名為 main 的根分支,在此情況下,您必須在 --operator-params 中設定 --git-branch=main

提示

您可以在 Azure 入口網站的 [已啟用 Azure Arc 的 Kubernetes 資源] 的 [GitOps] 索引標籤中建立設定。

驗證設定

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

az k8s-configuration show --name cluster-config --cluster-name AzureArcTest1 --resource-group AzureArcTest --cluster-type connectedClusters

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

{
  "complianceStatus": {
    "complianceState": "Installed",
    "lastConfigApplied": "2020-12-10T18:26:52.801000+00:00",
    "message": "...",
    "messageLevel": "Information"
  },
  "configurationProtectedSettings": {},
  "enableHelmOperator": false,
  "helmOperatorProperties": {
    "chartValues": "",
    "chartVersion": ""
  },
  "id": "/subscriptions/<sub id>/resourceGroups/AzureArcTest/providers/Microsoft.Kubernetes/connectedClusters/AzureArcTest1/providers/Microsoft.KubernetesConfiguration/sourceControlConfigurations/cluster-config",
  "name": "cluster-config",
  "operatorInstanceName": "cluster-config",
  "operatorNamespace": "cluster-config",
  "operatorParams": "--git-readonly",
  "operatorScope": "cluster",
  "operatorType": "Flux",
  "provisioningState": "Succeeded",
  "repositoryPublicKey": "...",
  "repositoryUrl": "git://github.com/Azure/arc-k8s-demo.git",
  "resourceGroup": "AzureArcTest",
  "sshKnownHostsContents": null,
  "systemData": {
    "createdAt": "2020-12-01T03:58:56.175674+00:00",
    "createdBy": null,
    "createdByType": null,
    "lastModifiedAt": "2020-12-10T18:30:56.881219+00:00",
    "lastModifiedBy": null,
    "lastModifiedByType": null
},
  "type": "Microsoft.KubernetesConfiguration/sourceControlConfigurations"
}

建立或更新設定時,會發生一些情況:

  1. Azure Arc config-agent 會監視 Azure Resource Manager 的新設定或更新設定 (Microsoft.KubernetesConfiguration/sourceControlConfigurations),並注意到新的 Pending 設定。
  2. config-agent 會讀取設定屬性,並建立目的地命名空間。
  3. Azure Arc controller-manager 會建立 Kubernetes 服務帳戶,並將其對應至 ClusterRoleBinding 或 RoleBinding 以取得適當的權限 (clusternamespace 範圍)。 然後,其會部署 flux 的執行個體。
  4. 如果使用 SSH 選項搭配 Flux 產生的金鑰,則 flux 會產生 SSH 金鑰並記錄公開金鑰。
  5. config-agent 會向 Azure 中的設定資源回報狀態。

當佈建程序發生時,設定資源將歷經數個狀態變更。 使用上述 az k8s-configuration show ... 命令來監視進度:

暫存變更 描述
complianceStatus->Pending 代表初始和進行中狀態。
complianceStatus ->Installed config-agent 已成功設定叢集並部署 flux,而不會發生錯誤。
complianceStatus ->Failed config-agent 部署 flux 時發生錯誤。 會在 complianceStatus.message 回應本文中提供詳細資料。

套用來自私人 Git 存放庫的設定

如果您使用的是私人 Git 存放庫,則需要在存放庫中設定 SSH 公開金鑰。 您可以提供 SSH 公開金鑰,或是 Flux 會產生公開金鑰。 您可以在特定的 Git 存放庫或有權存取存放庫的 Git 使用者上設定公開金鑰。

取得您自己的公開金鑰

如果已產生您自己的 SSH 金鑰,則您已經擁有私人和公開金鑰。

使用 Azure CLI 取得公開金鑰

如果 Flux 正在產生金鑰,請在 Azure CLI 中使用下列內容。

az k8s-configuration show --resource-group <resource group name> --cluster-name <connected cluster name> --name <configuration name> --cluster-type connectedClusters --query 'repositoryPublicKey' 
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAREDACTED"

從 Azure 入口網站取得公開金鑰

如果 Flux 正在產生金鑰,請在 Azure 入口網站中逐步解說下列內容。

  1. 在 Azure 入口網站中,瀏覽至連線的叢集資源。
  2. 在資源頁面中,選取 [GitOps],並查看此叢集的設定清單。
  3. 選取使用私人 Git 存放庫的設定。
  4. 在開啟的內容視窗中,複製視窗底部的存放庫公開金鑰

使用 GitHub 新增公開金鑰

使用下列其中一個選項:

  • 選項 1:將公開金鑰新增至使用者帳戶 (適用於帳戶中的所有存放庫):

    1. 開啟 GitHub 並按一下頁面右上角的設定檔圖示。
    2. 按一下 [設定]
    3. 按一下 [SSH 和 GPG 金鑰]
    4. 按一下 [新增 SSH 金鑰]
    5. 提供標題。
    6. 貼上周圍不含任何引號的公開金鑰。
    7. 按一下 [新增 SSH 金鑰]
  • 選項 2:將公開金鑰作為部署金鑰新增至 Git 存放庫 (僅適用於此存放庫):

    1. 開啟 GitHub 並瀏覽至您的存放庫。
    2. 按一下 [設定]
    3. 按一下 [部署金鑰]
    4. 按一下 [新增部署金鑰]
    5. 提供標題。
    6. 檢查 [允許寫入權限]。
    7. 貼上周圍不含任何引號的公開金鑰。
    8. 按一下 [新增金鑰]

使用 Azure DevOps 存放庫新增公開金鑰

使用下列步驟將金鑰新增至 SSH 金鑰:

  1. 在右上角的 [使用者設定] 底下 (設定檔影像旁邊),按一下 [SSH 公開金鑰]
  2. 選取 [+ 新增金鑰]。
  3. 提供名稱。
  4. 貼上周圍不含任何引號的公開金鑰。
  5. 按一下新增

驗證 Kubernetes 設定

config-agent 安裝 flux 執行個體之後,Git 存放庫中保留的資源應該會開始流向叢集。 檢查以查看是否已使用下列命令建立命名空間、部署和資源:

kubectl get ns --show-labels
NAME              STATUS   AGE    LABELS
azure-arc         Active   24h    <none>
cluster-config    Active   177m   <none>
default           Active   29h    <none>
itops             Active   177m   fluxcd.io/sync-gc-mark=sha256.9oYk8yEsRwWkR09n8eJCRNafckASgghAsUWgXWEQ9es,name=itops
kube-node-lease   Active   29h    <none>
kube-public       Active   29h    <none>
kube-system       Active   29h    <none>
team-a            Active   177m   fluxcd.io/sync-gc-mark=sha256.CS5boSi8kg_vyxfAeu7Das5harSy1i0gc2fodD7YDqA,name=team-a
team-b            Active   177m   fluxcd.io/sync-gc-mark=sha256.vF36thDIFnDDI2VEttBp5jgdxvEuaLmm7yT_cuA2UEw,name=team-b

我們可以看到已建立 team-ateam-bitopscluster-config 命名空間。

flux 運算子已部署至 cluster-config 命名空間,如設定資源所指示:

kubectl -n cluster-config get deploy  -o wide
NAME             READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                         SELECTOR
cluster-config   1/1     1            1           3h    flux         docker.io/fluxcd/flux:1.16.0   instanceName=cluster-config,name=flux
memcached        1/1     1            1           3h    memcached    memcached:1.5.15               name=memcached

進一步探索

您可以使用下列項目瀏覽其他部署為設定存放庫一部分的資源:

kubectl -n team-a get cm -o yaml
kubectl -n itops get all

清除資源

使用 Azure CLI 或 Azure 入口網站來刪除設定。 執行 delete 命令之後,將會立即在 Azure 中刪除設定資源。 應該會在 10 分鐘內從叢集完整刪除相關聯的物件。 如果設定在移除時處於失敗狀態,則完整刪除相關聯的物件最多可能需要一小時的時間。

刪除具有 namespace 範圍的設定時,並不會由 Azure Arc 刪除命名空間,以避免中斷現有的工作負載。 如有需要,您可以使用 kubectl 手動刪除此命名空間。

az k8s-configuration delete --name cluster-config --cluster-name AzureArcTest1 --resource-group AzureArcTest --cluster-type connectedClusters

注意

刪除設定時,不會刪除任何因為追蹤的 Git 存放庫進行部署而產生的叢集變更。

下一步

前進至下一個教學課程,以了解如何使用 GitOps 實作 CI/CD。