自动化金丝雀部署之Flagger集成Istio-原理和基本实战

引言

本文通过监听资源对象、日志、事件等方法讲解和验证了Flagger和Istio实现自动化金丝雀部署的原理
由于内容较长,仅验证了自动化金丝雀部署和回滚两个功能。

实战内容参考文档:https://fluxcd.io/flagger/tutorials/istio-progressive-delivery/

什么是Flagger?

官网:https://fluxcd.io/flagger/

Flagger是一种渐进式交付工具,会在流量迁移时分析测量指标和运行测试任务降低发版风险,仅可以发布运行在Kubernetes的应用。

  • 流量路由:Flagger采用服务网格或Ingress实现,服务网格包括:App Mesh、Istio、Linkerd、Kuma、Open Service Mesh,Ingress包括:Contour、Gloo、NGINX、Skipper、 Traefik、APISIX
  • 发布分析和报警:分析数据来源支持 Prometheus、InfluxDB、Datadog、New Relic、CloudWatch、Stackdriver 、Graphite,报警支持 Slack、MS Teams、Discord 和 Rocket。

Flagger集成Istio原理

创建Canary时会解释各个资源对象的内容和功能

准备环境

安装Istio

参考博客:《Istio:搭建Istio完整环境》

安装Flagger

参考安装文档:https://fluxcd.io/flagger/install/flagger-install-on-kubernetes/

helm repo add flagger https://flagger.app
kubectl apply -f https://raw.githubusercontent.com/fluxcd/flagger/main/artifacts/flagger/crd.yaml

helm upgrade -i flagger flagger/flagger \
--namespace=istio-system \
--set crd.create=false \
--set meshProvider=istio \
--set metricsServer=http://prometheus:9090

验证安装结果:

创建IngressGateway

cat <<EOF | kubectl apply -f -
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: public-gateway
  namespace: istio-system
spec:
  selector:
    istio: ingressgateway
  servers:
    - port:
        number: 80
        name: http
        protocol: HTTP
      hosts:
        - "*"  
EOF

部署podinfo

kubectl create ns test
kubectl label namespace test istio-injection=enabled
kubectl apply -k https://github.com/fluxcd/flagger//kustomize/podinfo?ref=main
kubectl apply -k https://github.com/fluxcd/flagger//kustomize/tester\?ref\=main

部署结果

创建Canary

与官网案例差异点:将example.com替换为test.canary.istio.cn

cat <<EOF | kubectl apply -f -
apiVersion: flagger.app/v1beta1
kind: Canary
metadata:
  name: podinfo
  namespace: test
spec:
  # deployment reference
  targetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: podinfo
  # the maximum time in seconds for the canary deployment
  # to make progress before it is rollback (default 600s)
  progressDeadlineSeconds: 60
  # HPA reference (optional)
  autoscalerRef:
    apiVersion: autoscaling/v2beta2
    kind: HorizontalPodAutoscaler
    name: podinfo
  service:
    # service port number
    port: 9898
    # container port number or name (optional)
    targetPort: 9898
    # Istio gateways (optional)
    gateways:
    - public-gateway.istio-system.svc.cluster.local
    # Istio virtual service host names (optional)
    hosts:
    - test.canary.istio.cn
    # Istio traffic policy (optional)
    trafficPolicy:
      tls:
        # use ISTIO_MUTUAL when mTLS is enabled
        mode: DISABLE
    # Istio retry policy (optional)
    retries:
      attempts: 3
      perTryTimeout: 1s
      retryOn: "gateway-error,connect-failure,refused-stream"
  analysis:
    # schedule interval (default 60s)
    interval: 1m
    # max number of failed metric checks before rollback
    threshold: 5
    # max traffic percentage routed to canary
    # percentage (0-100)
    maxWeight: 50
    # canary increment step
    # percentage (0-100)
    stepWeight: 10
    metrics:
    - name: request-success-rate
      # minimum req success rate (non 5xx responses)
      # percentage (0-100)
      thresholdRange:
        min: 99
      interval: 1m
    - name: request-duration
      # maximum req duration P99
      # milliseconds
      thresholdRange:
        max: 500
      interval: 30s
    # testing (optional)
    webhooks:
      - name: acceptance-test
        type: pre-rollout
        url: http://flagger-loadtester.test/
        timeout: 30s
        metadata:
          type: bash
          cmd: "curl -sd 'test' http://podinfo-canary:9898/token | grep token"
      - name: load-test
        url: http://flagger-loadtester.test/
        timeout: 5s
        metadata:
          cmd: "hey -z 1m -q 10 -c 2 http://podinfo-canary.test:9898/"
EOF

查看flagger日志如下:

日志告诉我们:

  1. 创建了灰度和主两个Service
  2. 创建了主Deployment,同时等待Deployment就绪
  3. 创建了主HPA
  4. 创建了主和灰度两个DR
  5. 创建了VS

接下来具体分析各资源,并了解它们实现了什么功能。

自动创建和更新Deployment

自动创建Service

各Service关键差异如下:

自动创建和更新HPA

自动创建VS(VirtualService)和DR(DestinationRule)

创建VS

创建DR

流量控制:流量100%到podinfo-primary(对应自动创建的Service podinfo-primary

Canary 状态变化

【案例1】验证金丝雀发布

注意:如果您在金丝雀分析期间对部署应用新的更改,Flagger将重新启动分析。

金丝雀部署由以下任何对象的更改触发:

  1. 部署 PodSpec(容器镜像、命令、端口、环境、资源等)
  2. ConfigMap 作为卷安装或映射到环境变量
  3. 作为卷安装或映射到环境变量的秘密

变化资源有:Canary(状态数据)、Deployment、VS

步骤1:更新容器镜像触发

kubectl -n test set image deployment/podinfo podinfod=ghcr.io/stefanprodan/podinfo:6.0.1

步骤2:查看Canary状态变化

问题1:为什么检查失败仍然发版成功?因为检查失败了4次(未达到5次),Canary配置如下:

步骤3:查看Deployment变化

步骤4:查看VS变化

灰度流量每次增加10%

步骤5:查看Flagger日志

访问链接:http://grafana.istio.cn:3000/explore

【案例2】验证自动回滚

验证方法:模拟HTTP 500错误和高延迟,观察Flagger是否暂停推出

观察方法:通过日志和canary事件

步骤1:更新镜像

kubectl -n test set image deployment/podinfo podinfod=ghcr.io/stefanprodan/podinfo:6.0.2

步骤2:模拟HTTP 500 错误,每2秒调用一次

loadtester=$(k -n test get po -l app=flagger-loadtester | awk 'END {print $1}')

while :; do kubectl -n test exec -it $loadtester -- sh -c 'curl -i http://podinfo-canary:9898/status/500'; sleep 2; done

步骤3:模拟高延迟,每2秒调用一次

loadtester=$(k -n test get po -l app=flagger-loadtester | awk 'END {print $1}')
while :; do kubectl -n test exec -it $loadtester -- sh -c 'curl -i http://podinfo-canary:9898/delay/1'; sleep 2; done

步骤4:查看Canary事件

步骤5:查看日志

附录

【Istio】内置指标:request-success-rate

代码位置:https://github.com/fluxcd/flagger/blob/main/pkg/metrics/observers/istio.go#L29

promql模板(gotemplate):

sum(
		rate(
			istio_requests_total{
				reporter="destination",
				destination_workload_namespace="{{ namespace }}",
				destination_workload=~"{{ target }}",
				response_code!~"5.*"
			}[{{ interval }}]
		)
	) 
	/ 
	sum(
		rate(
			istio_requests_total{
				reporter="destination",
				destination_workload_namespace="{{ namespace }}",
				destination_workload=~"{{ target }}"
			}[{{ interval }}]
		)
	) 
	* 100

指标数据示例

【Istio】内置指标:request-duration

代码位置:https://github.com/fluxcd/flagger/blob/main/pkg/metrics/observers/istio.go#L51

promql模板(gotemplate):

histogram_quantile(
		0.99,
		sum(
			rate(
				istio_request_duration_milliseconds_bucket{
					reporter="destination",
					destination_workload_namespace="{{ namespace }}",
					destination_workload=~"{{ target }}"
				}[{{ interval }}]
			)
		) by (le)
	)

指标数据示例

结语

本文全面解答了应用Flagger和Istio的所有疑问,若您还遇到了其它问题和诉求,请留言告知🙏。

todo:即将输出自动化金丝雀部署的会话亲和性镜像流量A/B测试案例。

请用微信扫码关注下🙏 ,持续更新云原生DevOps最佳实践。

  • 35
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
为什么使用istio:云平台令使用它们的公司受益匪浅。但不可否认的是,上云会给 DevOps 团队带来压力。为了可移植性,开发人员必须使用微服务来构建应用,同时运维人员也正在管理着极端庞大的混合云和多云的部署环境。 Istio 允许您连接、保护、控制和观察服务。从较高的层面来说,Istio 有助于降低这些部署的复杂性,并减轻开发团队的压力。它是一个完全开源的服务网格,作为透明的一层接入到现有的分布式应用程序里。它也是一个平台,拥有可以集成任何日志、遥测和策略系统的 API 接口。Istio 多样化的特性使您能够成功且高效地运行分布式微服务架构,并提供保护、连接和监控微服务的统一方法。教学内容:istio原理,envoy原理,envoy案例,envoy配置,istio crd配置,istio流量管理,istio安全配置,istio可观察性,istio策略控制,istio升级,istio常见问题,istio wasm,istio多控制面板,gateway-api,slime教学特色:a.1000多个istio实战案例,20多个envoy案例。800多个envoyfilter案例,全程已实战为主,理论相对较少,案例90%可试验b.涵盖98%以上crd字段配置c.不仅讲解yaml配置,同时结合envoy配置讲解d.不回避难点内容,深入讲解envoyfilter配置e深入讲解envoyf详细讲解额外内容,比如gateway-api,wasm,升降级,发布,灰度发布,蓝绿发布,istioctl命令,slime,多控制面板,多集群,常见问题g以一个完整案例串联所有内容h以markdown文件提供课件,内容详细,方便大家练习I有学员指出我的istio课程不够突出重点,安装80/20原则,20%内容是常用的,那我是否就讲这20%就可以了呢,其他课程确实是这么干的,他们只讲擅长的20%,我的目的不是这样的,我希望istio课程买我的一个就够了,让你全面学习istio,甚至遇到偏的问题不需要百度,课程里就有讲过,但是难免会出现一个问题,就是不够突出重点,我尽量兼顾全面的时候突出重点,讲到重点,核心功能时我会提示下。 

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值