AKS 和已啟用 Azure Arc 的 Kubernetes 適用的 GitOps Flux v2 設定
Azure 提供在 Azure Kubernetes Service (AKS) 和已啟用 Azure Arc 的 Kubernetes 叢集中使用 GitOps 來管理組態的功能。 您可以在這些叢集中輕鬆啟用和使用 GitOps。
使用 GitOps 時,請在 Git 存放庫的檔案中宣告 Kubernetes 叢集的所需狀態。 Git 存放庫可能包含下列檔案:
- YAML 格式的資訊清單,用於描述 Kubernetes 資源 (例如命名空間、秘密、部署等)
- Helm 圖表,用於部署應用程式
- Kustomize 檔案,用於描述環境特定的變更
因為這些檔案儲存在 Git 存放庫中,都已設定版本,可以輕鬆追蹤版本之間的變更。 Kubernetes 控制器在叢集中執行,並持續協調叢集狀態與 Git 存放庫中宣告的所需狀態。 這些運算子會從 Git 存放庫提取檔案,並將所需狀態套用至叢集。 運算子也會持續確保叢集維持在所需狀態。
在已啟用 Azure Arc 的 Kubernetes 或 Azure Kubernetes Service 上,GitOps 使用熱門的開放原始碼工具集 Flux。 Flux 支援常見的檔案來源 (Git 和 Helm 存放庫、貯體、Azure Blob 儲存體) 和範本類型 (YAML、Helm 和 Kustomize)。 Flux 也支援 多租使用者 和部署相依性管理, 以及其他功能。
Flux 叢集延伸模組
在已啟用 Azure Arc 的 Kubernetes 或 AKS 叢集中,GitOps 啟用為Microsoft.KubernetesConfiguration/extensions/microsoft.flux
叢集延伸模組資源。 叢集中必須安裝 microsoft.flux
延伸模組,才能建立一或多個 fluxConfigurations
。 在叢集中建立第一個 Microsoft.KubernetesConfiguration/fluxConfigurations
時會自動安裝此延伸模組,或者,您也可以使用入口網站、Azure CLI (az k8s-extension create --extensionType=microsoft.flux)、ARM 範本或 REST API 來手動安裝。
版本支援
支援最新版的 Flux v2 延伸模組和兩個舊版 (N-2)。 通常建議您使用最新版的延伸模組。
注意
最後,Azure 會停止使用 Flux v1 支援 GitOps,因此我們建議您儘快 移轉至 Flux v2 。
控制器
microsoft.flux
延伸模組預設會安裝 Flux 控制器 (來源、Kustomize、Helm、通知) 和 FluxConfig CRD、fluxconfig-agent 和 fluxconfig-controller。 您可以控制安裝哪些控制器,還可選擇安裝 image-automation 和 image-reflector 控制器,以提供更新和擷取 Docker 映像的功能。
Flux 來源控制器:監看 source.toolkit.fluxcd.io 自訂資源。 處理 Git 存放庫、Helm 存放庫、貯體和 Azure Blob 儲存體之間的同步。 處理私人 Git、Helm 存放庫和 Azure Blob 儲存體帳戶的來源授權。 透過 tar 封存檔案呈現來源的最新變更。
Flux Kustomize 控制器:監看
kustomization.toolkit.fluxcd.io
自訂資源。 將 Kustomize 或原始 YAML 檔案從來源套用至叢集。Flux Helm 控制器:監看
helm.toolkit.fluxcd.io
自訂資源。 從來源控制器所呈現的 Helm 存放庫來源擷取相關聯的圖表。 建立HelmChart
自訂資源,並將具有特定版本、名稱和客戶定義值的HelmRelease
套用至叢集。Flux 通知控制器:監看
notification.toolkit.fluxcd.io
自訂資源。 接收來自所有 Flux 控制器的通知。 將通知推送至使用者定義的 Webhook 端點。Flux 自訂資源定義:
kustomizations.kustomize.toolkit.fluxcd.io
imagepolicies.image.toolkit.fluxcd.io
imagerepositories.image.toolkit.fluxcd.io
imageupdateautomations.image.toolkit.fluxcd.io
alerts.notification.toolkit.fluxcd.io
providers.notification.toolkit.fluxcd.io
receivers.notification.toolkit.fluxcd.io
buckets.source.toolkit.fluxcd.io
gitrepositories.source.toolkit.fluxcd.io
helmcharts.source.toolkit.fluxcd.io
helmrepositories.source.toolkit.fluxcd.io
helmreleases.helm.toolkit.fluxcd.io
fluxconfigs.clusterconfig.azure.com
FluxConfig CRD:
fluxconfigs.clusterconfig.azure.com
自訂資源 (定義FluxConfig
Kubernetes 物件) 的自訂資源定義。fluxconfig-agent:負責監看 Azure 是否有新的或更新的
fluxConfigurations
資源,以及在叢集中啟動相關聯的 Flux 設定。 此外,還負責針對每個fluxConfigurations
資源,將叢集中的 Flux 狀態變更推送回 Azure。fluxconfig-controller:監看
fluxconfigs.clusterconfig.azure.com
自訂資源,並回應叢集中因為 GitOps 機制新增或更新設定而發生的變更。
注意
microsoft.flux
延伸模組安裝在 flux-system
命名空間中,具有全叢集範圍。 無法在命名空間範圍安裝此延伸模組,嘗試在命名空間範圍安裝會失敗,並發生 400 錯誤。
Flux 設定
您可以建立 Flux 設定資源 (Microsoft.KubernetesConfiguration/fluxConfigurations
),以支援 GitOps 從 Git 存放庫、貯體來源或 Azure Blob 儲存體來管理叢集。 當您建立 fluxConfigurations
資源時,您提供給 參數的值,例如目標 Git 存放庫,可用來建立及設定 Kubernetes 物件,以啟用該叢集中的 GitOps 程式。 為了確保資料安全性,叢集設定服務會以待用加密方式將 fluxConfigurations
資源資料儲存在 Azure Cosmos DB 資料庫中。
隨 microsoft.flux
延伸模組安裝的 fluxconfig-agent
和 fluxconfig-controller
代理程式管理 GitOps 設定程序。
fluxconfig-agent
負責:
- 輪詢 Kubernetes 設定資料平面服務是否有新的或更新的
fluxConfigurations
資源。 - 使用設定資訊在叢集中建立或更新
FluxConfig
自訂資源。 - 監看
FluxConfig
自訂資源,並將狀態變更推送回相關聯的 Azure fluxConfiguration 資源。
fluxconfig-controller
負責:
- 監看受控
fluxConfigurations
所建立 Flux 自訂資源的狀態更新。 - 建立在
fluxConfigurations
存留期內存在的私人/公開金鑰組。 如果 URL 以 SSH 為基礎,且使用者在建立設定期間未提供自己的私密金鑰,則會使用此金鑰進行驗證。 - 根據使用者提供的 private-key/http basic-auth/known-hosts/no-auth 資料,建立自訂驗證袐密。
- 設定 RBAC (佈建的服務帳戶、建立/指派的角色繫結、建立/指派的角色)。
- 根據
FluxConfig
自訂資源的資訊建立GitRepository
或Bucket
自訂資源和Kustomization
自訂資源。
Azure 中的每個 fluxConfigurations
資源會與 Kubernetes 叢集中的一個 Flux GitRepository
或 Bucket
自訂資源及一或多個 Kustomization
自訂資源產生關聯。 當您建立 fluxConfigurations
資源時,您會在每個 Kustomization
的來源中指定來源 (Git 存放庫、貯體或 Azure Blob 儲存體) 的 URL 及同步目標。 您可以設定 Kustomization
自訂資源之間的相依性,以控制部署順序。 此外,針對不同的應用程式和應用程式小組,您也可以在同一個叢集上建立命名空間範圍的多個 fluxConfigurations
資源。
注意
fluxconfig-agent
監視 Azure 中是否有新的或更新的 fluxConfiguration
資源。 此代理程式需要連線至 Azure,才能將 fluxConfiguration
的所需狀態套用至叢集。 如果此代理程式無法連線至 Azure,則在代理程式能夠連線之前,在叢集中變更會出現延遲。 如果叢集與 Azure 已中斷連線超過 48 小時,則對叢集的要求會逾時,而且必須在 Azure 中重新套用變更。
敏感性客戶輸入 (例如私密金鑰和權杖/密碼) 儲存在 Kubernetes 設定服務中不超過 48 小時。 如果您在 Azure 中更新上述任何值,請確定叢集在 48 小時內與 Azure 連線。
GitOps 與 Private Link
如果您已新增支援私人連結至已啟用 Azure Arc 的 Kubernetes 叢集,則 microsoft.flux
延伸模組立即可以與 Azure 恢復通訊。 若要連線至部署 Kubernetes 資訊清單所需的 Git 存放庫、Helm 存放庫或任何其他端點,您必須將這些端點佈建在防火牆後方,或列在防火牆上,Flux 來源控制器才能順利連線到這些端點。
資料存留處
Azure GitOps 服務 (Azure Kubernetes 組態管理) 儲存/處理客戶資料。 客戶資料預設會複寫至配對的區域。 在新加坡、東亞和巴西南部區域,所有客戶資料都在區域中儲存並處理。
大規模套用 Flux 設定
因為 Azure Resource Manager 會管理您的設定,在訂用帳戶或資源群組的範圍內,您可以使用 Azure 原則,跨所有 Azure Kubernetes Service 和已啟用 Azure Arc 的 Kubernetes 資源自動建立相同的設定。 此大規模強制執行可確保特定設定會一致地套用到整個叢集群組。
參數
如需 Flux 支援之所有參數的描述,請參閱 官方 Flux 檔。 Azure 中的 Flux 尚不支援所有參數。 讓我們知道 Azure 實作中是否有您需要的參數。
您可以使用 參數來查看 Azure CLI 命令支援 -h
的參數完整清單 k8s-configuration flux
:
az k8s-configuration flux -h
Group
az k8s-configuration flux : Commands to manage Flux v2 Kubernetes configurations.
Subgroups:
deployed-object : Commands to see deployed objects associated with Flux v2 Kubernetes
configurations.
kustomization : Commands to manage Kustomizations associated with Flux v2 Kubernetes
configurations.
Commands:
create : Create a Flux v2 Kubernetes configuration.
delete : Delete a Flux v2 Kubernetes configuration.
list : List all Flux v2 Kubernetes configurations.
show : Show a Flux v2 Kubernetes configuration.
update : Update a Flux v2 Kubernetes configuration.
以下是 CLI 命令的參數 k8s-configuration flux create
:
az k8s-configuration flux create -h
This command is from the following extension: k8s-configuration
Command
az k8s-configuration flux create : Create a Flux v2 Kubernetes configuration.
Arguments
--cluster-name -c [Required] : Name of the Kubernetes cluster.
--cluster-type -t [Required] : Specify Arc connected clusters or AKS managed clusters.
Allowed values: connectedClusters, managedClusters.
--name -n [Required] : Name of the flux configuration.
--resource-group -g [Required] : Name of resource group. You can configure the default group
using `az configure --defaults group=<name>`.
--url -u [Required] : URL of the source to reconcile.
--bucket-insecure : Communicate with a bucket without TLS. Allowed values: false,
true.
--bucket-name : Name of the S3 bucket to sync.
--container-name : Name of the Azure Blob Storage container to sync
--interval --sync-interval : Time between reconciliations of the source on the cluster.
--kind : Source kind to reconcile. Allowed values: bucket, git, azblob.
Default: git.
--kustomization -k : Define kustomizations to sync sources with parameters ['name',
'path', 'depends_on', 'timeout', 'sync_interval',
'retry_interval', 'prune', 'force'].
--namespace --ns : Namespace to deploy the configuration. Default: default.
--no-wait : Do not wait for the long-running operation to finish.
--scope -s : Specify scope of the operator to be 'namespace' or 'cluster'.
Allowed values: cluster, namespace. Default: cluster.
--suspend : Suspend the reconciliation of the source and kustomizations
associated with this configuration. Allowed values: false,
true.
--timeout : Maximum time to reconcile the source before timing out.
Auth Arguments
--local-auth-ref --local-ref : Local reference to a kubernetes secret in the configuration
namespace to use for communication to the source.
Bucket Auth Arguments
--bucket-access-key : Access Key ID used to authenticate with the bucket.
--bucket-secret-key : Secret Key used to authenticate with the bucket.
Git Auth Arguments
--https-ca-cert : Base64-encoded HTTPS CA certificate for TLS communication with
private repository sync.
--https-ca-cert-file : File path to HTTPS CA certificate file for TLS communication
with private repository sync.
--https-key : HTTPS token/password for private repository sync.
--https-user : HTTPS username for private repository sync.
--known-hosts : Base64-encoded known_hosts data containing public SSH keys
required to access private Git instances.
--known-hosts-file : File path to known_hosts contents containing public SSH keys
required to access private Git instances.
--ssh-private-key : Base64-encoded private ssh key for private repository sync.
--ssh-private-key-file : File path to private ssh key for private repository sync.
Git Repo Ref Arguments
--branch : Branch within the git source to reconcile with the cluster.
--commit : Commit within the git source to reconcile with the cluster.
--semver : Semver range within the git source to reconcile with the
cluster.
--tag : Tag within the git source to reconcile with the cluster.
Global Arguments
--debug : Increase logging verbosity to show all debug logs.
--help -h : Show this help message and exit.
--only-show-errors : Only show errors, suppressing warnings.
--output -o : Output format. Allowed values: json, jsonc, none, table, tsv,
yaml, yamlc. Default: json.
--query : JMESPath query string. See http://jmespath.org/ for more
information and examples.
--subscription : Name or ID of subscription. You can configure the default
subscription using `az account set -s NAME_OR_ID`.
--verbose : Increase logging verbosity. Use --debug for full debug logs.
Azure Blob Storage Account Auth Arguments
--sp_client_id : The client ID for authenticating a service principal with Azure Blob, required for this authentication method
--sp_tenant_id : The tenant ID for authenticating a service principal with Azure Blob, required for this authentication method
--sp_client_secret : The client secret for authenticating a service principal with Azure Blob
--sp_client_cert : The Base64 encoded client certificate for authenticating a service principal with Azure Blob
--sp_client_cert_password : The password for the client certificate used to authenticate a service principal with Azure Blob
--sp_client_cert_send_chain : Specifies whether to include x5c header in client claims when acquiring a token to enable subject name / issuer based authentication for the client certificate
--account_key : The Azure Blob Shared Key for authentication
--sas_token : The Azure Blob SAS Token for authentication
--mi_client_id : The client ID of the managed identity for authentication with Azure Blob
Examples
Create a Flux v2 Kubernetes configuration
az k8s-configuration flux create --resource-group my-resource-group \
--cluster-name mycluster --cluster-type connectedClusters \
--name myconfig --scope cluster --namespace my-namespace \
--kind git --url https://github.com/Azure/arc-k8s-demo \
--branch main --kustomization name=my-kustomization
Create a Kubernetes v2 Flux Configuration with Bucket Source Kind
az k8s-configuration flux create --resource-group my-resource-group \
--cluster-name mycluster --cluster-type connectedClusters \
--name myconfig --scope cluster --namespace my-namespace \
--kind bucket --url https://bucket-provider.minio.io \
--bucket-name my-bucket --kustomization name=my-kustomization \
--bucket-access-key my-access-key --bucket-secret-key my-secret-key
Create a Kubernetes v2 Flux Configuration with Azure Blob Storage Source Kind
az k8s-configuration flux create --resource-group my-resource-group \
--cluster-name mycluster --cluster-type connectedClusters \
--name myconfig --scope cluster --namespace my-namespace \
--kind azblob --url https://mystorageaccount.blob.core.windows.net \
--container-name my-container --kustomization name=my-kustomization \
--account-key my-account-key
組態一般引數
參數 | [格式] | 注意 |
---|---|---|
--cluster-name -c |
String | Azure 中叢集資源的名稱。 |
--cluster-type -t |
connectedClusters , managedClusters |
用於 connectedClusters 已啟用 Azure Arc 的 Kubernetes 叢集和 managedClusters AKS 叢集。 |
--resource-group -g |
String | 保留 Azure Arc 或 AKS 叢集資源的 Azure 資源組名。 |
--name -n |
String | Azure 中 Flux 組態的名稱。 |
--namespace --ns |
String | 要部署組態的命名空間名稱。 預設:default 。 |
--scope -s |
String | 運算子的許可權範圍。 可能的值為 cluster (完整存取) 或 namespace (限制存取) 。 預設:cluster 。 |
--suspend |
flag | 暫停此 Flux 設定中定義的所有來源和 kustomize 對帳。 暫停時作用中的對帳將會繼續。 |
來源一般引數
參數 | [格式] | 注意 |
---|---|---|
--kind |
String | 要協調的來源種類。 允許值:bucket 、git 、azblob 。 預設:git 。 |
--timeout |
golang 持續時間格式 | 在逾時之前嘗試協調來源的時間上限。預設值: 10m 。 |
--sync-interval --interval |
golang 持續時間格式 | 叢集上來源對帳之間的時間。 預設:10m 。 |
Git 存放庫來源參考引數
參數 | [格式] | 注意 |
---|---|---|
--branch |
String | Git 來源內的分支,以同步至叢集。 預設:master 。 較新的存放庫可能有名為 的 main 根分支,在此情況下,您必須設定 --branch=main 。 |
--tag |
String | Git 來源內的標籤,以同步至叢集。 範例: --tag=3.2.0 . |
--semver |
String | Git 來源內要同步至叢集的 Git 標籤 semver 範圍。 範例: --semver=">=3.1.0-rc.1 <3.2.0" . |
--commit |
String | Git 來源內的 Git 認可 SHA 以同步至叢集。 範例: --commit=363a6a8fe6a7f13e05d34c163b0ef02a777da20a . |
如需詳細資訊,請參閱 Git 存放 庫簽出策略的 Flux 檔。
公用 Git 存放庫
參數 | [格式] | 備註 |
---|---|---|
--url -u |
HTTP[s]://server/repo[.git] | 要與叢集協調的 Git 存放庫來源 URL。 |
具有 SSH 和 Flux 建立金鑰的私人 Git 存放庫
將 Flux 產生的公開金鑰新增至 Git 服務提供者的使用者帳戶中。
參數 | [格式] | 備註 |
---|---|---|
--url -u |
ssh://user@server/repo[.git] | git@ 如果公開金鑰與存放庫相關聯,而不是使用者帳戶,則應該取代 user@ 。 |
具有 SSH 和使用者提供的金鑰的私人 Git 存放庫
直接或從檔案使用您自己的私密金鑰。 金鑰必須是 PEM 格式 ,並以換行 () \n
結尾。
將關聯的公開金鑰新增至 Git 服務提供者中的使用者帳戶。
參數 | [格式] | 備註 |
---|---|---|
--url -u |
ssh://user@server/repo[.git] | git@ 如果公開金鑰與存放庫相關聯,而不是使用者帳戶,則應該取代 user@ 。 |
--ssh-private-key |
PEM 格式的 Base64 金鑰 | 直接提供金鑰。 |
--ssh-private-key-file |
本機檔案的完整路徑 | 提供包含 PEM 格式索引鍵之本機檔案的完整路徑。 |
具有 SSH 和使用者提供的已知主機的私人 Git 主機
Flux 操作員會在其檔案中 known_hosts
維護通用 Git 主機的清單。 Flux 會先使用這項資訊來驗證 Git 存放庫,再建立 SSH 連線。 如果您使用不常見的 Git 存放庫或自己的 Git 主機,您可以提供主機金鑰,讓 Flux 能夠識別您的存放庫。
就像私密金鑰一樣,您可以直接或在檔案中提供您的 known_hosts
內容。 當您提供自己的內容時,請使用 known_hosts內容格式規格,以及上述任一個 SSH 金鑰案例。
參數 | [格式] | 備註 |
---|---|---|
--url -u |
ssh://user@server/repo[.git] | git@ 可以取代 user@ 。 |
--known-hosts |
Base64 字串 | 直接提供 known_hosts 內容。 |
--known-hosts-file |
本機檔案的完整路徑 | 在本機檔案中提供 known_hosts 內容。 |
具有 HTTPS 使用者和金鑰的私人 Git 存放庫
參數 | [格式] | 備註 |
---|---|---|
--url -u |
https://server/repo[.git] |
具有基本驗證的 HTTPS。 |
--https-user |
原始字串 | HTTPS 使用者名稱。 |
--https-key |
原始字串 | HTTPS 個人存取權杖或密碼。 |
具有 HTTPS CA 憑證的私人 Git 存放庫
參數 | [格式] | 備註 |
---|---|---|
--url -u |
https://server/repo[.git] |
具有基本驗證的 HTTPS。 |
--https-ca-cert |
Base64 字串 | TLS 通訊的 CA 憑證。 |
--https-ca-cert-file |
本機檔案的完整路徑 | 在本機檔案中提供 CA 憑證內容。 |
貯體來源引數
如果您使用 bucket
來源而非 git
來源,以下是貯體特定的命令引數。
參數 | [格式] | 備註 |
---|---|---|
--url -u |
URL 字串 | 的 bucket URL。 支援的格式:HTTP://、HTTPs://。 |
--bucket-name |
String | 要同步處理的 bucket 名稱。 |
--bucket-access-key |
String | 用來向 進行驗證的存取金鑰識別碼 bucket 。 |
--bucket-secret-key |
String | 用來向 進行驗證的 bucket 秘密金鑰。 |
--bucket-insecure |
Boolean | 與沒有 TLS 的 bucket 通訊。 如果未提供,則假設為 false;如果提供,則假設為 true。 |
Azure Blob 儲存體帳戶來源引數
如果您使用 azblob
來源,以下是 Blob 特定的命令引數。
參數 | [格式] | 備註 |
---|---|---|
--url -u |
URL 字串 | 的 azblob URL。 |
--container-name |
String | 要同步Azure Blob 儲存體容器的名稱 |
--sp_client_id |
String | 向 Azure Blob 驗證服務主體所需的用戶端識別碼,這是此驗證方法的必要專案 |
--sp_tenant_id |
String | 使用此驗證方法所需的租使用者識別碼,以向 Azure Blob 驗證服務主體 |
--sp_client_secret |
String | 使用 Azure Blob 驗證服務主體的用戶端密碼 |
--sp_client_cert |
String | 使用 Azure Blob 驗證服務主體的 Base64 編碼用戶端憑證 |
--sp_client_cert_password |
String | 用來向 Azure Blob 驗證服務主體的用戶端憑證密碼 |
--sp_client_cert_send_chain |
String | 指定在取得權杖以啟用用戶端憑證主體名稱/簽發者型驗證時,是否要在用戶端宣告中包含 x5c 標頭 |
--account_key |
String | 用於驗證的 Azure Blob 共用金鑰 |
--sas_token |
String | 用於驗證的 Azure Blob SAS 權杖 |
--mi_client_id |
String | 使用 Azure Blob 進行驗證之受控識別的用戶端識別碼 |
使用來源進行驗證的本機密碼
您可以使用本機 Kubernetes 秘密向 或 bucket
azBlob
來源進行驗證 git
。 本機密碼必須包含來源所需的所有驗證參數,而且必須建立在與 Flux 組態相同的命名空間中。
參數 | [格式] | 注意 |
---|---|---|
--local-auth-ref --local-ref |
String | Flux 設定命名空間中 Kubernetes 秘密的本機參考,以用於向來源進行驗證。 |
針對 HTTPS 驗證,您可以使用 和 password
建立秘密 username
:
kubectl create ns flux-config
kubectl create secret generic -n flux-config my-custom-secret --from-literal=username=<my-username> --from-literal=password=<my-password-or-key>
針對 SSH 驗證,您可以使用 和 known_hosts
欄位建立秘密 identity
:
kubectl create ns flux-config
kubectl create secret generic -n flux-config my-custom-secret --from-file=identity=./id_rsa --from-file=known_hosts=./known_hosts
在這兩種情況下,當您建立 Flux 組態時,請使用 --local-auth-ref my-custom-secret
取代其他驗證參數:
az k8s-configuration flux create -g <cluster_resource_group> -c <cluster_name> -n <config_name> -t connectedClusters --scope cluster --namespace flux-config -u <git-repo-url> --kustomization name=kustomization1 --local-auth-ref my-custom-secret
深入瞭解如何搭配這些驗證方法使用本機 Kubernetes 秘密:
注意
如果您需要 Flux 透過 Proxy 存取來源,則必須使用 Proxy 設定來更新 Azure Arc 代理程式。 如需詳細資訊,請參閱使用輸出 Proxy 伺服器連線。
Git 實作
為了支援實作 Git 的各種存放庫提供者,Flux 可以設定為使用兩個 Git 程式庫的其中一個: go-git
或 libgit2
。 如需詳細資訊,請參閱 Flux 檔 。
Flux v2 的 GitOps 實作會自動決定要用於公用雲端存放庫的程式庫:
- 針對 GitHub、GitLab 和 BitBucket 存放庫,Flux 會使用
go-git
。 - 對於 Azure DevOps 和其他所有存放庫,Flux 會使用
libgit2
。
對於內部部署存放庫,Flux 會使用 libgit2
。
Kustomization
藉由使用 az k8s-configuration flux create
,您可以在組態期間建立一或多個 kustomizations。
參數 | [格式] | 備註 |
---|---|---|
--kustomization |
沒有任何值 | 設定 kustomization 的參數字串開頭。 您可以使用它多次來建立多個 kustomizations。 |
name |
String | 這個 kustomization 的唯一名稱。 |
path |
String | Git 存放庫中要與叢集協調的路徑。 預設值是分支的最上層。 |
prune |
Boolean | 預設為 false 。 設定 prune=true 為可確保從存放庫移除 Flux 部署至叢集的物件,或如果刪除 Flux 組態或 kustomizations,則會清除這些物件。 對於使用者無法存取叢集且只能透過 Git 存放庫進行變更的環境而言,使用 prune=true 非常重要。 |
depends_on |
String | 此組態內 (一或多個 kustomization 的名稱,) 必須協調此 kustomization 才能協調。 例如:depends_on=["kustomization1","kustomization2"] 。 請注意,如果您移除具有相依 kustomization 的 kustomization,相依 kustomizations 將會取得 DependencyNotReady 狀態,而對帳將會停止。 |
timeout |
golang 持續時間格式 | 預設:10m 。 |
sync_interval |
golang 持續時間格式 | 預設:10m 。 |
retry_interval |
golang 持續時間格式 | 預設:10m 。 |
validation |
String | 值: none 、、 server client 。 預設:none 。 如需詳細資訊,請參閱 Flux 檔 。 |
force |
Boolean | 預設:false 。 設定 force=true 為 指示 kustomize 控制器在修補失敗時重新建立資源,因為欄位變更不可變。 |
您也可以使用 az k8s-configuration flux kustomization
來建立、更新、列出、顯示和刪除 Flux 組態中的 kustomizations:
az k8s-configuration flux kustomization -h
Group
az k8s-configuration flux kustomization : Commands to manage Kustomizations associated with Flux
v2 Kubernetes configurations.
Commands:
create : Create a Kustomization associated with a Flux v2 Kubernetes configuration.
delete : Delete a Kustomization associated with a Flux v2 Kubernetes configuration.
list : List Kustomizations associated with a Flux v2 Kubernetes configuration.
show : Show a Kustomization associated with a Flux v2 Kubernetes configuration.
update : Update a Kustomization associated with a Flux v2 Kubernetes configuration.
以下是 kustomization 建立選項:
az k8s-configuration flux kustomization create -h
This command is from the following extension: k8s-configuration
Command
az k8s-configuration flux kustomization create : Create a Kustomization associated with a
Kubernetes Flux v2 Configuration.
Arguments
--cluster-name -c [Required] : Name of the Kubernetes cluster.
--cluster-type -t [Required] : Specify Arc connected clusters or AKS managed clusters.
Allowed values: connectedClusters, managedClusters.
--kustomization-name -k [Required] : Specify the name of the kustomization to target.
--name -n [Required] : Name of the flux configuration.
--resource-group -g [Required] : Name of resource group. You can configure the default
group using `az configure --defaults group=<name>`.
--dependencies --depends --depends-on : Comma-separated list of kustomization dependencies.
--force : Re-create resources that cannot be updated on the
cluster (i.e. jobs). Allowed values: false, true.
--interval --sync-interval : Time between reconciliations of the kustomization on the
cluster.
--no-wait : Do not wait for the long-running operation to finish.
--path : Specify the path in the source that the kustomization
should apply.
--prune : Garbage collect resources deployed by the kustomization
on the cluster. Allowed values: false, true.
--retry-interval : Time between reconciliations of the kustomization on the
cluster on failures, defaults to --sync-interval.
--timeout : Maximum time to reconcile the kustomization before
timing out.
Global Arguments
--debug : Increase logging verbosity to show all debug logs.
--help -h : Show this help message and exit.
--only-show-errors : Only show errors, suppressing warnings.
--output -o : Output format. Allowed values: json, jsonc, none,
table, tsv, yaml, yamlc. Default: json.
--query : JMESPath query string. See http://jmespath.org/ for more
information and examples.
--subscription : Name or ID of subscription. You can configure the
default subscription using `az account set -s
NAME_OR_ID`.
--verbose : Increase logging verbosity. Use --debug for full debug
logs.
Examples
Create a Kustomization associated with a Kubernetes v2 Flux Configuration
az k8s-configuration flux kustomization create --resource-group my-resource-group \
--cluster-name mycluster --cluster-type connectedClusters --name myconfig \
--kustomization-name my-kustomization-2 --path ./my/path --prune --force
多租用戶
Flux v2 支援0.26版中的多租使用者。 這項功能已與 Azure GitOps 與 Flux v2 整合。
注意
針對多租使用者功能,您必須知道資訊清單是否包含 HelmRelease、Kustomization、ImagePolicy 或其他物件的跨命名空間 sourceRef,或使用 小於 1.20.6 的 Kubernetes 版本。 若要準備:
- 升級至 Kubernetes 1.20.6 版或更新版本。
- 在您的 Kubernetes 資訊清單中,確保所有物件
sourceRef
都與 GitOps 組態位於相同的命名空間內。- 如果您需要時間更新資訊清單,您可以選擇 退出多租使用者。 不過,您仍然需要升級 Kubernetes 版本。
更新多租使用者的資訊清單
假設您將 部署到 fluxConfiguration
叢集設定命名空間中具有 叢集 範圍的其中一個 Kubernetes 叢集。 您可以設定來源來同步存放 https://github.com/fluxcd/flux2-kustomize-helm-example
庫。 這是 使用 GitOps 搭配 Flux v2 部署應用程式教學課程中使用的相同範例 Git 存放庫。 在 Flux 同步處理存放庫之後,它會部署資訊清單中所述的資源, (YAML 檔案) 。 其中兩個資訊清單描述 HelmRelease 和 HelmRepository 物件。
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
name: nginx
namespace: nginx
spec:
releaseName: nginx-ingress-controller
chart:
spec:
chart: nginx-ingress-controller
sourceRef:
kind: HelmRepository
name: bitnami
namespace: flux-system
version: "5.6.14"
interval: 1h0m0s
install:
remediation:
retries: 3
# Default values
# https://github.com/bitnami/charts/blob/master/bitnami/nginx-ingress-controller/values.yaml
values:
service:
type: NodePort
apiVersion: source.toolkit.fluxcd.io/v1beta1
kind: HelmRepository
metadata:
name: bitnami
namespace: flux-system
spec:
interval: 30m
url: https://charts.bitnami.com/bitnami
根據預設,Flux 擴充功能會模擬僅部署在叢集組態命名空間中的flux-applier服務帳戶來部署 fluxConfigurations
。 使用上述資訊清單時,當啟用多租使用者時,HelmRelease 會遭到封鎖。 這是因為 HelmRelease 位於 nginx 命名空間中,並且參考 flux-system 命名空間中的 HelmRepository。 此外,Flux helm-controller 無法套用 HelmRelease,因為nginx命名空間中沒有flux-applyr服務帳戶。
若要使用多租使用者,正確的方法是將所有 Flux 物件部署到與 fluxConfigurations
相同的命名空間。 這可避免跨命名空間參考問題,並允許 Flux 控制器取得套用物件的許可權。 因此,針對 在叢集設定 命名空間中建立的 GitOps 組態,上述資訊清單會變更為下列專案:
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
name: nginx
namespace: cluster-config
spec:
releaseName: nginx-ingress-controller
targetNamespace: nginx
chart:
spec:
chart: nginx-ingress-controller
sourceRef:
kind: HelmRepository
name: bitnami
namespace: cluster-config
version: "5.6.14"
interval: 1h0m0s
install:
remediation:
retries: 3
# Default values
# https://github.com/bitnami/charts/blob/master/bitnami/nginx-ingress-controller/values.yaml
values:
service:
type: NodePort
apiVersion: source.toolkit.fluxcd.io/v1beta1
kind: HelmRepository
metadata:
name: bitnami
namespace: cluster-config
spec:
interval: 30m
url: https://charts.bitnami.com/bitnami
退出宣告多租使用者
安裝擴充 microsoft.flux
功能時,預設會啟用多租使用者,以確保叢集中的安全性。 不過,如果您需要停用多租使用者,您可以在叢集中建立或更新 microsoft.flux
擴充功能,方法是使用 「--configuration-settings multiTenancy.enforce=false」 來退出宣告:
az k8s-extension create --extension-type microsoft.flux --configuration-settings multiTenancy.enforce=false -c CLUSTER_NAME -g RESOURCE_GROUP -n flux -t <managedClusters or connectedClusters>
az k8s-extension update --configuration-settings multiTenancy.enforce=false -c CLUSTER_NAME -g RESOURCE_GROUP -n flux -t <managedClusters or connectedClusters>
從 Flux v1 移轉
如果您已在已啟用 Azure Arc 的 Kubernetes 或 AKS 叢集中使用 Flux v1,而且想要移轉至在相同叢集中使用 Flux v2,您必須先從叢集中刪除 Flux v1 sourceControlConfigurations
。 microsoft.flux
如果叢集中有 Flux v1 sourceControlConfigurations
資源,叢集擴充功能將不會安裝。
使用這些 Azure CLI 命令來尋找並刪除叢集中的現有 sourceControlConfigurations
命令:
az k8s-configuration list --cluster-name <Arc or AKS cluster name> --cluster-type <connectedClusters OR managedClusters> --resource-group <resource group name>
az k8s-configuration delete --name <configuration name> --cluster-name <Arc or AKS cluster name> --cluster-type <connectedClusters OR managedClusters> --resource-group <resource group name>
您也可以使用 Azure 入口網站,在已啟用 Azure Arc 的 Kubernetes 或 AKS 叢集中檢視和刪除現有的 GitOps 組態。
如需從 Flux v1 移轉至 Flux v2 的詳細資訊,請參閱 fluxcd 專案: 從 Flux v1 移轉至 v2。
下一步
- 使用我們的教學課程,瞭解如何 在 AKS 或已啟用 Azure Arc 的 Kubernetes 叢集上啟用 GitOps。
- 瞭解 使用 GitOps 的 CI/CD 工作流程。