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

使用Flux+Flagger+Istio+Kubernetes實(shí)戰(zhàn)GitOps云原生漸進(jìn)式(金絲雀)交付

云計(jì)算 云原生
漸進(jìn)交付是高級(jí)部署模式(如金絲雀canaries、功能標(biāo)志feature flags和A/B測(cè)試A/B testing)的總稱(chēng)。漸進(jìn)交付技術(shù)通過(guò)讓?xiě)?yīng)用程序開(kāi)發(fā)人員和 SRE 團(tuán)隊(duì)對(duì)爆炸半徑blast radius進(jìn)行細(xì)粒度控制,從而降低在生產(chǎn)中引入新軟件版本的風(fēng)險(xiǎn)。

[[403388]]

在這篇指南中,你將獲得使用 Kubernetes 和 Istio 使用 GitOps 進(jìn)行漸進(jìn)式交付(Progressive Delivery)的實(shí)際經(jīng)驗(yàn)。

介紹

Demo: https://github.com/stefanprodan/gitops-istio

GitOps 是什么?

GitOps 是一種進(jìn)行持續(xù)交付的方式,它使用 Git 作為聲明性(declarative)基礎(chǔ)設(shè)施和工作負(fù)載(workloads)的真實(shí)來(lái)源。對(duì)于 Kubernetes,這意味著使用 git push 而不是 kubectl apply/delete 或 helm install/upgrade。

在這個(gè) workshop 中,您將使用 GitHub 來(lái)托管配置存儲(chǔ)庫(kù),并將 Flux 作為 GitOps 交付解決方案。

什么是漸進(jìn)式交付?

漸進(jìn)交付是高級(jí)部署模式(如金絲雀canaries、功能標(biāo)志feature flags和A/B測(cè)試A/B testing)的總稱(chēng)。漸進(jìn)交付技術(shù)通過(guò)讓?xiě)?yīng)用程序開(kāi)發(fā)人員和 SRE 團(tuán)隊(duì)對(duì)爆炸半徑blast radius進(jìn)行細(xì)粒度控制,從而降低在生產(chǎn)中引入新軟件版本的風(fēng)險(xiǎn)。

在這個(gè) workshop 中,您將使用 Flagger 和 Prometheus 為您的應(yīng)用程序自動(dòng)執(zhí)行 Canary 發(fā)布和 A/B Testing。

準(zhǔn)備工作

您將需要具有 LoadBalancer 支持的 Kubernetes 集群 v1.16 或更高版本。出于測(cè)試目的,您可以使用帶有 2 個(gè) CPU 和 4GB 內(nèi)存的 Minikube。

使用 Homebrew 安裝 flux CLI:

  1. brew install fluxcd/tap/flux 

macOS AMD64/ARM64、Linux AMD64/ARM 和 Windows 的二進(jìn)制文件可在 flux2 release page 下載。

驗(yàn)證您的集群是否滿足前提條件:

  1. flux check --pre 

使用 Homebrew 安裝 jq 和 yq:

  1. brew install jq yq 

Fork 這個(gè)倉(cāng)庫(kù)并克隆它:

  1. git clone https://github.com/<YOUR-USERNAME>/gitops-istio 
  2. cd gitops-istio 

Cluster bootstrap

使用 flux bootstrap 命令,您可以在 Kubernetes 集群上安裝 Flux 并將其配置為從 Git 存儲(chǔ)庫(kù)管理自身。如果集群上存在 Flux 組件,則 bootstrap 命令將在需要時(shí)執(zhí)行升級(jí)。

通過(guò)指定您的 GitHub 存儲(chǔ)庫(kù) fork URL 來(lái)引導(dǎo) Flux:

  1. flux bootstrap git \ 
  2.   --author-email=<YOUR-EMAIL> \ 
  3.   --url=ssh://git@github.com/<YOUR-USERNAME>/gitops-istio \ 
  4.   --branch=main \ 
  5.   --path=clusters/my-cluster 

上面的命令需要 ssh-agent,如果您使用的是 Windows,請(qǐng)參閱 flux bootstrap github 文檔。

在引導(dǎo)時(shí),F(xiàn)lux 生成一個(gè) SSH key 并打印 public key。為了用 git 同步你的集群狀態(tài),你需要復(fù)制 public key 并使用 write 創(chuàng)建一個(gè) deploy key 訪問(wèn)你的 GitHub 倉(cāng)庫(kù)。在 GitHub 上轉(zhuǎn)到 Settings > Deploy keys 點(diǎn)擊 Add deploy key, 勾選☑️ Allow write access,粘貼 Flux public key 并單擊 Add key。

當(dāng) Flux 訪問(wèn)你的存儲(chǔ)庫(kù)時(shí),它會(huì)做以下事情:

  • 安裝 Istio operator
  • 等待 Istio 控制平面準(zhǔn)備好
  • 安裝 Flagger、Prometheus 和 Grafana
  • 創(chuàng)建 Istio 公共網(wǎng)關(guān)
  • 創(chuàng)建 prod 命名空間namespace
  • 創(chuàng)建負(fù)載測(cè)試器(load tester) deployment
  • 創(chuàng)建前端(frontend) deployment 和金絲雀canary
  • 創(chuàng)建后端(backend) deployment 和金絲雀canary

使用 Istio 引導(dǎo)集群時(shí),定義 apply 順序很重要。對(duì)于要使用 Istio sidecar 注入的應(yīng)用程序 pod,Istio 控制平面必須在應(yīng)用程序之前啟動(dòng)并運(yùn)行。

在 Flux v2 中,你可以通過(guò)定義對(duì)象之間的依賴(lài)關(guān)系來(lái)指定執(zhí)行順序。例如,在 clusters/my-cluster/apps.yaml 中我們告訴 Flux,apps 的協(xié)調(diào)取決于一個(gè) istio-system :

  1. apiVersion: kustomize.toolkit.fluxcd.io/v1beta1 
  2. kind: Kustomization 
  3. metadata: 
  4.   name: apps 
  5.   namespace: flux-system 
  6. spec: 
  7.   interval: 30m0s 
  8.   dependsOn: 
  9.     - name: istio-system 
  10.   sourceRef: 
  11.     kind: GitRepository 
  12.     name: flux-system 
  13.   path: ./apps 

首先觀測(cè) Flux 安裝 Istio,然后觀測(cè) demo apps:

  1. watch flux get kustomizations 

您可以使用以下命令跟蹤 Flux reconciliation 日志:

  1. flux logs --all-namespaces --follow --tail=10 

Istio 定制和升級(jí)

您可以使用位于 istio/system/profile.yaml 的 IstioOperator 資源自定義 Istio 安裝:

  1. apiVersion: install.istio.io/v1alpha1 
  2. kind: IstioOperator 
  3. metadata: 
  4.   name: istio-default 
  5.   namespace: istio-system 
  6. spec: 
  7.   profile: demo 
  8.   components: 
  9.     pilot: 
  10.       k8s: 
  11.         resources: 
  12.           requests: 
  13.             cpu: 10m 
  14.             memory: 100Mi 

修改 Istio 設(shè)置后,您可以將更改推送到 git,F(xiàn)lux 將在集群上應(yīng)用它。Istio operator 將根據(jù)您的更改重新配置 Istio 控制平面。

當(dāng)新的 Istio 版本可用時(shí),update-istio GitHub Action workflow 工作流將打開(kāi)一個(gè)pull request,其中包含升級(jí) Istio Operator 所需的清單更新。新的 Istio 版本通過(guò) e2e workflow 在 Kubernetes Kind 上進(jìn)行測(cè)試,當(dāng) PR 合并到主分支時(shí),F(xiàn)lux 將在集群內(nèi)升級(jí) Istio。

應(yīng)用程序引導(dǎo)

當(dāng) Flux 將 Git 存儲(chǔ)庫(kù)與您的集群同步時(shí),它將創(chuàng)建前端/后端部署(frontend/backend deployment)、HPA 和一個(gè)金絲雀對(duì)象canary object。Flagger 使用 canary 定義創(chuàng)建了一系列對(duì)象:Kubernetes deployments、ClusterIP services、Istio 目標(biāo)規(guī)則(destination rules)和虛擬服務(wù)(virtual services)。這些對(duì)象在網(wǎng)格(mesh)上公開(kāi)(expose)應(yīng)用程序,并推動(dòng)金絲雀分析(canary analysis)和推廣(promotion)。

  1. # applied by Flux 
  2. deployment.apps/frontend 
  3. horizontalpodautoscaler.autoscaling/frontend 
  4. canary.flagger.app/frontend 
  5.  
  6. # generated by Flagger 
  7. deployment.apps/frontend-primary 
  8. horizontalpodautoscaler.autoscaling/frontend-primary 
  9. service/frontend 
  10. service/frontend-canary 
  11. service/frontend-primary 
  12. destinationrule.networking.istio.io/frontend-canary 
  13. destinationrule.networking.istio.io/frontend-primary 
  14. virtualservice.networking.istio.io/frontend 

檢查 Flagger 是否成功初始化了金絲雀:

  1. kubectl -n prod get canaries 
  2.  
  3. NAME       STATUS        WEIGHT 
  4. backend    Initialized   0 
  5. frontend   Initialized   0 

當(dāng) frontend-primary 部署上線時(shí),F(xiàn)lager 會(huì)將所有流量路由到主 Pod,并將 frontend 部署 scale 到零。

使用以下命令查找 Istio 入口網(wǎng)關(guān)(ingress gateway)地址:

  1. kubectl -n istio-system get svc istio-ingressgateway -ojson | jq .status.loadBalancer.ingress 

打開(kāi)瀏覽器并導(dǎo)航到入口地址,您將看到前端 UI。

金絲雀發(fā)布

Flagger 實(shí)現(xiàn)了一個(gè)控制循環(huán),該控制循環(huán)在測(cè)量關(guān)鍵性能指標(biāo)(如 HTTP 請(qǐng)求成功率、請(qǐng)求平均持續(xù)時(shí)間和 pod 運(yùn)行狀況)的同時(shí),逐步將流量轉(zhuǎn)移到金絲雀。在分析 KPI 的基礎(chǔ)上,將金絲雀升級(jí)或中止,并將分析結(jié)果發(fā)布到 Slack。

金絲雀分析由以下任何對(duì)象的更改觸發(fā):

部署 PodSpec(容器鏡像、命令、端口、環(huán)境等)

ConfigMaps 和 Secrets 作為卷(volumes)掛載或映射到環(huán)境變量

對(duì)于不接收恒定流量的工作負(fù)載,F(xiàn)lagger 可以配置一個(gè) webhook,當(dāng)它被調(diào)用時(shí),將啟動(dòng)一個(gè)目標(biāo)工作負(fù)載的負(fù)載測(cè)試。canary 配置可以在 apps/backend/canary.yaml 上找到。

從 GitHub 拉取更改:

  1. git pull origin main 

要觸發(fā)后端應(yīng)用程序的金絲雀部署,請(qǐng)碰撞容器鏡像:

  1. yq e '.images[0].newTag="5.0.1"' -i ./apps/backend/kustomization.yaml 

提交和推送更改:

  1. git add -A && \ 
  2. git commit -m "backend 5.0.1" && \ 
  3. git push origin main 

告訴 Flux 拉取更改或等待一分鐘讓 Flux 自行檢測(cè)更改:

  1. flux reconcile source git flux-system 

觀測(cè) Flux 將您的集群與最新提交進(jìn)行協(xié)調(diào):

  1. watch flux get kustomizations 

幾秒鐘后,F(xiàn)lager 檢測(cè)到部署修訂(deployment revision)已更改并開(kāi)始新的 rollout:

  1. $ kubectl -n prod describe canary backend 
  2.  
  3. Events: 
  4.  
  5. New revision detected! Scaling up backend.prod 
  6. Starting canary analysis for backend.prod 
  7. Pre-rollout check conformance-test passed 
  8. Advance backend.prod canary weight 5 
  9. ... 
  10. Advance backend.prod canary weight 50 
  11. Copying backend.prod template spec to backend-primary.prod 
  12. Promotion completed! Scaling down backend.prod 

在分析過(guò)程中,Grafana 可以監(jiān)控金絲雀的進(jìn)程。您可以通過(guò)端口轉(zhuǎn)發(fā)訪問(wèn)儀表板:

  1. kubectl -n istio-system port-forward svc/flagger-grafana 3000:80 

Istio 儀表板的 URL 是 http://localhost:3000/d/flagger-istio/istio-canary?refresh=10s&orgId=1&var-namespace=prod&var-primary=backend-primary&var-canary=backend

請(qǐng)注意,如果在金絲雀分析(canary analysis)期間對(duì)部署應(yīng)用了新的更改,F(xiàn)lagger 將重新啟動(dòng)分析階段。

A/B 測(cè)試

除了加權(quán)路由(weighted routing),F(xiàn)lagger 還可以配置為根據(jù) HTTP 匹配條件將流量路由到金絲雀。在 A/B 測(cè)試場(chǎng)景中,您將使用 HTTP headers 或 cookie 來(lái)定位用戶(hù)的特定部分。這對(duì)于需要會(huì)話(session)關(guān)聯(lián)的前端應(yīng)用程序特別有用。

您可以通過(guò)指定 HTTP 匹配條件和迭代次數(shù)來(lái)啟用 A/B 測(cè)試:

  1. analysis: 
  2.   # schedule interval (default 60s) 
  3.   interval: 10s 
  4.   # max number of failed metric checks before rollback 
  5.   threshold: 10 
  6.   # total number of iterations 
  7.   iterations: 12 
  8.   # canary match condition 
  9.   match: 
  10.     - headers: 
  11.         user-agent: 
  12.           regex: ".*Firefox.*" 
  13.     - headers: 
  14.         cookie: 
  15.           regex: "^(.*?;)?(type=insider)(;.*)?$" 

上述配置將針對(duì) Firefox 用戶(hù)和擁有內(nèi)部 cookie 的用戶(hù)運(yùn)行兩分鐘的分析。前端配置可以在 apps/frontend/canary.yaml 中找到。

通過(guò)更新前端容器鏡像觸發(fā)部署:

  1. yq e '.images[0].newTag="5.0.1"' -i ./apps/frontend/kustomization.yaml 
  2.  
  3. git add -A && \ 
  4. git commit -m "frontend 5.0.1" && \ 
  5. git push origin main 
  6.  
  7. flux reconcile source git flux-system 

Flager 檢測(cè)到部署修訂已更改并開(kāi)始 A/B 測(cè)試:

  1. $ kubectl -n istio-system logs deploy/flagger -f | jq .msg 
  2.  
  3. New revision detected! Scaling up frontend.prod 
  4. Waiting for frontend.prod rollout to finish: 0 of 1 updated replicas are available 
  5. Pre-rollout check conformance-test passed 
  6. Advance frontend.prod canary iteration 1/10 
  7. ... 
  8. Advance frontend.prod canary iteration 10/10 
  9. Copying frontend.prod template spec to frontend-primary.prod 
  10. Waiting for frontend-primary.prod rollout to finish: 1 of 2 updated replicas are available 
  11. Promotion completed! Scaling down frontend.prod 

您可以通過(guò)以下方式監(jiān)控所有金絲雀:

  1. $ watch kubectl get canaries --all-namespaces 
  2.  
  3. NAMESPACE   NAME      STATUS        WEIGHT 
  4. prod        frontend  Progressing   100 
  5. prod        backend   Succeeded     0 

基于 Istio 指標(biāo)的回滾

Flagger 使用 Istio 遙測(cè)提供的指標(biāo)來(lái)驗(yàn)證金絲雀工作負(fù)載。前端應(yīng)用 analysis 定義了兩個(gè)指標(biāo)檢查:

  1. metrics: 
  2.    - name: error-rate 
  3.      templateRef: 
  4.        name: error-rate 
  5.        namespace: istio-system 
  6.      thresholdRange: 
  7.        max: 1 
  8.      interval: 30s 
  9.    - name: latency 
  10.      templateRef: 
  11.        name: latency 
  12.        namespace: istio-system 
  13.      thresholdRange: 
  14.        max: 500 
  15.      interval: 30s 

用于檢查錯(cuò)誤率(error rate)和延遲的 Prometheus 查詢(xún),位于 flagger-metrics.yaml。

在金絲雀分析期間,您可以生成 HTTP 500 errors 和高延遲(high latency)來(lái)測(cè)試 Flagger 的回滾。

生成 HTTP 500 errors:

  1. watch curl -b 'type=insider' http://<INGRESS-IP>/status/500 

生成延遲:

  1. watch curl -b 'type=insider' http://<INGRESS-IP>/delay/1 

當(dāng)失敗的檢查次數(shù)達(dá)到金絲雀分析閾值(threshold)時(shí),流量將路由回主服務(wù)器,金絲雀縮放為零,并將推出(rollout)標(biāo)記為失敗。

  1. $ kubectl -n istio-system logs deploy/flagger -f | jq .msg 
  2.  
  3. New revision detected! Scaling up frontend.prod 
  4. Pre-rollout check conformance-test passed 
  5. Advance frontend.prod canary iteration 1/10 
  6. Halt frontend.prod advancement error-rate 31 > 1 
  7. Halt frontend.prod advancement latency 2000 > 500 
  8. ... 
  9. Rolling back frontend.prod failed checks threshold reached 10 
  10. Canary failed! Scaling down frontend.prod 

您可以使用針對(duì) Prometheus、Datadog 和 Amazon CloudWatch 的自定義指標(biāo)檢查來(lái)擴(kuò)展分析。

有關(guān)為 Slack、MS Teams、Discord 或 Rocket 配置 canary 分析警報(bào)的信息,請(qǐng)參閱文檔。

責(zé)任編輯:姜華 來(lái)源: 黑客下午茶
相關(guān)推薦

2021-07-13 06:35:11

Argo Rollou GitOpsKubernetes

2022-08-22 10:40:40

Kubernete部署分析運(yùn)行

2021-06-24 08:25:38

flux2GitOps 云原生

2022-02-17 13:09:55

金絲雀部署服務(wù)集群測(cè)試

2023-04-11 07:59:56

Kruise漸進(jìn)式交付

2022-11-30 08:00:00

金絲雀部署IT測(cè)試

2021-07-29 05:09:54

Linkerd金絲雀部署Flagger

2023-10-08 07:34:04

2014-12-16 13:51:55

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

2010-04-27 13:41:42

云計(jì)算

2022-12-06 17:32:18

2021-06-15 05:52:33

Linkerd canary網(wǎng)絡(luò)技術(shù)

2023-09-28 07:34:33

2021-10-08 20:12:22

微服務(wù)架構(gòu)Service

2024-11-04 16:04:06

2024-01-02 07:37:52

FlaggerKubernetesIstio

2021-02-28 07:52:24

蠕蟲(chóng)數(shù)據(jù)金絲雀

2021-07-16 06:40:19

Argo RollouAnalysis云原生

2024-11-20 09:39:56

漸進(jìn)式遷移云策略云支出
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)