GCE Kubernetes Session Persistence

2/12/2018

I'm running a wordpress / woocommerce site on GCE Kubernetes and having trouble scaling because of session persistence.

The LoadBalancer (GCE Ingress) sends all traffic to a reverse proxy that then sends the traffic to different services I have set up, one of which is wordpress.

If I use SessionAffinity: ClientIP on the WordPress service all of the traffic goes to one pod and the others are ignored. This seems to because the service is seeing the LoadBalancer's ip address rather than the Client's. This is in spite of externalTrafficPolicy: Local set on both nginx reverse proxy and the wordpress NodePort services.

I've also tried using the wordpress service as the default backend and I managed to get traffic to go to all pods but lost session affinity.

The Ingress also performs TLS termination, which I've seen can effect ClientIP visibility, but I think that issue is resolved by the external traffic policy.

We are also using Cloudflare, I'm wondering if that could have an effect. But we are using the ngx_http_realip_module to try to get the correct Client IP address.

-- Steve
google-compute-engine
kubernetes
kubernetes-ingress

1 Answer

2/12/2018

I had a similar issue in one of the PHP services deployed in my cluster. Sessions are evil :) but sometimes you do need to use them. You can cluster your session data in PHP in couple ways, so that you do not need to use sticky sessions on loadbalancer(s).

  • shared RWX volume in your pod(s) that will keep the session files available to all instances in your deployment. Unless you use something like S3 for wordpress uploads, you probably already do something similar for binaries, as I suggest for session files.
  • session handler with Memcached or Redis as the session storage (this is what I have now)
  • you can even keep them in your MySQL, same as WP database, although I've seen that it can be of significant performance impact.

You can find simple mamcache example here. If you'd need a clustered storage, you could look into Redis clustering, or, as I would, into Couchbase

-- Radek 'Goblin' Pieczonka
Source: StackOverflow