Ingress session sticky

本文探讨了在 Kubernetes 的 Ingress 中为何无法直接使用 Service 的会话粘滞性,并介绍了 Nginx Controller 和 Traefik Controller 的配置方法进行会话亲和性设置。Nginx Controller 可通过特定注解实现 Cookie 基础的会话持久化,而 Traefik Controller 则提供了相应的 Kubernetes Ingress 配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

service session sticky

kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 80
  sessionAffinity: ClientIP
  sessionAffinityConfig:
    clientIP:
      timeoutSeconds: 10

ingress

  • 问题:为什么在ingress中不能使用上面service的session sticky。
  • 答案:因为ingress controller中配置的是POD的地址,不经过service,所以上面的service方式不生效。

Nginx Controller

https://kubernetes.github.io/ingress-nginx/examples/affinity/cookie/

Session affinity can be configured using the following annotations:

NameDescriptionValue
nginx.ingress.kubernetes.io/affinityType of the affinity, set this to cookie to enable session affinitystring (NGINX only supports cookie)
nginx.ingress.kubernetes.io/affinity-modeThe affinity mode defines how sticky a session is. Use balanced to redistribute some sessions when scaling pods or persistent for maximum stickyness.balanced (default) or persistent
nginx.ingress.kubernetes.io/session-cookie-nameName of the cookie that will be createdstring (defaults to INGRESSCOOKIE)
nginx.ingress.kubernetes.io/session-cookie-pathPath that will be set on the cookie (required if your Ingress paths use regular expressions)string (defaults to the currently matched path)
nginx.ingress.kubernetes.io/session-cookie-samesiteSameSite attribute to apply to the cookieBrowser accepted values are None, Lax, and Strict
nginx.ingress.kubernetes.io/session-cookie-conditional-samesite-noneWill omit SameSite=None attribute for older browsers which reject the more-recently defined SameSite=None value“true” or “false”
nginx.ingress.kubernetes.io/session-cookie-max-ageTime until the cookie expires, corresponds to the Max-Age cookie directivenumber of seconds
nginx.ingress.kubernetes.io/session-cookie-expiresLegacy version of the previous annotation for compatibility with older browsers, generates an Expires cookie directive by adding the seconds to the current datenumber of seconds
nginx.ingress.kubernetes.io/session-cookie-change-on-failureWhen set to false nginx ingress will send request to upstream pointed by sticky cookie even if previous attempt failed. When set to true and previous attempt failed, sticky cookie will be changed to point to another upstream.true or false (defaults to false)

You can create the example Ingress to test this:

bash-3.2$less ingress.yaml
apiVersion: networking.k8s.io/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: /

bash-3.2$kubectl create -f ingress.yaml
验证

You can confirm that the Ingress works:

$ kubectl describe ing nginx-test
Name:           nginx-test
Namespace:      default
Address:
Default backend:    default-http-backend:80 (10.180.0.4:8080,10.240.0.2:8080)
Rules:
  Host                          Path    Backends
  ----                          ----    --------
  stickyingress.example.com
                                /        nginx-service:80 (<none>)
Annotations:
  affinity: cookie
  session-cookie-name:      INGRESSCOOKIE
  session-cookie-expires: 172800
  session-cookie-max-age: 172800
Events:
  FirstSeen LastSeen    Count   From                SubObjectPath   Type        Reason  Message
  --------- --------    -----   ----                -------------   --------    ------  -------
  7s        7s      1   {nginx-ingress-controller }         Normal      CREATE  default/nginx-test


$ curl -I http://stickyingress.example.com
HTTP/1.1 200 OK
Server: nginx/1.11.9
Date: Fri, 10 Feb 2017 14:11:12 GMT
Content-Type: text/html
Content-Length: 612
Connection: keep-alive
Set-Cookie: INGRESSCOOKIE=a9907b79b248140b56bb13723f72b67697baac3d; Expires=Sun, 12-Feb-17 14:11:12 GMT; Max-Age=172800; Path=/; HttpOnly
Last-Modified: Tue, 24 Jan 2017 14:02:19 GMT
ETag: "58875e6b-264"
Accept-Ranges: bytes

traefik Controller

https://docs.traefik.io/routing/providers/kubernetes-ingress/

kind: Ingress
apiVersion: networking.k8s.io/v1beta1
metadata:
  name: myingress
  annotations:
    traefik.ingress.kubernetes.io/service.sticky.cookie: "true"
    traefik.ingress.kubernetes.io/service.sticky.cookie.name: foobar

spec:
  rules:
    - host: example.com
      http:
        paths:
          - path: /bar
            backend:
              serviceName: whoami
              servicePort: 80
          - path: /foo
            backend:
              serviceName: whoami
              servicePort: 80
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

正说杂谈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值