Kubernetes:为什么我的请求被重定向到不同的 pods,尽管我将 sessionAffinity 设置为 ClientIP?

Kubernetes : why are my requests redirected to different pods although I set sessionAffinity to ClientIP?

在我的无外设服务中,我配置了 sessionAffinity,这样 来自特定客户端的连接每次都会传递到同一个 Pod as described here

这是清单:

apiVersion: v1
kind: Service
metadata:
  name: service1
spec:
  clusterIP: None
  selector:
    app: nginx
  sessionAffinity: ClientIP
  sessionAffinityConfig:
    clientIP:
      timeoutSeconds: 30

我运行一些nginxpods来测试:

$ kubectl create deployment nginx --image=stenote/nginx-hostname

问题是当我 curl 我的服务时,我被重定向到不同的 pods 并且 sessionAffinity 似乎被忽略了。

$ kubectl run --generator=run-pod/v1 --rm utils -it --image arunvelsriram/utils bash
root@utils:/# for i in $(seq 1 10) ; do curl service1; done
nginx-78d58889-b7fm2
nginx-78d58889-b7fm2
nginx-78d58889-b7fm2
nginx-78d58889-b7fm2
nginx-78d58889-b7fm2
nginx-78d58889-8rpxd
nginx-78d58889-b7fm2
nginx-78d58889-62jlw
nginx-78d58889-8rpxd
nginx-78d58889-62jlw

注意。当我检查

$ kubectl describe  svc service1
Name:              service1
Namespace:         abdelghani
Labels:            <none>
Annotations:       <none>
Selector:          app=nginx
Type:              ClusterIP
IP Families:       <none>
IP:                None
IPs:               <none>
Session Affinity:  ClientIP
Events:            <none>

SessionAffinity 配置存在。

请注意,我的服务是无头的,即 clusterIP: None。 SessionAffinity 似乎适用于非无头服务。但是,我无法在文档中找到明确的解释。是不是和平台没有代理有关?

阿卜杜勒加尼

使用无外设服务(集群 IP:None)时,您不使用代理。

来自k8s docs

For headless Services, a cluster IP is not allocated, kube-proxy does not handle these Services, and there is no load balancing or proxying done by the platform for them. How DNS is automatically configured depends on whether the Service has selectors defined

因此,当使用无头服务时,dns 会使用与给定服务关联的所有 pods 的随机 ips 列表进行响应。

/app # dig service1 +search +short
172.17.0.8
172.17.0.10
172.17.0.9
/app # dig service1 +search +short
172.17.0.9
172.17.0.10
172.17.0.8
/app # dig service1 +search +short
172.17.0.8
172.17.0.10
172.17.0.9
/app # dig service1 +search +short
172.17.0.10
172.17.0.9
172.17.0.8
/app # dig service1 +search +short
172.17.0.9
172.17.0.8
172.17.0.10

而 curl 只得到一个并随之而来。

由于每次请求都会发生这种情况,因此每次从 dns 获取不同的 ip 时,都会连接到不同的 pod。