kubernetes中经常需要配置sticky session,此时需要根据你使用的ingress controller来设置了。
1、Nginx Ingress
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: nginx-test annotations: nginx.ingress.kubernetes.io/affinity: "cookie" nginx.ingress.kubernetes.io/session-cookie-name: "route" nginx.ingress.kubernetes.io/session-cookie-expires: "172800" nginx.ingress.kubernetes.io/session-cookie-max-age: "172800" spec: rules: - host: stickyingress.example.com http: paths: - backend: serviceName: http-svc servicePort: 80 path: /
2、Traefik Ingress
--- apiVersion: extensions/v1beta1 kind: Ingress metadata: annotations: kubernetes.io/ingress.class: traefik labels: app: test name: test spec: rules: - host: 192.168.1.2 http: paths: - path: / backend: serviceName: test servicePort: 8080 --- apiVersion: v1 kind: Service metadata: name: test labels: app: test annotations: traefik.ingress.kubernetes.io/affinity: "true" traefik.ingress.kubernetes.io/session-cookie-name: "sticky" spec: type: NodePort ports: - port: 8080 targetPort: 8080 protocol: TCP name: http selector: app: demo
然后在外部Nginx的upstream配置ip_hash,即可实现 kubernetes sticky session。