Skip to content

Instantly share code, notes, and snippets.

@theundefined
Forked from suside/HAProxy_and_K8s.md
Created June 14, 2017 06:45
Show Gist options
  • Save theundefined/19e18ba6150ddddc625fdb941ee7a3cb to your computer and use it in GitHub Desktop.
Save theundefined/19e18ba6150ddddc625fdb941ee7a3cb to your computer and use it in GitHub Desktop.
Kubernetes + HAProxy sticky session affinity

Kubernetes + HAProxy sticky session affinity

  1. Make sure you have balance source in haproxy.
  2. Backend server section in haproxy config should have all your k8s nodes.
  3. sessionAffinity in k8s is irrelevant.
  4. Exposed k8s service need to have nodePort set and this annotation:

kubectl annotate service myService service.beta.kubernetes.io/external-traffic=OnlyLocal

This will cause internal k8s loadbalancer on nodeⁿ to route traffic only to pod on nodeⁿ. From Haproxy point of view it will look like nodeⁿ:nodePort === pod on nodeⁿ:port thus disabling k8s LB completly.

Further reading: https://kubernetes.io/docs/tutorials/services/source-ip/#source-ip-for-services-with-typenodeport

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment