自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

flux2+kustomize+helm+github 多集群 GitOps 云原生漸進式交付

云計算 云原生
我們將配置 Flux 以使用 HelmRepository 和 HelmRelease 自定義資源安裝、測試和升級演示應用程序。 Flux 將監(jiān)控 Helm 存儲庫,并根據(jù) semver 范圍自動將 Helm 版本升級到最新的 chart 版本。

對于此示例,我們假設有兩個集群的場景:暫存(staging)和生產(chǎn)(production)。最終目標是利用 Flux 和 Kustomize 來管理兩個集群,同時最大限度地減少重復聲明。

我們將配置 Flux 以使用 HelmRepository 和 HelmRelease 自定義資源安裝、測試和升級演示應用程序。 Flux 將監(jiān)控 Helm 存儲庫,并根據(jù) semver 范圍自動將 Helm 版本升級到最新的 chart 版本。

準備工作

flux2-kustomize-helm-example

  • https://github.com/fluxcd/flux2-kustomize-helm-example

您將需要 Kubernetes 集群版本 1.16 或更新版本以及 kubectl 版本 1.18 或更新。對于快速的本地測試,您可以使用 Kubernetes kind。不過,任何其他 Kubernetes 設置也可以正常工作。

為了遵循本指南,您需要一個 GitHub 帳戶和一個可以創(chuàng)建存儲庫的 personal access token(檢查 repo 下的所有權限)。

使用 Homebrew 在 MacOS 和 Linux 上安裝 Flux CLI:

  1. brew install fluxcd/tap/flux 

或者通過使用 Bash 腳本下載預編譯的二進制文件來安裝 CLI:

  1. curl -s https://fluxcd.io/install.sh | sudo bash 

項目結構

Git 存儲庫包含以下頂級目錄:

  • apps 目錄包含每個集群具有自定義配置的 Helm 版本
  • infrastructure 目錄包含常見的基礎設施工具,例如 NGINX ingress controller 和 Helm 存儲庫定義
  • clusters 目錄包含每個集群的 Flux 配置
  1. ├── apps 
  2. │   ├── base 
  3. │   ├── production  
  4. │   └── staging 
  5. ├── infrastructure 
  6. │   ├── nginx 
  7. │   ├── redis 
  8. │   └── sources 
  9. └── clusters 
  10.     ├── production 
  11.     └── staging 

apps 配置結構為:

  • apps/base/ 目錄包含命名空間和 Helm 發(fā)布定義(release definitions)
  • apps/production/ 目錄包含生產(chǎn) Helm 發(fā)布值(release values)
  • apps/staging/ 目錄包含 staging values
  1. ./apps/ 
  2. ├── base 
  3. │   └── podinfo 
  4. │       ├── kustomization.yaml 
  5. │       ├── namespace.yaml 
  6. │       └── release.yaml 
  7. ├── production 
  8. │   ├── kustomization.yaml 
  9. │   └── podinfo-patch.yaml 
  10. └── staging 
  11.     ├── kustomization.yaml 
  12.     └── podinfo-patch.yaml 

在 apps/base/podinfo/ 目錄中,我們有一個 HelmRelease,兩個集群都有共同的值:

  1. apiVersion: helm.toolkit.fluxcd.io/v2beta1 
  2. kind: HelmRelease 
  3. metadata: 
  4.   name: podinfo 
  5.   namespace: podinfo 
  6. spec: 
  7.   releaseName: podinfo 
  8.   chart: 
  9.     spec: 
  10.       chart: podinfo 
  11.       sourceRef: 
  12.         kind: HelmRepository 
  13.         name: podinfo 
  14.         namespace: flux-system 
  15.   interval: 5m 
  16.   values
  17.     cache: redis-master.redis:6379 
  18.     ingress: 
  19.       enabled: true 
  20.       annotations: 
  21.         kubernetes.io/ingress.class: nginx 
  22.       path: "/*" 

在 apps/staging/ 目錄中,我們有一個帶有 staging 特定值的 Kustomize 補丁(patch):

  1. apiVersion: helm.toolkit.fluxcd.io/v2beta1 
  2. kind: HelmRelease 
  3. metadata: 
  4.   name: podinfo 
  5. spec: 
  6.   chart: 
  7.     spec: 
  8.       version: ">=1.0.0-alpha" 
  9.   test: 
  10.     enable: true 
  11.   values
  12.     ingress: 
  13.       hosts: 
  14.         - podinfo.staging 

請注意,使用 version: ">=1.0.0-alpha" 我們配置 Flux 以自動將 HelmRelease 升級到最新的 chart 版本,包括 alpha、beta 和預發(fā)布(pre-releases)。

在 apps/production/ 目錄中,我們有一個帶有生產(chǎn)特定值的 Kustomize 補?。?/p>

  1. apiVersion: helm.toolkit.fluxcd.io/v2beta1 
  2. kind: HelmRelease 
  3. metadata: 
  4.   name: podinfo 
  5.   namespace: podinfo 
  6. spec: 
  7.   chart: 
  8.     spec: 
  9.       version: ">=1.0.0" 
  10.   values
  11.     ingress: 
  12.       hosts: 
  13.         - podinfo.production 

請注意,使用 version: ">=1.0.0" 我們配置 Flux 以自動將 HelmRelease 升級到 最新的穩(wěn)定 chart 版本(alpha、beta 和 pre-releases 將被忽略)。

基礎設施:

  1. ./infrastructure/ 
  2. ├── nginx 
  3. │   ├── kustomization.yaml 
  4. │   ├── namespace.yaml 
  5. │   └── release.yaml 
  6. ├── redis 
  7. │   ├── kustomization.yaml 
  8. │   ├── namespace.yaml 
  9. │   └── release.yaml 
  10. └── sources 
  11.     ├── bitnami.yaml 
  12.     ├── kustomization.yaml 
  13.     └── podinfo.yaml 

在 infrastructure/sources/ 目錄中,我們有 Helm 存儲庫定義:

  1. apiVersion: source.toolkit.fluxcd.io/v1beta1 
  2. kind: HelmRepository 
  3. metadata: 
  4.   name: podinfo 
  5. spec: 
  6.   interval: 5m 
  7.   url: https://stefanprodan.github.io/podinfo 
  8. --- 
  9. apiVersion: source.toolkit.fluxcd.io/v1beta1 
  10. kind: HelmRepository 
  11. metadata: 
  12.   name: bitnami 
  13. spec: 
  14.   interval: 30m 
  15.   url: https://charts.bitnami.com/bitnami 

請注意,使用 interval: 5m 我們將 Flux 配置為每五分鐘拉一次 Helm 存儲庫索引。如果索引包含與 HelmRelease semver 范圍匹配的新 chart 版本,F(xiàn)lux 將升級該版本。

Bootstrap staging 和 production

集群目錄包含 Flux 配置:

  1. ./clusters/ 
  2. ├── production 
  3. │   ├── apps.yaml 
  4. │   └── infrastructure.yaml 
  5. └── staging 
  6.     ├── apps.yaml 
  7.     └── infrastructure.yaml 

在 clusters/staging/ 目錄中,我們有 Kustomization 定義:

  1. apiVersion: kustomize.toolkit.fluxcd.io/v1beta1 
  2. kind: Kustomization 
  3. metadata: 
  4.   name: apps 
  5.   namespace: flux-system 
  6. spec: 
  7.   interval: 10m0s 
  8.   dependsOn: 
  9.     - name: infrastructure 
  10.   sourceRef: 
  11.     kind: GitRepository 
  12.     name: flux-sytem 
  13.   path: ./apps/staging 
  14.   prune: true 
  15.   validation: client 
  16. --- 
  17. apiVersion: kustomize.toolkit.fluxcd.io/v1beta1 
  18. kind: Kustomization 
  19. metadata: 
  20.   name: infrastructure 
  21.   namespace: flux-system 
  22. spec: 
  23.   interval: 10m0s 
  24.   sourceRef: 
  25.     kind: GitRepository 
  26.     name: flux-system 
  27.   path: ./infrastructure 

請注意,使用 path: ./apps/staging 我們配置 Flux 以同步暫存 Kustomize 覆蓋,并使用 dependsOn 我們告訴 Flux 在部署應用程序之前創(chuàng)建基礎設施項。

在您的個人 GitHub 帳戶上 Fork 此存儲庫并導出您的 GitHub access token、用戶名和存儲庫名稱:

  1. export GITHUB_TOKEN=<your-token> 
  2. export GITHUB_USER=<your-username> 
  3. export GITHUB_REPO=<repository-name

驗證您的臨時集群是否滿足先決條件:

  1. flux check --pre 

將 kubectl context 設置為您的 staging 集群和 bootstrap Flux:

  1. flux bootstrap github \ 
  2.     --context=staging \ 
  3.     --owner=${GITHUB_USER} \ 
  4.     --repository=${GITHUB_REPO} \ 
  5.     --branch=main \ 
  6.     --personal \ 
  7.     --path=clusters/staging 

bootstrap 命令在 clusters/staging/flux-system 目錄中提交 Flux 組件的清單,并在 GitHub 上創(chuàng)建一個具有只讀訪問權限的部署密鑰,因此它可以在集群內拉取更改(pull changes)。

注意在 staging 上安裝的 Helm releases:

  1. $ watch flux get helmreleases --all-namespaces  
  2. NAMESPACE   NAME    REVISION    SUSPENDED   READY   MESSAGE                           
  3. nginx       nginx   5.6.14      False       True    release reconciliation succeeded     
  4. podinfo     podinfo 5.0.3       False       True    release reconciliation succeeded     
  5. redis       redis   11.3.4      False       True    release reconciliation succeeded 

驗證 demo app 是否可以通過 ingress 訪問:

  1. $ kubectl -n nginx port-forward svc/nginx-ingress-controller 8080:80 & 
  2.  
  3. $ curl -H "Host: podinfo.staging" http://localhost:8080 
  4.   "hostname""podinfo-59489db7b5-lmwpn"
  5.   "version""5.0.3" 

通過設置生產(chǎn)集群的上下文和路徑來引導生產(chǎn)上的 Flux:

  1. flux bootstrap github \ 
  2.     --context=production \ 
  3.     --owner=${GITHUB_USER} \ 
  4.     --repository=${GITHUB_REPO} \ 
  5.     --branch=main \ 
  6.     --personal \ 
  7.     --path=clusters/production 

監(jiān)控 production reconciliation:

  1. $ watch flux get kustomizations 
  2. NAME            REVISION                                        READY 
  3. apps            main/797cd90cc8e81feb30cfe471a5186b86daf2758d   True 
  4. flux-system     main/797cd90cc8e81feb30cfe471a5186b86daf2758d   True 
  5. infrastructure  main/797cd90cc8e81feb30cfe471a5186b86daf2758d   True 

加密 Kubernetes secrets

為了將 secrets 安全地存儲在 Git 存儲庫中, 您可以使用 Mozilla 的 SOPS CLI 通過 OpenPGP 或 KMS 加密 Kubernetes secrets。

安裝 gnupg 和 sops:

  1. brew install gnupg sops 

為 Flux 生成一個不指定密碼短語(passphrase)的 GPG key,并獲取GPG key ID:

  1. $ gpg --full-generate-key 
  2. Email address: fluxcdbot@users.noreply.github.com 
  3.  
  4. $ gpg --list-secret-keys fluxcdbot@users.noreply.github.com 
  5. sec   rsa3072 2020-09-06 [SC] 
  6.       1F3D1CED2F865F5E59CA564553241F147E7C5FA4 

使用 private key 在集群上創(chuàng)建 Kubernetes secret:

  1. gpg --export-secret-keys \ 
  2. --armor 1F3D1CED2F865F5E59CA564553241F147E7C5FA4 | 
  3. kubectl create secret generic sops-gpg \ 
  4. --namespace=flux-system \ 
  5. --from-file=sops.asc=/dev/stdin 

生成 Kubernetes secret manifest 并使用 sops 加密 secret 的數(shù)據(jù)字段:

  1. kubectl -n redis create secret generic redis-auth \ 
  2. --from-literal=password=change-me \ 
  3. --dry-run=client \ 
  4. -o yaml > infrastructure/redis/redis-auth.yaml 
  5.  
  6. sops --encrypt \ 
  7. --pgp=1F3D1CED2F865F5E59CA564553241F147E7C5FA4 \ 
  8. --encrypted-regex '^(data|stringData)$' \ 
  9. --in-place infrastructure/redis/redis-auth.yaml 

添加 secret 到 infrastructure/redis/kustomization.yaml:

  1. apiVersion: kustomize.config.k8s.io/v1beta1 
  2. kind: Kustomization 
  3. namespace: redis 
  4. resources: 
  5.   - namespace.yaml 
  6.   - release.yaml 
  7.   - redis-auth.yaml 

通過編輯 infrastructure.yaml 文件在集群上啟用解密:

  1. apiVersion: kustomize.toolkit.fluxcd.io/v1beta1 
  2. kind: Kustomization 
  3. metadata: 
  4.   name: infrastructure 
  5.   namespace: flux-system 
  6. spec: 
  7.   # content omitted for brevity 
  8.   decryption: 
  9.     provider: sops 
  10.     secretRef: 
  11.       name: sops-gpg 

導出公鑰(public key),以便任何有權訪問存儲庫的人都可以加密 secrets 但不能解密它們:

  1. gpg --export -a fluxcdbot@users.noreply.github.com > public.key 

將更改推送到主分支:

  1. git add -A && git commit -m "add encrypted secret" && git push 

驗證是否已在兩個集群的 redis 命名空間中創(chuàng)建了 secret:

  1. kubectl --context staging -n redis get secrets 
  2. kubectl --context production -n redis get secrets 

您可以使用 Kubernetes secrets 為您的 Helm releases 提供值:

  1. apiVersion: helm.toolkit.fluxcd.io/v2beta1 
  2. kind: HelmRelease 
  3. metadata: 
  4.   name: redis 
  5. spec: 
  6.   # content omitted for brevity 
  7.   values
  8.     usePassword: true 
  9.   valuesFrom: 
  10.   - kind: Secret 
  11.     name: redis-auth 
  12.     valuesKey: password 
  13.     targetPath: password 

在 docs 中了解有關 Helm releases values 覆蓋的更多信息。

添加集群

如果要將集群添加到你的 fleet 中,請先在本地克隆存儲庫:

  1. git clone https://github.com/${GITHUB_USER}/${GITHUB_REPO}.git 
  2. cd ${GITHUB_REPO} 

使用您的集群名稱在 clusters 中創(chuàng)建一個目錄:

  1. mkdir -p clusters/dev 

從 staging 復制同步清單:

  1. cp clusters/staging/infrastructure.yaml clusters/dev 
  2. cp clusters/staging/apps.yaml clusters/dev 

您可以在 apps 內創(chuàng)建一個 dev overlay,確保將 clusters/dev/apps.yaml 內的 spec.path 更改為 path: ./apps/dev。

將更改推送到主分支:

  1. git add -A && git commit -m "add dev cluster" && git push 

將 kubectl 上下文和路徑設置為您的 dev cluster 并引導 Flux:

  1. flux bootstrap github \ 
  2.     --context=dev \ 
  3.     --owner=${GITHUB_USER} \ 
  4.     --repository=${GITHUB_REPO} \ 
  5.     --branch=main \ 
  6.     --personal \ 
  7.     --path=clusters/dev 

相同的環(huán)境

如果你想啟動一個相同的環(huán)境,你可以引導一個集群,例如 production-clone 并重用 production 定義。

引導 production-clone 集群:

  1. flux bootstrap github \ 
  2.     --context=production-clone \ 
  3.     --owner=${GITHUB_USER} \ 
  4.     --repository=${GITHUB_REPO} \ 
  5.     --branch=main \ 
  6.     --personal \ 
  7.     --path=clusters/production-clone 

在本地拉取更改:

  1. git pull origin main 

在 clusters/production-clone 目錄中創(chuàng)建一個 kustomization.yaml:

  1. apiVersion: kustomize.config.k8s.io/v1beta1 
  2. kind: Kustomization 
  3. resources: 
  4.   - flux-system 
  5.   - ../production/infrastructure.yaml 
  6.   - ../production/apps.yaml 

請注意,除了 flux-system kustomize overlay,我們還包括來自 production 目錄的 infrastructure 和 apps 清單。

將更改推送到主分支:

  1. git add -A && git commit -m "add production clone" && git push 

告訴 Flux 在 production-clone 集群上部署生產(chǎn)工作負載(production workloads):

  1. flux reconcile kustomization flux-system \ 
  2.     --context=production-clone \ 
  3.     --with-source 

 

責任編輯:姜華 來源: 黑客下午茶
相關推薦

2021-06-03 05:48:58

GitOps 云原生Kubernetes

2023-04-11 07:59:56

Kruise漸進式交付

2014-12-16 13:51:55

華為eSpace UC統(tǒng)一通信

2010-04-27 13:41:42

云計算

2021-07-13 06:35:11

Argo Rollou GitOpsKubernetes

2022-08-22 10:40:40

Kubernete部署分析運行

2020-06-15 08:08:37

云原生CNCF技術雷達

2024-11-04 16:04:06

2022-12-06 17:32:18

2021-07-16 06:40:19

Argo RollouAnalysis云原生

2023-09-28 07:34:33

2024-11-20 09:39:56

漸進式遷移云策略云支出

2022-12-20 10:15:45

開發(fā)集群

2016-01-05 16:07:17

2021-12-21 11:01:30

自動駕駛數(shù)據(jù)人工智能

2023-10-07 07:51:55

FluxCDKubernetes

2023-10-27 07:36:36

2024-01-29 01:15:11

HelmKubernetesKustomize

2023-10-15 16:54:55

云原生
點贊
收藏

51CTO技術棧公眾號