<p>I am totally new to Istio, and the pitch looks very exciting. However, I can't make it work, which probably means I don't use it properly.
My goal is to implement session affinity between 2 services, this is why originally I end up using Istio. However, I do a very basic test, and it does not seem to work:
I have an kubernetes demo app which as a front service, a stateful-service, and a stateless service. From a browser, I access the front-service which dispatches the request either on the stateful or stateless services, using K8s service names as url: <a href="http://api-stateful" rel="nofollow noreferrer">http://api-stateful</a> or <a href="http://api-stateless" rel="nofollow noreferrer">http://api-stateless</a>.</p>

<p>I want to declare a virtual service to intercept requests sent from the front-service to the stateful-service.I don't declare it as a gateway, as I understood gateway was at the external border of the K8s cluster.
I use Docker on Windows with Istio 1.6.</p>

<p>I copy my yaml file below. the basic test I want to do: reroute traffic for api-stateful to api-stateless, to validate that the Virtual Service is taken into account. And it does not work. Do you see what is wrong? Is it a wrong usage of Virtual Service ? My Kiali console does not detect any problem in setup.</p>

<pre><code>####################################################################
######################### 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
</code></pre>

Joel
<p>As mentioned in comments this can be fixed with <code>DestinationRule</code> with sticky session configuration.</p>

<p>Example of that can be found in istio <a href="https://istio.io/latest/docs/reference/config/networking/destination-rule/#LoadBalancerSettings" rel="nofollow noreferrer">documentation</a>:</p>

<blockquote>
  <h2>LoadBalancerSettings<a href="https://istio.io/latest/docs/reference/config/networking/destination-rule/#LoadBalancerSettings" rel="nofollow noreferrer"></a></h2>
  
  <p>Load balancing policies to apply for a specific destination. See Envoy’s load balancing  <a href="https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/upstream/load_balancing/load_balancing" rel="nofollow noreferrer">documentation</a>  for more details.</p>
  
  <p>For example, the following rule uses a round robin load balancing policy for all traffic going to the ratings service.</p>
  
  <pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: bookinfo-ratings
spec:
  host: ratings.prod.svc.cluster.local
  trafficPolicy:
    loadBalancer:
      simple: ROUND_ROBIN

</code></pre>
  
  <p>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.</p>
  
  <pre class="lang-yaml prettyprint-override"><code> 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
</code></pre>
</blockquote>
