验证service sessionAffinity超出timeoutSeconds走轮询

k8s 1.15.0

sessionAffinity原理

service会话保持配置

...
  sessionAffinity: ClientIP
  sessionAffinityConfig:
    clientIP:
      timeoutSeconds: 10800
...

sessionAffinity默认是None,表示轮询。
sessionAffinity取值只有None或者ClientIP。

pkg/proxy/iptables/proxier.go
syncProxyRules函数

iptables规则

第一个请求匹配不上recent模块后走轮询,走完轮询,通过--set记录后端IP,最近默认3小时内有请求就会话保持。这里配置30s。
当有Pod故障恢复后,kube-proxy重置iptables规则,第一次请求走轮询。

验证超出timeoutSeconds后无请求走轮询

apiVersion: v1
kind: Service
metadata:
  name: tomcat
  namespace: default
spec:
  sessionAffinity: ClientIP
  sessionAffinityConfig:
    clientIP:
      timeoutSeconds: 30
  type: ClusterIP
  selector:
    app: tomcat
  ports:
  - name: http
    port: 80
    targetPort: 8080

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat
spec:
  replicas: 2
  selector:
    matchLabels:
      app: tomcat
  template:
    metadata:
      labels:
        app: tomcat
    spec:
      containers:
        - name: tomcat
          image: tomcat:8

无Pod异常情况下后端IP变化
bns4z->jcz4k->bns4z

 

posted on 2023-07-07 23:35  王景迁  阅读(128)  评论(0编辑  收藏  举报

导航