如何在前台和后台服务之间使用 Istio Virtual Service

How to use Istio Virtual Service in-between front and back services

我是 Istio 的新手,pitch 看起来非常令人兴奋。但是,我无法让它工作,这可能意味着我没有正确使用它。 我的目标是在 2 个服务之间实现会话关联,这就是我最初最终使用 Istio 的原因。但是,我做了一个非常基本的测试,但它似乎不起作用: 我有一个 kubernetes 演示应用程序,它作为前台服务、有状态服务和无状态服务。从浏览器,我访问前端服务,该服务在有状态或无状态服务上分派请求,使用 K8s 服务名称 url: http://api-stateful or http://api-stateless.

我想声明一个虚拟服务来拦截从前端服务发送到有状态的请求-service.I不要将其声明为网关,因为我知道网关位于K8s集群。 我在 Windows 上使用 Docker 和 Istio 1.6。

我在下面复制了我的 yaml 文件。我想做的基本测试:将 api-stateful 的流量重新路由到 api-stateless,以验证是否考虑了虚拟服务。它不起作用。你看到什么不对了吗?这是对虚拟服务的错误使用吗?我的 Kiali 控制台在设置中没有检测到任何问题。

####################################################################
######################### STATEFUL BACKEND #########################
# Deployment for pocbackend containers, listening on port 3000
apiVersion: apps/v1
kind: Deployment
metadata:
  name: stateful-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: stateful-backend
      tier: backend
  template:
    metadata:
      labels:
        app: stateful-backend
        tier: backend
    spec:
       containers:
       - name: pocbackend
         image: pocbackend:2.0
         ports:
           - name: http
             containerPort: 3000
---
# Service for Stateful containers, listening on port 3000
apiVersion: v1
kind: Service
metadata:
  name: api-stateful
spec:
  selector:
    app: stateful-backend
    tier: backend
  ports:
  - protocol: TCP
    port: 3002
    targetPort: http
---
#####################################################################
######################### STATELESS BACKEND #########################
# Deployment for pocbackend containers, listening on port 3000
apiVersion: apps/v1
kind: Deployment
metadata:
  name: stateless-backend
spec:
  replicas: 3
  selector:
    matchLabels:
      app: stateless-backend
      tier: backend
  template:
    metadata:
      labels:
        app: stateless-backend
        tier: backend
    spec:
      containers:
      - name: pocbackend
        image: pocbackend:2.0        
        ports:
           - name: http
             containerPort: 3000
 ---
# Service for Stateless containers, listening on port 3000
apiVersion: v1
kind: Service
 metadata:
  name: api-stateless
spec:
  selector:
    app: stateless-backend
    tier: backend
  ports:
  - protocol: TCP
    port: 3001
    targetPort: http
---
#############################################################
######################### FRONT END #########################
# deployment of the container pocfrontend listening to port 3500
apiVersion: apps/v1
kind: Deployment
metadata:
  name: front-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: frontend
      tier: frontend
  template:
    metadata:
      labels:
        app: frontend
        tier: frontend
    spec:
      containers:
      - name: pocfrontend
        image: pocfrontend:2.0               
        ports:
           - name: http
             containerPort: 3500      
---
# Service exposing frontend on node port 85
apiVersion: v1
kind: Service
metadata:
  name: frontend-service
spec:
  type: NodePort
  selector:
    app: frontend
    tier: frontend
  ports:
  - protocol: TCP
    port: 3500
    targetPort: http
    nodePort: 30000
---
##############################################################
############ ISTIO PROXY FOR API-STATEFUL SERVIC E############
##############################################################
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: api-stateful-proxy
spec:
  hosts:
  - api-stateful
  http: 
  - route:
      - destination:
          host: api-stateless

如评论中所述,这可以通过 DestinationRule 和粘性会话配置来解决。

可以在 istio 中找到示例 documentation:

LoadBalancerSettings

Load balancing policies to apply for a specific destination. See Envoy’s load balancing documentation for more details.

For example, the following rule uses a round robin load balancing policy for all traffic going to the ratings service.

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: bookinfo-ratings
spec:
  host: ratings.prod.svc.cluster.local
  trafficPolicy:
    loadBalancer:
      simple: ROUND_ROBIN

The following example sets up sticky sessions for the ratings service hashing-based load balancer for the same ratings service using the the User cookie as the hash key.

 apiVersion: networking.istio.io/v1alpha3
 kind: DestinationRule
 metadata:
   name: bookinfo-ratings
 spec:
   host: ratings.prod.svc.cluster.local
   trafficPolicy:
     loadBalancer:
       consistentHash:
         httpCookie:
           name: user
           ttl: 0s