如何通过 Istio 在 Header 中加入 version=canary 路由到 Struts 灰度版本?
解读
在国内金融、运营商、政务云等真实落地场景中,Struts 2.x 应用通常以传统 WAR 包形式跑在 Tomcat 容器内,再被封装进 Docker 镜像,最终由 Kubernetes 托管。
面试官问“Header 加 version=canary 路由到灰度”,本质想验证三件事:
- 你是否理解 Istio 流量治理是 Layer7 决策,与 Struts 代码零侵入;
- 能否把“灰度”拆成 K8s 资源层(Deployment)+ 网络层(Service)+ 治理层(VirtualService & DestinationRule) 三段式落地;
- 是否具备 “可观测性闭环” 意识:灰度流量比例、错误率、RT 如何回显给运维。
知识点
- Istio 1.19+ 流量拓扑:Gateway → VirtualService → DestinationRule → Subset → Pod
- Header-based 路由优先级:VirtualService 中 match 条件按声明顺序匹配,精确匹配 > 前缀匹配 > 正则匹配
- Struts 2.x 容器化注意点:
- 必须把 server.xml 中 <Context path=""> 与 Istio 的 VirtualService 前缀保持一致,否则 404
- 镜像里要暴露 management.endpoints.web.exposure.include=health,info,供 K8s 探针与 Istio Sidecar 判定就绪
- DestinationRule 流量策略:
- loadBalancer.consistentHashHeader 可保证同一灰度用户会话粘连
- connectionPool.tcp.maxConnections 需调大,避免 Struts 拦截器链过长导致 Istio 限流
- 国内合规要求:金融客户要求灰度标签 version=canary 必须同时写入 access.log,以便审计;需在 Struts 的 log4j2.xml 里加 %X{version} 占位符,由 Istio 的 EnvoyFilter 注入
答案
步骤一:准备双 Deployment
# struts-stable-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: struts-app-stable
spec:
replicas: 3
selector:
matchLabels:
app: struts-app
version: stable
template:
metadata:
labels:
app: struts-app
version: stable
annotations:
sidecar.istio.io/inject: "true"
spec:
containers:
- name: struts
image: registry.cn-hangzhou.aliyuncs.com/xxx/struts:1.0.0
ports:
- containerPort: 8080
name: http
env:
- name: JAVA_OPTS
value: "-Dserver.servlet.context-path=/trade"
---
# struts-canary-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: struts-app-canary
spec:
replicas: 1
selector:
matchLabels:
app: struts-app
version: canary
template:
metadata:
labels:
app: struts-app
version: canary
annotations:
sidecar.istio.io/inject: "true"
spec:
containers:
- name: struts
image: registry.cn-hangzhou.aliyuncs.com/xxx/struts:1.1.0-canary
ports:
- containerPort: 8080
name: http
env:
- name: JAVA_OPTS
value: "-Dserver.servlet.context-path=/trade"
步骤二:统一 Service(无 selector 版本差异)
apiVersion: v1
kind: Service
metadata:
name: struts-app-svc
namespace: trade
spec:
ports:
- port: 80
targetPort: 8080
name: http
selector:
app: struts-app
步骤三:定义 DestinationRule,给两个版本打 Subset
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: struts-app-dr
namespace: trade
spec:
host: struts-app-svc.trade.svc.cluster.local
trafficPolicy:
connectionPool:
tcp:
maxConnections: 200
subsets:
- name: stable
labels:
version: stable
- name: canary
labels:
version: canary
步骤四:VirtualService 按 Header version=canary 精准路由
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: struts-app-vs
namespace: trade
spec:
hosts:
- struts-app-svc
http:
- match:
- headers:
version:
exact: canary
route:
- destination:
host: struts-app-svc.trade.svc.cluster.local
subset: canary
weight: 100
- route:
- destination:
host: struts-app-svc.trade.svc.cluster.local
subset: stable
weight: 100
步骤五:Gateway 层把流量引进来(以 Istio IngressGateway NodePort 模式为例,国内云厂商常用)
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
name: struts-gw
namespace: trade
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "struts.example.com"
---
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: struts-app-vs-gw
namespace: trade
spec:
hosts:
- "struts.example.com"
gateways:
- struts-gw
http:
- match:
- headers:
version:
exact: canary
route:
- destination:
host: struts-app-svc.trade.svc.cluster.local
subset: canary
- route:
- destination:
host: struts-app-svc.trade.svc.cluster.local
subset: stable
步骤六:验证
# 带灰度头
curl -H "version: canary" -H "Host: struts.example.com" http://$NODE_IP:31380/trade/order/query
# 返回 Server: envoy 且 body 中带 1.1.0-canary 字样
# 默认流量
curl -H "Host: struts.example.com" http://$NODE_IP:31380/trade/order/query
# 返回 1.0.0 版本
拓展思考
- 渐进式交付:结合 Flagger 自动把 Header 灰度过渡到 50% 流量,再全量;Struts 拦截器里可插 MDC.put("gray", "canary") 供日志链路染色
- 多租户场景:同一套 Struts 代码,不同省公司使用 x-tenant-id 与 version 组合路由,需用 Istio EnvoyFilter Lua 脚本 把两个头拼成一致哈希,防止会话漂移
- 性能调优:Envoy 默认 60s 超时,Struts 文件上传大表单可能触发 504,需在 VirtualService 的 timeout 字段显式放开 300s,并同步调高 DestinationRule 的 connectionPool.http.h2MaxRequests
- 回滚策略:若 canary 版本出现 OGNL 注入漏洞,可立即把 DestinationRule 的 canary subset 权重改为 0,无需重启 Pod,0 秒完成回滚
- 合规审计:国内银行要求灰度流量 “双录”(录流量、录日志),可用 Istio Telemetry V2 把 Header version=canary 的百分比、成功率实时推到 Prometheus + Grafana,并在 Struts 的 AccessLogValve 里打印 %H 头,满足央行 14 号文检查