Loading
close

超时

time 更新时间:2024-02-05 14:41:14

本章节介绍服务网格如何通过VirtualService配置,实现http超时功能。

操作场景

当网站发生故障,导致网站用户的请求一直处于等待状态时,为优化网站用户的浏览体验,需要为服务配置 timeout。

部署服务

部署 v1 和 v2 版本的 deployment以及对应的service 至集群

apiVersion: apps/v1
kind: Deployment
metadata:
  name: product-v2
  namespace: base
  labels:
    app: product
    version: v2
spec:
  replicas: 1
  selector:
    matchLabels:
      app: product
      version: v2
  template:
    metadata:
      labels:
        app: product
        version: v2
    spec:
      containers:
        - name: product
          image: nginx:1.25-alpine
          ports:
            - containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: product-v1
  namespace: base
spec:
  replicas: 1
  selector:
    matchLabels:
      app: product
      version: v1
  template:
    metadata:
      labels:
        app: product
        version: v1
    spec:
      containers:
        - name: product
          image: nginx:1.25-alpine
          ports:
            - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: product
  name: product
  namespace: base
spec:
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: product
  type: ClusterIP

应用服务网格配置

通过DestinationRule定义服务的subset v1和v2,在VirtualService配置中关联service(product),并添加timeout字段,可以实现超时自动断开,并返回504错误(Gateway Timeout),从而优化网站用户体验。将以下VirtualService部署至集群:

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: product-vs
  namespace: base
spec:
  hosts:
    - "product.base.svc.cluster.local"
  http:
    - match:
        - headers:
            cookie:
              exact: vip=false
      route:
        - destination:
            host: product.base.svc.cluster.local
            subset: v1
      timeout: 100ms
    - match:
        - headers:
            cookie:
              exact: vip=true
      route:
        - destination:
            host: product.base.svc.cluster.local
            subset: v2
      timeout: 300ms
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: product
  namespace: base
spec:
  host: product
  subsets:
    - name: v1
      labels:
        version: v1
    - name: v2
      labels:
        version: v2

完成配置之后,当请求来临时,cookie中如包含vip=false,其超时时间为100ms;cookie中如包含vip=true,其超时时间为300ms。

此篇文章对你是否有帮助?
没帮助
locked-file

您暂无权限访问该产品