Sticky Session with Istio

NIRAV SHAH
3 min readMar 19, 2023

Envoy+Istio is widely adopted as the standard protocol in the Kubernetes ecosystem. However, configuring even simple Nginx settings within Istio can be a challenging task that requires research. It’s worth noting that enabling sticky session configuration in Nginx is only possible in the paid version, Nginx Plus. I attempted to request ChatGPT to assist me with this matter.

Istio is an open-source service mesh that allows you to manage and secure your microservices. It provides a variety of features to improve the reliability, security, and observability of your application. One of the key features of Istio is its ability to use sticky sessions with destination rules.

Sticky sessions are a way to ensure that a client always connects to the same instance of a microservice. This is important for stateful applications where session data is stored on a particular instance. If a client were to connect to a different instance on each request, their session data would be lost. Sticky sessions ensure that this does not happen by routing requests to the same instance each time.

To use sticky sessions with Istio, you need to create a destination rule. A destination rule is a configuration object that tells Istio how to route traffic to a specific service. It can be used to specify a subset of instances within a service and apply traffic routing policies to them.

To create a destination rule with sticky sessions, you need to specify a load balancing policy that uses the “Cookie” header. This header contains a unique identifier that is used to associate a client with a particular instance of a service. The load balancing policy should be set to “Cookie” and the name of the cookie should be specified in the “Sticky” field.

Here’s an example destination rule that uses sticky sessions:

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: my-service
spec:
host: my-service
trafficPolicy:
loadBalancer:
consistentHash:
httpHeaderName: Cookie
useSourceIp: false
simple: ROUND_ROBIN
connectionPool:
tcp:
maxConnections: 100
subsets:
- name: v1
labels:
version: v1
trafficPolicy:
loadBalancer:
consistentHash:
httpHeaderName: Cookie
useSourceIp: false
sticky:
cookie:
name: session-cookie

In this example, the destination rule specifies a subset of instances labeled “v1” and applies a load balancing policy with sticky sessions. The name of the cookie used to identify clients is “session-cookie”.

Once you have created the destination rule, you can apply it to your virtual service. A virtual service is a configuration object that defines how traffic should be routed to a service. You can specify the destination rule in the “route” field of the virtual service.

Here’s an example virtual service that uses the destination rule we created earlier:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: my-service
spec:
hosts:
- my-service
http:
- route:
- destination:
host: my-service
subset: v1

In this example, the virtual service routes traffic to the “v1” subset of instances of the “my-service” service.

In conclusion, sticky sessions with destination rules are an important feature of Istio that can help you ensure the reliability and consistency of your microservices. By using a destination rule with a load balancing policy that uses cookies, you can ensure that clients always connect to the same instance of a service. This is especially important for stateful applications where session data is stored on a particular instance.

Reference:

--

--